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


package engine.db.handlers;

import engine.gameManager.DbManager;
import engine.objects.ItemBase;
import org.pmw.tinylog.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class dbItemBaseHandler extends dbHandlerBase {

    public dbItemBaseHandler() {

    }

    public void LOAD_BAKEDINSTATS(ItemBase itemBase) {

        int itemBaseID = itemBase.getUUID();
        switch(itemBaseID){
            case 27550://bow
            case 27560://dagger
            case 27570://hammer
            case 27580://axe
            case 27590://sword
                //return new String[]{"PRE-010","SUF-260"};
                itemBaseID =  8000000;
                break;
            case 27600://staff
                //return new String[]{"PRE-334","PRE-315"};
                itemBaseID =  8000010;
                break;

            case 188500://HA chest
            case 188510://HA arms
            case 188520://HA legs
            case 188530://HA gloves
            case 188550://HA helm
            case 188720://CC hood
            case 188900://MA chest
            case 188910://MA Sleeves
            case 188920://MA Legs
            case 188930://MA gloves
            case 188950://MA helm
            case 189100://la chest
            case 189110://la arms
            case 189120://la legs
            case 189130://la gloves
            case 189150://la helm
            case 189550://CC gloves
                //return new String[]{"PRE-130", "PRE-232", "PRE-212", "PRE-222", "SUF-007"};
                itemBaseID = 8000020;
                break;

            case 188540://HA boots
            case 188940://MA boots
            case 189140://LA boots
            case 189560://CC boots
                //return new String[]{"PRE-133", "PRE-230", "PRE-210", "PRE-220", "SUF-003","SUF-150"};
                itemBaseID = 8000030;
                break;

            case 188700://CC robe
                //return new String[]{"PRE-130", "PRE-232", "PRE-212", "PRE-222","SUF-317","SUF-317","SUF-317"};
                itemBaseID = 8000040;
                break;

            case 189500://MA shield
            case 189510://HA shield
                //return new String[]{"PRE-125","PRE-125","PRE-125", "PRE-230", "PRE-210", "PRE-220"};
                itemBaseID = 8000050;
        }



        try (Connection connection = DbManager.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_item_bakedinstat` WHERE `itemID` = ?")) {

            preparedStatement.setInt(1, itemBaseID);
            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {
                if (rs.getBoolean("fromUse"))
                    itemBase.getUsedStats().put(rs.getInt("token"), rs.getInt("numTrains"));
                else
                    itemBase.getBakedInStats().put(rs.getInt("token"), rs.getInt("numTrains"));
            }
        } catch (SQLException e) {
            Logger.error(e);
        }
    }

    public void LOAD_ANIMATIONS(ItemBase itemBase) {

        ArrayList<Integer> tempList = new ArrayList<>();
        ArrayList<Integer> tempListOff = new ArrayList<>();

        try (Connection connection = DbManager.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_itembase_animations` WHERE `itemBaseUUID` = ?")) {

            preparedStatement.setInt(1, itemBase.getUUID());
            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {
                int animation = rs.getInt("animation");
                boolean rightHand = rs.getBoolean("rightHand");

                if (rightHand)
                    tempList.add(animation);
                else
                    tempListOff.add(animation);
            }
        } catch (SQLException e) {
            Logger.error(e);
        }

        itemBase.setAnimations(tempList);
        itemBase.setOffHandAnimations(tempListOff);
    }

    public void LOAD_ALL_ITEMBASES() {

        ItemBase itemBase;
        int recordsRead = 0;

        try (Connection connection = DbManager.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_itembase")) {

            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {
                recordsRead++;
                itemBase = new ItemBase(rs);
                ItemBase.addToCache(itemBase);

                //copy the vampire shrine for saetor and change uuid and name
                if(itemBase.getName().contains("Vampire Shrine")){
                    ItemBase saetorShrine = new ItemBase(rs);
                    saetorShrine.setUUID(1035);
                    saetorShrine.setName(saetorShrine.getName().replace("Vampire","Saetor"));
                    ItemBase.addToCache(saetorShrine);
                }
            }

        } catch (SQLException e) {
            Logger.error(e);
        }
        Logger.info("read: " + recordsRead + " cached: " + ItemBase.getUUIDCache().size());
    }

    public HashMap<Integer, ArrayList<Integer>> LOAD_RUNES_FOR_NPC_AND_MOBS() {

        HashMap<Integer, ArrayList<Integer>> runeSets = new HashMap<>();
        int runeSetID;
        int runeBaseID;
        int recordsRead = 0;

        try (Connection connection = DbManager.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_npc_runeSet")) {

            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {

                recordsRead++;

                runeSetID = rs.getInt("runeSet");
                runeBaseID = rs.getInt("runeBase");

                if (runeSets.get(runeSetID) == null) {
                    ArrayList<Integer> runeList = new ArrayList<>();
                    runeList.add(runeBaseID);
                    runeSets.put(runeSetID, runeList);
                } else {
                    ArrayList<Integer> runeList = runeSets.get(runeSetID);
                    runeList.add(runeSetID);
                    runeSets.put(runeSetID, runeList);
                }
            }

        } catch (SQLException e) {
            Logger.error(e);
            return runeSets;
        }

        Logger.info("read: " + recordsRead + " cached: " + runeSets.size());
        return runeSets;
    }

}
