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


package engine.db.handlers;

import engine.gameManager.DbManager;
import engine.gameManager.ZoneManager;
import engine.mbEnums;
import engine.objects.Zone;
import engine.objects.ZoneTemplate;
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;

public class dbZoneHandler extends dbHandlerBase {

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

    public ArrayList<Zone> GET_ALL_ZONES() {

        ArrayList<Zone> zoneList = new ArrayList<>();

        try (Connection connection = DbManager.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_zone`.*, `object`.`parent` FROM `object` INNER JOIN `obj_zone` ON `obj_zone`.`UID` = `object`.`UID` ORDER BY `object`.`UID` ASC;")) {

            ResultSet rs = preparedStatement.executeQuery();
            zoneList = getObjectsFromRs(rs, 2000);

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

        return zoneList;
    }

    public Zone GET_BY_UID(long ID) {

        Zone zone = (Zone) DbManager.getFromCache(mbEnums.GameObjectType.Zone, (int) ID);

        if (zone != null)
            return zone;

        try (Connection connection = DbManager.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_zone`.*, `object`.`parent` FROM `object` INNER JOIN `obj_zone` ON `obj_zone`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;")) {

            preparedStatement.setLong(1, ID);

            ResultSet rs = preparedStatement.executeQuery();
            zone = (Zone) getObjectFromRs(rs);

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

        return zone;
    }

    public void LOAD_ALL_ZONE_TEMPLATES() {

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

            ResultSet rs = preparedStatement.executeQuery();

            while (rs.next()) {
                ZoneTemplate zoneTemplate = new ZoneTemplate(rs);
                ZoneManager._zone_templates.put(zoneTemplate.templateID, zoneTemplate);
            }

            // Add player city

            ZoneTemplate zoneTemplate = new ZoneTemplate();

            zoneTemplate.templateID = 0;
            zoneTemplate.sea_level_type = "PARENT";
            zoneTemplate.sea_level = 0;
            zoneTemplate.max_blend = 128;
            zoneTemplate.min_blend = 128;
            zoneTemplate.terrain_max_y = 5;
            zoneTemplate.major_radius = (int) mbEnums.CityBoundsType.ZONE.halfExtents;
            zoneTemplate.minor_radius = (int) mbEnums.CityBoundsType.ZONE.halfExtents;
            zoneTemplate.terrain_type = "PLANAR";

            ZoneManager._zone_templates.put(zoneTemplate.templateID, zoneTemplate);

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

    }

    public boolean DELETE_ZONE(final Zone zone) {

        try (Connection connection = DbManager.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `object` WHERE `UID` = ? AND `type` = 'zone'")) {

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

            return (preparedStatement.executeUpdate() > 0);

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

        return false;
    }

}
