package moe.oko.opennaw.model;

import moe.oko.opennaw.OpenNAW;
import net.kyori.adventure.text.Component;

import org.bukkit.*;

public class City {
    private String name;
    private String resource;
    private CityState state;
    private Nation owner;

    //Null until its needed.
    private Nation attacker;

    private Location point;
    private short health;

    public City(String name, String resource, Location location) {
        this.name = name;
        this.resource = resource;
        //Default is unclaimed.
        this.owner = Nation.UNCLAIMED;
        this.state = CityState.NORMAL;
        this.point = location;
        this.health = 300;
    }

    //DB Const
    public City(String name, String resource, Location location, Nation owner) {
        this.name = name;
        this.resource = resource;
        this.point = location;
        this.owner = owner;
        this.health = 300;
        this.state = CityState.NORMAL;
    }

    public String getName() {
        return name;
    }

    public String getResource() {
        return resource;
    }

    public Nation getOwner() {
        return owner;
    }

    public void setOwner(Nation nation) {
        this.owner = nation;
    }

    public Location getPoint() {
        return point;
    }

    public short getHealth() {
        return health;
    }

    public void damage(short damage) {
        this.health -= damage;
    }

    public boolean underSiege() {
        return state.equals(CityState.SIEGE);
    }

    public void startSiege(Nation nation) {
        state = CityState.SIEGE;
        Bukkit.broadcast(Component.text(ChatColor.RED + nation.getName() + ChatColor.GOLD + " has begun a siege against the city of " + ChatColor.AQUA + getName()));
        for(var player : Bukkit.getOnlinePlayers()) {
            player.playSound(player.getLocation(), Sound.ITEM_TRIDENT_THUNDER, 1F, 1F);
        }
        attacker = nation;
    }

    public void damage() {
        //Ran each time a city is damaged by a capture event
        //Makes an explosion of ash around a block
        point.getWorld().spawnParticle(Particle.EXPLOSION_LARGE, point, 1, .01, .01, .01);
        point.getWorld().playSound(point, Sound.ENTITY_GENERIC_EXPLODE, 1F, 1F);
        health = (short) (health - OpenNAW.getInstance().getConfigHelper().cityDamageOnStrike());
        switch (health) {
            case 250:
            case 200:
            case 150:
            case 100:
            case 50:
                for(var player : Bukkit.getOnlinePlayers()) {
                    if(player.getLocation().distance(point) < 100) {
                        player.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "THE CITY HAS FALLEN TO " + health + " HEALTH...");
                    }
                }
                return;
            case 10:
                for(var player : Bukkit.getOnlinePlayers()) {
                    if(player.getLocation().distance(point) < 100) {
                        player.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "THE CITY IS ABOUT TO BE CAPTURED!");
                    }
                }
        }
        if(health == 0 || health < 0) {
            capture();
        }
    }

    public void defend() {
        //successful defense logic.
        Bukkit.broadcast(Component.text(ChatColor.RED + attacker.getName() + ChatColor.GOLD + "'s siege against the city of "
                + ChatColor.AQUA + getName() + ChatColor.GOLD + " has " + ChatColor.RED + "failed"));
        if(health < 250) {
            health = (short) (health + 50);
        }
        OpenNAW.getInstance().getActionLogger().cancelDefenseActions(this);
        attacker = null;
    }

    public void capture() {
        setOwner(attacker);
        attacker = null; //reset to null so there isn't any bugs
        //This is our "capture" logic. So now we do shenanigans.
        if(OpenNAW.getInstance().isDynmapEnabled()) {
            //Dyn is enabled, we need to do setters.
            OpenNAW.getInstance().getDynmapHandler().updateCityInfoMarker(this);
        }
        Bukkit.broadcast(Component.text(ChatColor.AQUA + getName() + ChatColor.GOLD + " has been successfully captured by " + ChatColor.RED + this.owner.getName()));
    }

    public boolean isUnclaimed() {
        return owner == Nation.UNCLAIMED;
    }
}