package engine.workthreads;

import engine.Enum;
import engine.InterestManagement.WorldGrid;
import engine.db.archive.DataWarehouse;
import engine.db.archive.MineRecord;
import engine.gameManager.BuildingManager;
import engine.gameManager.ChatManager;
import engine.net.DispatchMessage;
import engine.net.client.msg.chat.ChatSystemMsg;
import engine.objects.Building;
import engine.objects.Guild;
import engine.objects.Mine;
import engine.objects.PlayerCharacter;
import org.pmw.tinylog.Logger;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class MineThread  implements Runnable {
    public MineThread(){

    }
    @Override
    public void run() {
        LocalDateTime nextPulse = LocalDateTime.now().withMinute(59).minusHours(1);
        while (true) {
            if(LocalDateTime.now().isAfter(nextPulse));
            processMineWindows();
            nextPulse = nextPulse.plusMinutes(30);
        }
    }

    public static void mineWindowOpen(Mine mine) {

        mine.setActive(true);
        ChatManager.chatSystemChannel(mine.getParentZone().getName() + "'s Mine is now Active!");
        Logger.info(mine.getParentZone().getName() + "'s Mine is now Active!");
    }

    public static boolean mineWindowClose(Mine mine) {

        // No need to end the window of a mine which never opened.

        if (mine.isActive == false)
            return false;

        Building mineBuilding = BuildingManager.getBuildingFromCache(mine.getBuildingID());

        if (mineBuilding == null) {
            Logger.debug("Null mine building for Mine " + mine.getObjectUUID() + " Building " + mine.getBuildingID());
            return false;
        }

        for(Integer id : mine._playerMemory ){
            PlayerCharacter.getPlayerCharacter(id).ZergMultiplier = 1.0f;
        }
        for(Integer id : mine._recentMemory.keySet()){
            PlayerCharacter.getPlayerCharacter(id).ZergMultiplier = 1.0f;
        }
        // Mine building still stands; nothing to do.
        // We can early exit here.

        if (mineBuilding.getRank() > 0) {
            mine.setActive(false);
            mine.lastClaimer = null;
            return true;
        }

        // This mine does not have a valid claimer
        // we will therefore set it to errant
        // and keep the window open.

        if (!Mine.validateClaimer(mine.lastClaimer)) {
            mine.lastClaimer = null;
            mine.updateGuildOwner(null);
            mine.setActive(true);
            return false;
        }

        //Update ownership to map

        mine.guildName = mine.getOwningGuild().getName();
        mine.guildTag = mine.getOwningGuild().getGuildTag();
        Guild nation = mine.getOwningGuild().getNation();
        mine.nationName = nation.getName();
        mine.nationTag = nation.getGuildTag();

        mineBuilding.rebuildMine(mine.capSize * 5000);
        WorldGrid.updateObject(mineBuilding);

        ChatSystemMsg chatMsg = new ChatSystemMsg(null, mine.lastClaimer.getName() + " has claimed the mine in " + mine.getParentZone().getName() + " for " + mine.getOwningGuild().getName() + ". The mine is no longer active.");
        chatMsg.setMessageType(10);
        chatMsg.setChannel(Enum.ChatChannelType.SYSTEM.getChannelID());
        DispatchMessage.dispatchMsgToAll(chatMsg);

        // Warehouse this claim event

        MineRecord mineRecord = MineRecord.borrow(mine, mine.lastClaimer, Enum.RecordEventType.CAPTURE);
        DataWarehouse.pushToWarehouse(mineRecord);

        mineBuilding.setRank(mineBuilding.getRank());
        mine.lastClaimer = null;
        mine.setActive(false);
        mine.wasClaimed = true;
        return true;
    }

    public static void processMineWindows(){
        for (Mine mine : Mine.getMines()) {
            Building tower = BuildingManager.getBuildingFromCache(mine.getBuildingID());
            if(tower == null)
                continue;
            LocalDateTime liveTime = LocalDateTime.now().withHour(mine.liveHour).withMinute(mine.liveMinute).minusMinutes(1);
            ZonedDateTime localizedTime = convertCSTToLocalTime(liveTime);
            if(!mine.isActive && !mine.wasOpened) {
                if (ZonedDateTime.now().isAfter(localizedTime)) {
                    mineWindowOpen(mine);
                    // mine has not opened today yet, and it is now after the time it should have, open the mine
                }
            }else{
                //mine is active right now
                if (ZonedDateTime.now().isBefore(localizedTime.plusMinutes(30))){
                    continue; // window is not over yet
                }

                if(tower.getRank() == 1){
                    //mine was not knocked down, close the window
                    mineWindowClose(mine);
                }else{
                    if(mine.lastClaimer != null)
                        mineWindowClose(mine); //has a claimer, close the window
                }
            }
        }
    }
    public static ZonedDateTime convertCSTToLocalTime(LocalDateTime cstDateTime) {
        // Define the CST time zone (Central Standard Time)
        ZoneId cstZoneId = ZoneId.of("America/Chicago"); // CST is represented by America/Chicago
        // Define the system's default time zone
        ZoneId localZoneId = ZoneId.systemDefault();

        // Create a ZonedDateTime object for CST
        ZonedDateTime cstZonedDateTime = ZonedDateTime.of(cstDateTime, cstZoneId);

        // Convert CST to the local time zone
        ZonedDateTime localZonedDateTime = cstZonedDateTime.withZoneSameInstant(localZoneId);

        return localZonedDateTime;
    }
}