package engine.objects;

import engine.mbEnums;
import engine.gameManager.DbManager;
import engine.net.ByteBufferWriter;

import java.util.ArrayList;
import java.util.HashMap;

/* Runegates are tied to particular buildings at
 * bootstrap derived from the Portal creation.
 * This is only to enable the toggling of effect
 * bits when traveler is used.
 */

public class Runegate {

    // Runegate class Instance variables
    public static HashMap<Integer, Runegate> _runegates = new HashMap<>();

    public Portal[] _portals;
    public Building gateBuilding;

    private Runegate(Building gateBuilding) {

        this._portals = new Portal[8];
        this.gateBuilding = gateBuilding;

        // Load portals for this runegate portals from the database

        configurePortals();

        // Chaos, Khar and Oblivion are on by default

        _portals[mbEnums.PortalType.CHAOS.ordinal()].activate(false);
        _portals[mbEnums.PortalType.OBLIV.ordinal()].activate(false);
        _portals[mbEnums.PortalType.MERCHANT.ordinal()].activate(false);

    }

    public static void loadAllRunegates() {

        ArrayList<Integer> gateList;

        gateList = DbManager.RunegateQueries.GET_RUNEGATE_LIST();

        for (int gateID : gateList) {

            Building gateBuilding = (Building) DbManager.getObject(mbEnums.GameObjectType.Building, gateID);

            Runegate runegate = new Runegate(gateBuilding);
            _runegates.put(gateID, runegate);
        }

    }

    public static ArrayList<String> GetAllOpenGateIDStrings() {
        ArrayList<String> openGateIDStrings = new ArrayList<>();

        openGateIDStrings.add("TRA-003");
        openGateIDStrings.add("TRA-004");
        openGateIDStrings.add("TRA-005");
        openGateIDStrings.add("TRA-006");
        openGateIDStrings.add("TRA-007");
        openGateIDStrings.add("TRA-008");
        openGateIDStrings.add("TRA-009");
        openGateIDStrings.add("TRA-010");
        return openGateIDStrings;
    }

    public void activatePortal(mbEnums.PortalType portalType) {

        this._portals[portalType.ordinal()].activate(true);

    }

    public void deactivatePortal(mbEnums.PortalType portalType) {

        this._portals[portalType.ordinal()].deactivate();

    }

    public Portal[] getPortals() {

        return this._portals;

    }

    public void collidePortals() {

        for (Portal portal : this.getPortals()) {

            if (portal.isActive())
                portal.collide();
        }
    }

    public void configurePortals() {

        ArrayList<Portal> portalList = DbManager.RunegateQueries.GET_PORTAL_LIST(this.gateBuilding.getObjectUUID());

        for (Portal portal : portalList) {
            this._portals[portal.portalType.ordinal()] = portal;
        }
    }

    public void _serializeForEnterWorld(ByteBufferWriter writer) {

        writer.putInt(gateBuilding.getObjectType().ordinal());
        writer.putInt(gateBuilding.getObjectUUID());
        writer.putString(gateBuilding.getParentZone().zoneName);
        writer.putFloat(gateBuilding.getLoc().getLat());
        writer.putFloat(gateBuilding.getLoc().getAlt());
        writer.putFloat(gateBuilding.getLoc().getLong());
    }

}