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


package engine.db.handlers;

import engine.gameManager.DbManager;
import engine.gameManager.ItemManager;
import engine.mbEnums;
import engine.objects.Contract;
import engine.objects.Item;
import org.pmw.tinylog.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class dbContractHandler extends dbHandlerBase {

    public dbContractHandler() {
        this.localClass = Contract.class;
        this.localObjectType = mbEnums.GameObjectType.valueOf(this.localClass.getSimpleName());
    }

    public Contract GET_CONTRACT(final int objectUUID) {

        Contract contract = (Contract) DbManager.getFromCache(mbEnums.GameObjectType.Contract, objectUUID);

        if (contract != null)
            return contract;

        if (objectUUID == 0)
            return null;

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

            preparedStatement.setInt(1, objectUUID);

            ResultSet rs = preparedStatement.executeQuery();
            contract = (Contract) getObjectFromRs(rs);

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

    public void LOAD_CONTRACT_INVENTORY(final Contract contract) {

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

            preparedStatement.setInt(1, contract.inventorySet);

            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {

                int templateID = rs.getInt("templateID");

                Item item = new Item(templateID);
                item.objectUUID = ItemManager.lastNegativeID.decrementAndGet();
                contract.getSellInventory().add(item);

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

    public void LOAD_SELL_LIST_FOR_CONTRACT(final Contract contract) {

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

            preparedStatement.setInt(1, contract.getObjectUUID());

            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {

                int type = rs.getInt("type");
                int value = rs.getInt("value");

                switch (type) {
                    case 1:
                        contract.getBuyItemType().add(value);
                        break;
                    case 2:
                        contract.getBuySkillToken().add(value);
                        break;
                    case 3:
                        contract.getBuyUnknownToken().add(value);
                        break;
                }
            }
        } catch (SQLException e) {
            Logger.error(e);
        }
    }

    public boolean updateAllowedBuildings(final Contract con) {

        try (Connection connection = DbManager.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `static_npc_contract` SET `slotInBuildings`=? WHERE `contractID`=?")) {

            preparedStatement.setString(1, mbEnums.asString(con.allowedBuildings));
            preparedStatement.setInt(2, con.getContractID());

            return (preparedStatement.executeUpdate() > 0);

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

    public boolean updateDatabase(final Contract con) {

        try (Connection connection = DbManager.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `static_npc_contract` SET `contractID`=?, `name`=?, "
                     + "`mobbaseID`=?, `classID`=?, vendorDialog=?, iconID=?, slotInBuildings=? WHERE `ID`=?")) {

            preparedStatement.setInt(1, con.getContractID());
            preparedStatement.setString(2, con.getName());
            preparedStatement.setInt(3, con.getMobbaseID());
            preparedStatement.setInt(4, con.getClassID());
            preparedStatement.setInt(5, (con.getVendorDialog() != null) ? con.getVendorDialog().getObjectUUID() : 0);
            preparedStatement.setInt(6, con.getIconID());
            preparedStatement.setInt(8, con.getObjectUUID());
            preparedStatement.setString(7, mbEnums.asString(con.allowedBuildings));

            return (preparedStatement.executeUpdate() > 0);

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