package moe.oko.opennaw.util;

import moe.oko.opennaw.OpenNAW;
import moe.oko.opennaw.model.City;
import moe.oko.opennaw.model.ExplosivesData;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.entity.EntityExplodeEvent;

import java.util.*;

public class TNTHandler {
    List<ExplosivesData> explosiveSave = new ArrayList<>();
    List<TNTPrimed> pendingExplosives = new ArrayList<>();

    public void startExplosive(Block block, Player player) {
        var city = OpenNAW.getInstance().getCityHandler().fetchCityViaName(WorldGuardHelper.fetchCityRegionViaLoc(block.getLocation()).getId());
        block.setType(Material.AIR);
        if(WorldGuardHelper.isPlayerPermitted(WorldGuardHelper.fetchCityRegionViaLoc(block.getLocation()), player)) {
            //Betrayal has its benefits... (10 Second explosive time)
            player.sendMessage(ChatColor.YELLOW + "" + ChatColor.ITALIC + "Your knowledge of the city allows you to evade the guards who may call out for help...");
            var tnt = (TNTPrimed) block.getWorld().spawnEntity(block.getLocation(), EntityType.PRIMED_TNT, false);
            tnt.setFuseTicks(200);
            pendingExplosives.add(tnt);
        } else {
            //Normal start. (60 Second explosive time + warning)
            Bukkit.broadcastMessage(ChatColor.RED + player.getDisplayName()
                    + ChatColor.GOLD + " has placed an improvised explosive at the walls of " + ChatColor.AQUA + city.getName() + ChatColor.GOLD + "!");
            var tnt = (TNTPrimed) block.getWorld().spawnEntity(block.getLocation(), EntityType.PRIMED_TNT, false);
            tnt.setFuseTicks(1200);
            pendingExplosives.add(tnt);
        }
    }

    public void defuse(TNTPrimed tnt) {
        pendingExplosives.remove(tnt);
        var loc = tnt.getLocation();
        tnt.remove(); //Delete tnt
        loc.getWorld().spawnParticle(Particle.WHITE_ASH, loc, 1000, .5, .5, .5);
    }

    public void detonate(EntityExplodeEvent event) {
        var city = OpenNAW.getInstance().getCityHandler().fetchCityViaName(WorldGuardHelper.fetchCityRegionViaLoc(event.getLocation()).getId());
        explosiveSave.add(new ExplosivesData(event.blockList(), city));
        pendingExplosives.remove((TNTPrimed) event.getEntity());
    }

    public void endSiege(City city) {
        for (var explosive : explosiveSave) {
            if(explosive.getCity().equals(city)) {
                explosive.regenerate();
                explosiveSave.remove(explosive);
            }
        }
    }

    public void shutdown() {
        for(var data : explosiveSave) {
            data.regenerate(); //Regen all before shutdown of server.
        }
        for(var tnt : pendingExplosives) {
            tnt.remove(); //Remove all tnt
        }
    }
}
