/*
    This file is a part of the SteamWar software.

    Copyright (C) 2020  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;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

public class RunnableScriptEvent {

    public enum LocationType {
        STATIC,
        DYNAMIC,
        DEFAULT,
        CUSTOM
    }

    public final ScriptedItem.EventType eventType;
    public final Entity entity;
    private final Location location;
    private Location customLocation;
    private LocationType locationType = LocationType.DEFAULT;
    Script.ScriptExecutor scriptExecutor = null;

    public RunnableScriptEvent(ScriptedItem.EventType eventType, Entity entity, Location location) {
        this.eventType = eventType;
        this.entity = entity;
        this.location = location;
    }

    public Location getLocation() {
        // Custom location
        if (locationType == LocationType.CUSTOM && customLocation != null) return customLocation;

        // Static initial Location
        if (locationType == LocationType.STATIC) return location;

        // Dynamic Location if entity is not null
        if (locationType == LocationType.DYNAMIC) return entity != null ? entity.getLocation() : location;

        // Default Location is static if EventType is onClick otherwise dynamic
        if (eventType == ScriptedItem.EventType.onClick) return location;
        if (entity != null) return entity.getLocation();
        return location;
    }

    public void setLocationType(LocationType locationType) {
        if (locationType == null) return;
        this.locationType = locationType;
    }

    public void setCustomLocation(double x, double y, double z, float pitch, float yaw) {
        this.customLocation = new Location(location.getWorld(), x, y, z, yaw, pitch);
    }

    public Player getPlayer() {
        return (Player) entity;
    }

    public void resumeScriptExecution() {
        if (scriptExecutor == null) return;
        scriptExecutor.resume();
    }

}
