/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.misslewars.scripts.implemented;

import com.google.gson.JsonObject;
import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import de.steamwar.misslewars.scripts.RunnableScriptEvent.LocationType;
import org.bukkit.Location;

import java.util.HashMap;
import java.util.Map;

import static de.steamwar.misslewars.scripts.utils.JsonUtils.getDouble;
import static de.steamwar.misslewars.scripts.utils.JsonUtils.getString;

public class LocationScript implements RunnableScript {

    private static final Map<String, LocationType> locationTypeMap = new HashMap<>();
    private static final Map<String, ScriptFunction> locationMap = new HashMap<>();

    static {
        locationTypeMap.put("static", LocationType.STATIC);
        locationTypeMap.put("dynamic", LocationType.DYNAMIC);
        locationTypeMap.put("custom", LocationType.CUSTOM);
        locationTypeMap.put("default", LocationType.DEFAULT);

        locationMap.put("offsetentity", (runnableScriptEvent, doubles) -> {
            if (runnableScriptEvent.entity == null) return false;
            Location location1 = runnableScriptEvent.entity.getLocation();
            runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2], 0, 0);
            return false;
        });
        locationMap.put("offsetlocation", (runnableScriptEvent, doubles) -> {
            Location location1 = runnableScriptEvent.getLocation();
            runnableScriptEvent.setCustomLocation(location1.getX() + doubles[0], location1.getY() + doubles[1], location1.getZ() + doubles[2], 0, 0);
            return false;
        });
        ScriptFunction absoluteLocation = (runnableScriptEvent, doubles) -> {
            runnableScriptEvent.setCustomLocation(doubles[0], doubles[1], doubles[2], 0, 0);
            return false;
        };
        locationMap.put("absolute", absoluteLocation);
        locationMap.put("fix", absoluteLocation);
        locationMap.put("fixed", absoluteLocation);
    }

    private LocationType locationType = null;
    private ScriptFunction locationExecutor = null;

    private double x, y, z = 0;

    public LocationScript(JsonObject location) {
        if (location.has("location")) {
            JsonObject jsonObject = location.getAsJsonObject("location");
            getDouble(jsonObject, "x", value -> x = value);
            getDouble(jsonObject, "y", value -> y = value);
            getDouble(jsonObject, "z", value -> z = value);
            locationExecutor = locationMap.getOrDefault(getString(jsonObject, "type", "").toLowerCase(), null);
            locationType = LocationType.CUSTOM;
        } else if (location.has("locationType")) {
            locationType = locationTypeMap.getOrDefault(getString(location, "locationType", "").toLowerCase(), LocationType.DEFAULT);
        }
    }

    @Override
    public boolean execute(RunnableScriptEvent runnableScriptEvent) {
        runnableScriptEvent.setLocationType(locationType);
        return defaultExecution(locationExecutor, true, b -> true, runnableScriptEvent, x, y, z);
    }

}
