package moe.oko.opennaw.listener;

import moe.oko.opennaw.OpenNAW;
import moe.oko.opennaw.model.action.CityAttackAction;
import moe.oko.opennaw.model.action.CityDefenseAction;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;

public class ActionListener implements Listener {

    @EventHandler
    public void onBlockInteract(PlayerInteractEvent event) {
        if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
            //Right Click Logic
            if(OpenNAW.getInstance().getCityHandler().isLocationAPoint(event.getClickedBlock().getLocation())) {
                var city = OpenNAW.getInstance().getCityHandler().fetchCityViaPoint(event.getClickedBlock().getLocation());
                if(!OpenNAW.getInstance().getNationHandler().isPlayerInNation(event.getPlayer(), city.getOwner())) {
                    //Player is not in nation and just clicked point, attempt our interaction.
                    OpenNAW.getInstance().getActionLogger().tryAction(new CityAttackAction(event.getPlayer(), city));
                } else {
                    if(city.underSiege()) {
                        //Under siege, try to defend.
                        OpenNAW.getInstance().getActionLogger().tryAction(new CityDefenseAction(event.getPlayer(), city));
                    }
                }
            }
        }
    }
}
