package nl.sbdeveloper.themeparkplus.listeners;

import nl.sbdeveloper.themeparkplus.ThemeParkPlus;
import nl.sbdeveloper.themeparkplus.api.PlusAPI;
import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow;
import nl.sbdeveloper.themeparkplus.util.ConfigUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;

import java.util.Collections;

public class WaitingTimeListener implements Listener {
    private static final double PER_PLAYER = ThemeParkPlus.getSConfig().getFile().getDouble("WaitingRow.MinutesPerPlayer");

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
        doWaitingRowThings(null, e.getPlayer().getLocation().getBlock().getLocation());
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e) {
        doWaitingRowThings(e.getPlayer().getLocation().getBlock().getLocation(), null);
    }

    @EventHandler
    public void onTeleport(PlayerTeleportEvent e) {
        if (e.getTo() == null) return;
        if ((e.getFrom().getBlockX() != e.getTo().getBlockX())
                || (e.getFrom().getBlockY() != e.getTo().getBlockY())
                || (e.getFrom().getBlockZ() != e.getTo().getBlockZ())) {
            doWaitingRowThings(e.getFrom().getBlock().getLocation(), e.getTo().getBlock().getLocation());
        }
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent e) {
        if (e.getTo() == null) return;
        if ((e.getFrom().getBlockX() != e.getTo().getBlockX())
                || (e.getFrom().getBlockY() != e.getTo().getBlockY())
                || (e.getFrom().getBlockZ() != e.getTo().getBlockZ())) {
            doWaitingRowThings(e.getFrom().getBlock().getLocation(), e.getTo().getBlock().getLocation());
        }
    }

    private void doWaitingRowThings(Location from, Location to) {
        WaitingRow row = PlusAPI.getRow(from);
        WaitingRow row2 = PlusAPI.getRow(to);

        WaitingRow usedRow = null;
        int waitingNew = 0;
        double waitingTimeNew = 0;
        if (row == null && row2 != null) {
            //ENTER
            usedRow = row2;
            waitingNew = row2.getWaitingPlayers() + 1;
            waitingTimeNew = row2.getWaitingTimeMinutes() + PER_PLAYER;
        } else if (row != null && row2 == null){
            //LEAVE
            usedRow = row;
            waitingNew = row.getWaitingPlayers() - 1;
            waitingTimeNew = row.getWaitingTimeMinutes() - PER_PLAYER;
        }

        if (usedRow == null) return;

        usedRow.setWaitingPlayers(waitingNew);
        usedRow.setWaitingTimeMinutes(waitingTimeNew);

        for (Location signLocation : usedRow.getSignLocations()) {
            BlockState signblocks = signLocation.getBlock().getState();
            if (signblocks instanceof Sign) {
                Sign signs = (Sign) signblocks;
                signs.setLine(3, waitingNew + " min.");
                signs.update();
            } else {
                Bukkit.getLogger().warning(ConfigUtil.getMessage("WaitingRow.WrongLocation", Collections.singletonMap("%ridename%", usedRow.getRideID())));
                if (usedRow.getSignLocations().size() == 1) {
                    PlusAPI.removeRow(usedRow);
                } else {
                    usedRow.removeSignLocation(signLocation);
                }
                ThemeParkPlus.getData().save();
            }
        }
    }
}
