package engine.gameManager;

import engine.mobileAI.MobAI;
import engine.mobileAI.utilities.CombatUtilities;
import engine.mobileAI.utilities.MovementUtilities;
import engine.objects.*;
import engine.server.MBServerStatics;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;

public class HellgateManager {
    public static ArrayList<Mob> hellgate_mobs;
    public static ArrayList<Mob> hellgate_mini_bosses;
    public static Mob hellgate_boss;

    public static Long hellgate_time_completed = 0L;
    public static final int citadel_ruins_zone_id = 993;
    public static final int hell_portal_1_zone_id = 994;
    public static final int hell_portal_2_zone_id = 996;
    public static final int hell_portal_3_zone_id = 997;
    public static final int hell_portal_4_zone_id = 998;

    public static boolean initialized = false;

    public static final ArrayList<Integer> static_rune_ids_low = new ArrayList<>(Arrays.asList(
            250001, 250002, 250003, 250004, 250005, 250006, 250010, 250011,
            250012, 250013, 250014, 250015, 250019, 250020, 250021, 250022,
            250023, 250024, 250028, 250029, 250030, 250031, 250032, 250033,
            250037, 250038, 250039, 250040, 250041, 250042
    ));

    public static final ArrayList<Integer> static_rune_ids_mid = new ArrayList<>(Arrays.asList(
            250006, 250007, 250008,
            250015, 250016, 250017,
            250024, 250025, 250026,
            250033, 250034, 250035,
            250042, 250043, 250044
    ));

    public static final ArrayList<Integer> static_rune_ids_high = new ArrayList<>(Arrays.asList(
            250007, 250008,
            250016, 250017,
            250025, 250026,
            250034, 250035,
            250043, 250044
    ));
    public static void confiureHellgate(){
        compile_mob_list();
    }
    public static void pulseHellgates(){
        if(!initialized){
            confiureHellgate();
            if(hellgate_boss != null)
                initialized = true;
            return;
        }

        if(hellgate_mobs == null) {
            return;
        }
        if(hellgate_mini_bosses == null) {
            return;
        }
        if(hellgate_boss == null){
            return;
        }

        if(!hellgate_boss.isAlive() && hellgate_time_completed == 0L)
            hellgate_time_completed = System.currentTimeMillis();

        if(hellgate_time_completed != 0L && System.currentTimeMillis() > hellgate_time_completed + MBServerStatics.THIRTY_MINUTES){
            ResetHellgate();
        }
    }

    public static void compile_mob_list(){
        if(hellgate_mobs == null) {
            hellgate_mobs =new ArrayList<>();
        }
        if(hellgate_mini_bosses == null) {
            hellgate_mini_bosses =new ArrayList<>();
        }
        Zone hellgate_zone = ZoneManager.getZoneByUUID(citadel_ruins_zone_id);
        if(hellgate_zone == null)
            return;

        for(Mob mob : hellgate_zone.zoneMobSet){
            switch(mob.getMobBaseID()){
                case 14163: // basic saetor warrior
                    mob.getCharItemManager().clearInventory();
                    SpecialLootHandler(mob,false,false);
                    mob.setResists(new Resists("Elite"));
                    mob.healthMax = 8500;
                    mob.setHealth(mob.healthMax);
                    hellgate_mobs.add(mob);
                    break;
                case 12770: // minotaur mini boss
                    mob.getCharItemManager().clearInventory();
                    SpecialLootHandler(mob,true,false);
                    mob.setResists(new Resists("Elite"));
                    mob.healthMax = 12500;
                    mob.setHealth(mob.healthMax);
                    hellgate_mini_bosses.add(mob);
                    break;
                case 14180: // mordoth, son of morlock
                    mob.getCharItemManager().clearInventory();
                    SpecialLootHandler(mob,false,true);
                    mob.setResists(new Resists("Elite"));
                    mob.healthMax = mob.mobBase.getHealthMax();
                    mob.setHealth(mob.healthMax);
                    hellgate_boss = mob;
                    break;
            }
        }
    }

    public static void ResetHellgate(){
        hellgate_time_completed = 0L;
        for(Mob mob : hellgate_mobs){
            if(!mob.isAlive()){
                if(!mob.despawned){
                    mob.despawn();
                }
                mob.respawn();
            }
            mob.setHealth(mob.healthMax);
            SpecialLootHandler(mob,false,false);
        }
        for(Mob mob : hellgate_mini_bosses){
            if(!mob.isAlive()){
                if(!mob.despawned){
                    mob.despawn();
                }
                mob.respawn();
            }
            mob.setHealth(mob.healthMax);
            SpecialLootHandler(mob,true,false);
        }
        if(!hellgate_boss.isAlive()){
            if(!hellgate_boss.despawned){
                hellgate_boss.despawn();
            }
            hellgate_boss.respawn();
        }
        hellgate_boss.setHealth(hellgate_boss.healthMax);
        SpecialLootHandler(hellgate_boss,false,true);
    }

    public static void SpecialMobAIHandler(Mob mob){

        if(mob.playerAgroMap.isEmpty())
            return;

        if(!mob.isAlive())
            return;

        if(mob.combatTarget == null)
            MobAI.NewAggroMechanic(mob);

        if(MovementUtilities.canMove(mob) &&  mob.combatTarget != null && !CombatUtilities.inRangeToAttack(mob,mob.combatTarget))
            MobAI.chaseTarget(mob);

        if(mob.combatTarget != null)
            MobAI.CheckForAttack(mob);

        if(mob.combatTarget == null && mob.loc.distanceSquared(mob.bindLoc) > 1024) {//32 units
            mob.teleport(mob.bindLoc);
            mob.setCombatTarget(null);
            MovementUtilities.aiMove(mob,mob.bindLoc,true);
        }
    }

    public static void SpecialLootHandler(Mob mob, Boolean commander, Boolean epic){
        mob.getCharItemManager().clearInventory();
        int contractRoll = ThreadLocalRandom.current().nextInt(1,101);
        if(contractRoll <= 25){
            //generate random contract

        }

        int runeRoll = ThreadLocalRandom.current().nextInt(1,101);
        ItemBase runeBase = null;
        int roll;
        int itemId;
        if(runeRoll <= 60 && !commander && !epic) {
            //generate random rune (standard 5-30)
            roll = ThreadLocalRandom.current().nextInt(static_rune_ids_low.size() + 1);
            itemId = static_rune_ids_low.get(0);
            try {
                itemId = static_rune_ids_low.get(roll);
            } catch (Exception e) {

            }
            runeBase = ItemBase.getItemBase(itemId);
            if (runeBase != null) {
                MobLoot rune = new MobLoot(mob, runeBase, true);

                if (rune != null)
                    mob.getCharItemManager().addItemToInventory(rune);
            }
        }
        if(runeRoll <= 50 && commander) {
            //generate random rune (30-40)
            roll = ThreadLocalRandom.current().nextInt(static_rune_ids_mid.size() + 1);
            itemId = static_rune_ids_mid.get(0);
            try {
                itemId = static_rune_ids_mid.get(roll);
            } catch (Exception e) {

            }
            runeBase = ItemBase.getItemBase(itemId);
            if (runeBase != null) {
                MobLoot rune = new MobLoot(mob, runeBase, true);

                if (rune != null)
                    mob.getCharItemManager().addItemToInventory(rune);
            }
        }

        if(runeRoll <= 80 && epic) {
            //generate random rune (35-40)
            roll = ThreadLocalRandom.current().nextInt(static_rune_ids_high.size() + 1);
            itemId = static_rune_ids_high.get(0);
            try {
                itemId = static_rune_ids_high.get(roll);
            } catch (Exception e) {

            }
            runeBase = ItemBase.getItemBase(itemId);
            if (runeBase != null) {
                MobLoot rune = new MobLoot(mob, runeBase, true);

                if (rune != null)
                    mob.getCharItemManager().addItemToInventory(rune);
            }
        }

        if(commander || epic) {
            //handle special case for racial guards

            roll = ThreadLocalRandom.current().nextInt(LootManager.racial_guard_uuids.size() + 1);
            itemId = LootManager.racial_guard_uuids.get(0);
            try {
                itemId = LootManager.racial_guard_uuids.get(roll);
            } catch (Exception e) {

            }
            runeBase = ItemBase.getItemBase(itemId);
            if (runeBase != null) {
                MobLoot rune = new MobLoot(mob, runeBase, true);

                if (rune != null)
                    mob.getCharItemManager().addItemToInventory(rune);
            }
        }

        if(epic){
            //handle glass chance for epic
            int glassRoll = ThreadLocalRandom.current().nextInt(1,101);
            if(glassRoll < 5){
                int glassID = LootManager.rollRandomItem(126);
                ItemBase glassItem = ItemBase.getItemBase(glassID);
                if (glassItem != null) {
                    MobLoot glass = new MobLoot(mob, glassItem, true);

                    if (glass != null)
                        mob.getCharItemManager().addItemToInventory(glass);
                }
            }
        }

        //handle gold drops
        int goldDrop;
        if(!commander && !epic){
            goldDrop = ThreadLocalRandom.current().nextInt(25000);
            mob.getCharItemManager().addGoldToInventory(goldDrop,false);
        }
        if(commander){
            goldDrop = ThreadLocalRandom.current().nextInt(100000,250000);
            mob.getCharItemManager().addGoldToInventory(goldDrop,false);
        }
        if(epic){
            goldDrop = ThreadLocalRandom.current().nextInt(2500000,5000000);
            mob.getCharItemManager().addGoldToInventory(goldDrop,false);
        }
    }
}