package jesse.keeblarcraft.AttributeMgr.AttributeNodes.FactionNodes;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import jesse.keeblarcraft.AttributeMgr.AttributeNodes.AbstractNode;
import jesse.keeblarcraft.Commands.FactionCommands;
import jesse.keeblarcraft.FactionMgr.FactionManager;
import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerInBaseCallback;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;

public class FactionItemRepair extends AbstractNode {
    private Boolean resetTimer = true;
    Timer timer = new Timer();
    Date date = new Date();
    int repairTier = -1;
    int tempValue = 0;
    FactionManager utilFactionManager = new FactionManager();

    @Override
    public String GetNodeTitle() {
        return "faction_item_repair";
    }

    @Override
    public String GetNodeDescription() {
;        return "Repairs item and too durability while a player is inside the faction";
    }

    @Override
    public HashMap<String, List<String>> GetDetails() {
        HashMap<String, List<String>> ret = new HashMap<String, List<String>>();
        ret.put("Faction Item Repair", List.of("Repairs a players items and tools in their durability while they are at the faction home!",
                                                "The repair rate is 1 durability every 5 seconds for each item not at max durability"));
        return ret;
    }

    /*
     * BaseRepair tier 1 speed
     */
    public void RepairInventory(ServerPlayerEntity player) {
        //probably will end up using this List<DefaultedList<ItemStack>> combinedInventory; from PlayerInventory
        //PlayerInventory playerInv = player.getInventory();// main is all your inventory you can see exluding your off hand and armor i think
        switch (utilFactionManager.GetFactionPower(player)) {//should be the faction power
            case 0:
                repairTier = -1;
                break;
            case 1:
                repairTier = -2;
                break;

            case 2:
                repairTier = -3;
                break;
        
            default:
                repairTier = -1;
                break;
        }
        for (ItemStack item : player.getInventory().main) { 
            if (item.isDamageable() && item.isDamaged()) {
                    item.setDamage(item.getDamage() - repairTier);
            }
        }
        for (ItemStack item : player.getInventory().armor) { 
            if (item.isDamageable() && item.isDamaged()) {
                    item.setDamage(item.getDamage() - repairTier);
            }
        }

        if (player.getInventory().offHand.isEmpty()) { return; } 

        if (player.getOffHandStack().isDamaged()) {
            player.getOffHandStack().setDamage(player.getOffHandStack().getDamage() - repairTier);
        }
        return;
    }

    @Override
    public void RegisterCallbacks() {
        PlayerInBaseCallback.EVENT.register((player, world) -> {
            /* DO STUFF  - REMEMBER: This function is called EVERY tick a player is inside the base! That's EVERY tick*/
            
            if (resetTimer) {
                resetTimer = false;
                // Make a new timer object here to count down to reset the 'canRepair object'
                // Reset the 'resetTimer' bool to true IN the timer
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        RepairInventory((ServerPlayerEntity) player);
                        resetTimer = true;
                    }
                }, 15000);//TESTING PURPOSES WILL CAHNGE HIGHER VALUE
            }

            return ActionResult.PASS;
        });
    }
}
