// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2022
//                www.magicbane.com

package engine.gameManager;

import engine.Enum;
import engine.loot.*;
import engine.net.DispatchMessage;
import engine.net.client.msg.ErrorPopupMsg;
import engine.net.client.msg.chat.ChatSystemMsg;
import engine.objects.*;
import org.pmw.tinylog.Logger;

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

/**
 * Class contains static methods for data from Magicbane's loot tables
 */
public enum LootManager {

    LOOTMANAGER;

    // Newer tables

    public static HashMap<Integer, ArrayList<GenTableEntry>> _genTables = new HashMap<>();
    public static HashMap<Integer, ArrayList<ItemTableEntry>> _itemTables = new HashMap<>();
    public static HashMap<Integer, ArrayList<ModTableEntry>> _modTables = new HashMap<>();
    public static HashMap<Integer, ArrayList<ModTypeTableEntry>> _modTypeTables = new HashMap<>();

    public static ArrayList<Integer> vorg_ha_uuids = new ArrayList<>(Arrays.asList(new Integer[]{27580, 27590, 188500, 188510, 188520, 188530, 188540, 188550, 189510}));
    public static ArrayList<Integer> vorg_ma_uuids = new ArrayList<>(Arrays.asList(new Integer[]{27570,188900,188910,188920,188930,188940,188950,189500}));
    public static ArrayList<Integer> vorg_la_uuids = new ArrayList<>(Arrays.asList(new Integer[]{27550,27560,189100,189110,189120,189130,189140,189150}));
    public static ArrayList<Integer> vorg_cloth_uuids = new ArrayList<>(Arrays.asList(new Integer[]{27600,188700,188720,189550,189560}));

    // Drop Rates

    public static float NORMAL_DROP_RATE;
    public static float NORMAL_EXP_RATE;
    public static float NORMAL_GOLD_RATE;
    public static float HOTZONE_DROP_RATE;
    public static float HOTZONE_EXP_RATE;
    public static float HOTZONE_GOLD_RATE;
    public static HashMap<Integer, ArrayList<BootySetEntry>> _bootySetMap = new HashMap<>();

    // Bootstrap routine to initialize the Loot Manager

    public static void init() {

        // Load loot tables from database.

        _genTables = DbManager.LootQueries.LOAD_GEN_ITEM_TABLES();
        _itemTables = DbManager.LootQueries.LOAD_ITEM_TABLES();
        _modTables = DbManager.LootQueries.LOAD_MOD_TABLES();
        _modTypeTables = DbManager.LootQueries.LOAD_MOD_TYPE_TABLES();

        // Cache drop rate values from Config manager.

        NORMAL_DROP_RATE = Float.parseFloat(ConfigManager.MB_NORMAL_DROP_RATE.getValue());
        NORMAL_EXP_RATE = Float.parseFloat(ConfigManager.MB_NORMAL_EXP_RATE.getValue());
        NORMAL_GOLD_RATE = Float.parseFloat(ConfigManager.MB_NORMAL_GOLD_RATE.getValue());
        HOTZONE_DROP_RATE = Float.parseFloat(ConfigManager.MB_HOTZONE_DROP_RATE.getValue());
        HOTZONE_EXP_RATE = Float.parseFloat(ConfigManager.MB_HOTZONE_EXP_RATE.getValue());
        HOTZONE_GOLD_RATE = Float.parseFloat(ConfigManager.MB_HOTZONE_GOLD_RATE.getValue());

    }

    public static void GenerateMobLoot(Mob mob) {
        int mobBootySet = mob.getMobBase().bootySet;
        if (mobBootySet != 0) {
            RunBootySetIfPresent(mobBootySet, mob);
        }

        mobBootySet = mob.bootySet;
        if (mobBootySet != 0) {
            RunBootySetIfPresent(mobBootySet, mob);
        }

        // Check mob's inventory for godly or disc runes to send a server announcement
        for (Item item : mob.getInventory()) {
            ItemBase itemBase = item.getItemBase();
            if (itemBase != null && itemBase.getName().toLowerCase().contains("of the gods")) {
                ChatSystemMsg chatMsg = new ChatSystemMsg(
                        null,
                        mob.getName() + " in " + mob.getParentZone().getName() + " has found the " + itemBase.getName() + ". Are you tough enough to take it?"
                );
                chatMsg.setMessageType(10);
                chatMsg.setChannel(Enum.ChatChannelType.SYSTEM.getChannelID());
                DispatchMessage.dispatchMsgToAll(chatMsg);
            }
        }
    }

    private static void RunBootySet(ArrayList<BootySetEntry> entries, Mob mob) {
        float dropRate = NORMAL_DROP_RATE;
        boolean isSafeZone = mob.parentZone.getSafeZone() == 0;

        if (isSafeZone) {
            GenerateLootDrop(mob, 1300);
            if (ThreadLocalRandom.current().nextInt(1, 10000) == 5000) {
                MobLoot extraLoot = rollForGlass(mob);
                if (extraLoot != null) {
                    mob.getCharItemManager().addItemToInventory(extraLoot);
                }
            }
        }

        boolean hasExtraRolled = false;
        for (BootySetEntry bse : entries) {
            switch (bse.bootyType) {
                case "GOLD":
                    GenerateGoldDrop(mob, bse);
                    break;
                case "LOOT":
                    if (ThreadLocalRandom.current().nextInt(1, 101) < (bse.dropChance * dropRate)) {
                        GenerateLootDrop(mob, bse.genTable);
                    }
                    if (isSafeZone && !hasExtraRolled && ThreadLocalRandom.current().nextInt(1, 5000) < 15 * dropRate) {
                        MobLoot extraLoot = (ThreadLocalRandom.current().nextInt(1, 101) <= 50)
                                ? rollForContract(bse.genTable, mob)
                                : rollForRune(bse.genTable, mob);
                        if (extraLoot != null) {
                            mob.getCharItemManager().addItemToInventory(extraLoot);
                        }
                        hasExtraRolled = true;
                    }
                    break;
                case "ITEM":
                    GenerateInventoryDrop(mob, bse);
                    break;
            }
        }

        MobLoot specialDrop = getSpecialDropForMob(mob);
        if (specialDrop != null) {
            enhanceMob(mob, specialDrop);
        }
    }

    private static void RunBootySetIfPresent(int bootySet, Mob mob) {
        if (_bootySetMap.containsKey(bootySet)) {
            RunBootySet(_bootySetMap.get(bootySet), mob);
        }
    }

    private static MobLoot getSpecialDropForMob(Mob mob) {
        MobLoot specialDrop = null;
        int mobUUID = mob.getObjectUUID();
        switch (mobUUID) {
            case 22595:
                specialDrop = createSpecialDrop(mob, 252134, "Melandrach The Blood-Mage");
                break;
            case 22432:
                specialDrop = createSpecialDrop(mob, 252135, "Kyrtaar The Blood-Mage");
                break;
            case 22537:
                specialDrop = createSpecialDrop(mob, 252136, "Vamir The Blood-Mage");
                break;
            case 16387:
                specialDrop = createSpecialDrop(mob, 252129, "Alatar The Blood-Mage");
                break;
            case 32724:
                specialDrop = createSpecialDrop(mob, 252130, "Elphaba The Blood-Mage");
                break;
            case 23379:
                specialDrop = createSpecialDrop(mob, 252131, "Bavmorda The Blood-Mage");
                break;
            case 10826:
                specialDrop = createSpecialDrop(mob, 252132, "Draco The Blood-Mage");
                break;
            case 15929:
                specialDrop = createSpecialDrop(mob, 252133, "Atlantes The Blood-Mage");
                break;
        }
        return specialDrop;
    }

    private static MobLoot createSpecialDrop(Mob mob, int itemBaseId, String name) {
        mob.setFirstName(name);
        return new MobLoot(mob, ItemBase.getItemBase(itemBaseId), true);
    }

    private static void enhanceMob(Mob mob, MobLoot specialDrop) {
        mob.setLevel((short) 65);
        mob.setSpawnTime(10800);
        mob.healthMax = 7500;
        mob.setHealth(7500);
        ChatSystemMsg chatMsg = new ChatSystemMsg(null, mob.getName() + " in " + mob.getParentZone().getName() + " has found the " + specialDrop.getName() + ". Are you tough enough to take it?");
        chatMsg.setMessageType(10);
        chatMsg.setChannel(Enum.ChatChannelType.SYSTEM.getChannelID());
        DispatchMessage.dispatchMsgToAll(chatMsg);
        mob.getCharItemManager().addItemToInventory(specialDrop);
        mob.setResists(new Resists("Dropper"));
    }

    public static MobLoot getGenTableItem(int genTableID, AbstractCharacter mob) {
        if (mob == null || !_genTables.containsKey(genTableID)) {
            return null;
        }

        int genRoll = ThreadLocalRandom.current().nextInt(1, 95);
        GenTableEntry selectedRow = GenTableEntry.rollTable(genTableID, genRoll, 1.0f);

        if (selectedRow == null || !_itemTables.containsKey(selectedRow.itemTableID)) {
            return null;
        }

        int itemTableRoll = (mob.getObjectType().ordinal() == 52) ?
                ThreadLocalRandom.current().nextInt(1, 321) :
                TableRoll(mob.level);

        ItemTableEntry tableRow = ItemTableEntry.rollTable(selectedRow.itemTableID, itemTableRoll);

        if (tableRow == null || tableRow.cacheID == 0) {
            return null;
        }

        ItemBase itemBase = ItemBase.getItemBase(tableRow.cacheID);
        if (itemBase == null) {
            return null;
        }

        if (itemBase.getType() == Enum.ItemType.RESOURCE) {
            if (ThreadLocalRandom.current().nextInt(1, 101) > 10) {
                return null;
            }
            int amount = ThreadLocalRandom.current().nextInt((int) (tableRow.minSpawn * 0.5f),
                    (int) ((tableRow.maxSpawn + 1) * 0.5f));
            return new MobLoot(mob, itemBase, amount, false);
        }

        MobLoot outItem = new MobLoot(mob, itemBase, false);
        try {
            if (selectedRow.pModTable != 0) {
                outItem = GeneratePrefix(mob, outItem, genTableID, genRoll);
                outItem.setIsID(false);
            }
            if (selectedRow.sModTable != 0) {
                outItem = GenerateSuffix(mob, outItem, genTableID, genRoll);
                outItem.setIsID(false);
            }
        } catch (Exception e) {
            Logger.error("Failed to generate mod for item: " + outItem.getName(), e);
        }

        return outItem;
    }

    private static MobLoot GeneratePrefix(AbstractCharacter mob, MobLoot inItem, int genTableID, int genRoll) {
        GenTableEntry selectedRow = GenTableEntry.rollTable(genTableID, genRoll, 1.0f);
        if (selectedRow == null) {
            return inItem;
        }

        ModTypeTableEntry prefixTable = ModTypeTableEntry.rollTable(selectedRow.pModTable, ThreadLocalRandom.current().nextInt(1, 101));
        if (prefixTable == null) {
            return inItem;
        }

        int prefixTableRoll = (mob.getObjectType().ordinal() == 52) ?
                ThreadLocalRandom.current().nextInt(1, 321) :
                TableRoll(mob.level);

        ModTableEntry prefixMod = ModTableEntry.rollTable(prefixTable.modTableID, prefixTableRoll);
        if (prefixMod == null) {
            return inItem;
        }

        if (!prefixMod.action.isEmpty()) {
            inItem.setPrefix(prefixMod.action);
            inItem.addPermanentEnchantment(prefixMod.action, 0, prefixMod.level, true);
        }

        return inItem;
    }

    private static MobLoot GenerateSuffix(AbstractCharacter mob, MobLoot inItem, int genTableID, int genRoll) {
        GenTableEntry selectedRow = GenTableEntry.rollTable(genTableID, genRoll, 1.0f);
        if (selectedRow == null) {
            return inItem;
        }

        ModTypeTableEntry suffixTable = ModTypeTableEntry.rollTable(selectedRow.sModTable, ThreadLocalRandom.current().nextInt(1, 101));
        if (suffixTable == null) {
            return inItem;
        }

        int suffixTableRoll = (mob.getObjectType().ordinal() == 52) ?
                ThreadLocalRandom.current().nextInt(1, 321) :
                TableRoll(mob.level);

        ModTableEntry suffixMod = ModTableEntry.rollTable(suffixTable.modTableID, suffixTableRoll);
        if (suffixMod == null) {
            return inItem;
        }

        if (!suffixMod.action.isEmpty()) {
            inItem.setSuffix(suffixMod.action);
            inItem.addPermanentEnchantment(suffixMod.action, 0, suffixMod.level, false);
        }

        return inItem;
    }

    public static int TableRoll(int mobLevel) {
        mobLevel = Math.min(mobLevel, 65);

        int max = Math.min((int) (4.882 * mobLevel + 127.0), 319);
        int min = Math.max((int) (4.469 * mobLevel - 3.469), 70);

        return ThreadLocalRandom.current().nextInt(min, max + 1);
    }

    public static void GenerateGoldDrop(Mob mob, BootySetEntry bse) {
        int chanceRoll = ThreadLocalRandom.current().nextInt(1, 101); // Simplified the chance roll to be out of 100 directly

        // Early exit if failed to hit minimum chance roll
        if (chanceRoll > bse.dropChance) {
            return;
        }

        // Determine and add gold to mob inventory
        int high = (int) (bse.highGold * NORMAL_GOLD_RATE);
        int low = (int) (bse.lowGold * NORMAL_GOLD_RATE);
        int gold = ThreadLocalRandom.current().nextInt(low, high);

        if (gold > 0) {
            MobLoot goldAmount = new MobLoot(mob, gold);
            mob.getCharItemManager().addItemToInventory(goldAmount);
        }
    }

    public static void GenerateLootDrop(Mob mob, int tableID) {
        if (mob.parentZone.getSafeZone() == 1) {
            return; // Exit early if in a safe zone
        }

        try {
            MobLoot toAdd = getGenTableItem(tableID, mob);
            if (toAdd != null && !isContractOrRune(toAdd)) {
                toAdd.setIsID(true);
                mob.getCharItemManager().addItemToInventory(toAdd);
            }
        } catch (Exception e) {
            // Handle exceptions if necessary
        }
    }

    private static boolean isContractOrRune(MobLoot loot) {
        Enum.ItemType type = loot.getItemBase().getType();
        return type == Enum.ItemType.CONTRACT || type == Enum.ItemType.RUNE;
    }

    public static void GenerateEquipmentDrop(Mob mob) {
        if (mob.parentZone.getSafeZone() == 1) {
            return;
        }

        int dropCount = 0;
        if (mob.getEquip() != null) {
            for (MobEquipment me : mob.getEquip().values()) {
                if (me.getDropChance() == 0) {
                    continue;
                }

                float equipmentRoll = ThreadLocalRandom.current().nextInt(1, 101); // Simplified the roll to be out of 100 directly
                float dropChance = me.getDropChance() * 100;

                if (equipmentRoll > dropChance) {
                    continue;
                }

                ItemBase genericIB = me.getItemBase();
                if (genericIB.isVorg()) {
                    genericIB = getRandomVorgPiece(genericIB); // Replaced separate method calls with a single one
                    mob.spawnTime = ThreadLocalRandom.current().nextInt(300, 2700);
                }

                MobLoot ml = new MobLoot(mob, genericIB, false);

                if (ml != null && dropCount < 1) {
                    ml.setIsID(true);
                    ml.setDurabilityCurrent((short) (ml.getDurabilityCurrent() - ThreadLocalRandom.current().nextInt(5) + 1));
                    mob.getCharItemManager().addItemToInventory(ml);
                    dropCount++;
                }

                if (ml != null && genericIB.isVorg() && mob.getMobBaseID() != 14062) {
                    ml.setIsID(true);
                    ml.setDurabilityCurrent(ml.getDurabilityMax());
                    mob.getCharItemManager().addItemToInventory(ml);
                }
            }
        }
    }

    private static ItemBase getRandomVorgPiece(ItemBase genericIB) {
        if (genericIB.isClothArmor()) {
            return getRandomVorgCloth();
        } else if (genericIB.isHeavyArmor()) {
            return getRandomVorgHA();
        } else if (genericIB.isMediumArmor()) {
            return getRandomVorgMA();
        } else if (genericIB.isLightArmor()) {
            return getRandomVorgLA();
        }
        return genericIB; // Return the original item base if it's not a vorg piece
    }

    public static void GenerateInventoryDrop(Mob mob, BootySetEntry bse) {
        // Check if the item is a discipline rune
        if (ItemBase.getItemBase(bse.itemBase).isDiscRune()) {
            if (!Mob.disciplineDroppers.contains(mob)) {
                Mob.disciplineDroppers.add(mob);
                mob.setResists(new Resists("Dropper"));
                ChatSystemMsg chatMsg = new ChatSystemMsg(null, mob.getName() + " in " + mob.getParentZone().getName() + " has found the " + ItemBase.getItemBase(bse.itemBase).getName() + ". Are you tough enough to take it?");
                chatMsg.setMessageType(10);
                chatMsg.setChannel(Enum.ChatChannelType.SYSTEM.getChannelID());
                DispatchMessage.dispatchMsgToAll(chatMsg);
            }
        }

        // Skip drop if mob is in a safe zone
        if (mob.parentZone.getSafeZone() == 1) {
            return;
        }

        // Roll for drop chance
        int chanceRoll = ThreadLocalRandom.current().nextInt(1, 100); // Changed upper bound to 100

        // Check if the chance roll exceeds drop chance
        if (chanceRoll > bse.dropChance) {
            return; // Early exit if the drop chance fails
        }

        // Create and add the loot item to the mob's inventory
        MobLoot lootItem = new MobLoot(mob, ItemBase.getItemBase(bse.itemBase), true);
        if (lootItem != null) {
            mob.getCharItemManager().addItemToInventory(lootItem);
        }
    }

    public static void peddleFate(PlayerCharacter playerCharacter, Item gift) {

        //get table ID for the itembase ID

        int tableID = 0;

        if (_bootySetMap.get(gift.getItemBaseID()) != null)
            tableID = _bootySetMap.get(gift.getItemBaseID()).get(ThreadLocalRandom.current().nextInt(_bootySetMap.get(gift.getItemBaseID()).size())).genTable;

        if (tableID == 0)
            return;

        //get the character item manager

        CharacterItemManager itemMan = playerCharacter.getCharItemManager();

        if (itemMan == null)
            return;

        //check if player owns the gift he is trying to open

        if (itemMan.doesCharOwnThisItem(gift.getObjectUUID()) == false)
            return;

        //roll 1-100 for the gen table selection

        int genRoll = ThreadLocalRandom.current().nextInt(1, 100 + 1);
        GenTableEntry selectedRow = GenTableEntry.rollTable(tableID, genRoll, LootManager.NORMAL_DROP_RATE);

        if(selectedRow == null)
            return;

        //roll 220-320 for the item table selection

        int itemRoll = ThreadLocalRandom.current().nextInt(220, 320 + 1);
        ItemTableEntry selectedItem = ItemTableEntry.rollTable(selectedRow.itemTableID, itemRoll);

        if (selectedItem == null)
            return;

        //create the item from the table, quantity is always 1

        MobLoot winnings = new MobLoot(playerCharacter, ItemBase.getItemBase(selectedItem.cacheID), 1, false);

        if (winnings == null)
            return;

        //early exit if the inventory of the player will not old the item

        if (itemMan.hasRoomInventory(winnings.getItemBase().getWeight()) == false) {
            ErrorPopupMsg.sendErrorPopup(playerCharacter, 21);
            return;
        }

        //determine if the winning item needs a prefix

        if(selectedRow.pModTable != 0){
            int prefixRoll = ThreadLocalRandom.current().nextInt(220,320 + 1);
            ModTableEntry prefix = ModTableEntry.rollTable(selectedRow.pModTable, prefixRoll);
            if(prefix != null)
                winnings.addPermanentEnchantment(prefix.action, 0, prefix.level, true);
        }

        //determine if the winning item needs a suffix

        if(selectedRow.sModTable != 0){
            int suffixRoll = ThreadLocalRandom.current().nextInt(220,320 + 1);
            ModTableEntry suffix = ModTableEntry.rollTable(selectedRow.sModTable, suffixRoll);
            if (suffix != null)
                winnings.addPermanentEnchantment(suffix.action, 0, suffix.level, true);
        }
        winnings.setIsID(true);

        //remove gift from inventory

        itemMan.consume(gift);

        //add winnings to player inventory

        Item playerWinnings = winnings.promoteToItem(playerCharacter);
        itemMan.addItemToInventory(playerWinnings);
        itemMan.updateInventory();
    }

    public static MobLoot rollForContract(int table, Mob mob) {
        int roll = (table == 1900 || table == 1500) ? 73 : 99;
        GenTableEntry selectedRow = GenTableEntry.rollTable(table, roll, 1.0f);
        if (selectedRow == null) {
            return null;
        }

        int itemTableId = selectedRow.itemTableID;
        if (!_itemTables.containsKey(itemTableId)) {
            return null;
        }

        ItemTableEntry tableRow = ItemTableEntry.rollTable(itemTableId, ThreadLocalRandom.current().nextInt(1, 321));
        if (tableRow == null || tableRow.cacheID == 0) {
            return null;
        }

        return new MobLoot(mob, ItemBase.getItemBase(tableRow.cacheID), false);
    }

    public static MobLoot rollForRune(int table, Mob mob) {
        int roll = (table == 1900 || table == 1500) ? 77 : 97;
        GenTableEntry selectedRow = GenTableEntry.rollTable(table, roll, 1.0f);
        if (selectedRow == null) {
            return null;
        }

        int itemTableId = selectedRow.itemTableID;
        if (!_itemTables.containsKey(itemTableId)) {
            return null;
        }

        ItemTableEntry tableRow = ItemTableEntry.rollTable(itemTableId, ThreadLocalRandom.current().nextInt(1, 321));
        if (tableRow == null || tableRow.cacheID == 0) {
            return null;
        }

        return new MobLoot(mob, ItemBase.getItemBase(tableRow.cacheID), false);
    }

    public static MobLoot rollForGlass(Mob mob) {
        ItemTableEntry tableRow = ItemTableEntry.rollTable(126, ThreadLocalRandom.current().nextInt(1, 321));
        if (tableRow != null && tableRow.cacheID != 0) {
            return new MobLoot(mob, ItemBase.getItemBase(tableRow.cacheID), false);
        }
        return null;
    }

    public static ItemBase getRandomVorgItem(int maxRandom, int... itemBaseIDs) {
        int random = ThreadLocalRandom.current().nextInt(maxRandom);
        int index = random / (maxRandom / itemBaseIDs.length);
        if (index >= itemBaseIDs.length) {
            index = itemBaseIDs.length - 1;
        }
        return ItemBase.getItemBase(itemBaseIDs[index]);
    }
    public static ItemBase getRandomVorgCloth() {
        return getRandomVorgItem(100, 27600, 188700, 188720, 189550, 189560);
    }

    public static ItemBase getRandomVorgLA() {
        return getRandomVorgItem(160, 27550, 27560, 189100, 189110, 189120, 189130, 189140, 189150);
    }

    public static ItemBase getRandomVorgMA() {
        return getRandomVorgItem(160, 27570, 188900, 188910, 188920, 188930, 188940, 188950, 189500);
    }

    public static ItemBase getRandomVorgHA() {
        return getRandomVorgItem(180, 27580, 27590, 188500, 188510, 188520, 188530, 188540, 188550, 189510);
    }
}
