package net.cobem.grizzly.habbohotel.catalog;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.storage.DatabaseResult;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class CatalogHandler {
    private Map<Integer, CatalogPage> Pages;
    private Map<Integer, CatalogItem> Items;
    private LinkedHashMap<Integer, CatalogParent> Parents;

    private Map<Integer, EventResponse> PageCache;

    public CatalogHandler() throws SQLException {
        /*
        Grizzly.write("Loading catalog parents... ", true, false);

        Parents = new LinkedHashMap<>();

        DatabaseResult GrabParents = Grizzly.getStorage().query("SELECT * FROM server_store_parents ORDER BY `order` ASC");

        ResultSet CatalogParents = GrabParents.getTable();

        while (CatalogParents.next()) {
            Parents.put(CatalogParents.getInt("id"), new CatalogParent(CatalogParents));
        }

        Grizzly.write(Parents.size(), false, true);

        */
        Grizzly.write("Loading catalog pages... ", true, false);

        Pages = new HashMap<>();

        DatabaseResult GrabPages = Grizzly.getStorage().query("SELECT * FROM catalog_pages ORDER BY `order_num` ASC");

        ResultSet CatalogPages = GrabPages.getTable();

        while (CatalogPages.next()) {
            Pages.put(CatalogPages.getInt("id"), new CatalogPage(CatalogPages));
        }

        Grizzly.write(Pages.size(), false, true);

        Grizzly.write("Loading catalog items... ", true, false);

        Items = new HashMap<>();

        DatabaseResult GrabItems = Grizzly.getStorage().query("SELECT * FROM catalog_items");

        ResultSet CatalogItems = GrabItems.getTable();

        while (CatalogItems.next()) {
            Items.put(CatalogItems.getInt("id"), new CatalogItem(CatalogItems));
        }

        Grizzly.write(Items.size(), false, true);

        PageCache = new HashMap<>();
    }

    public Map<Integer, CatalogPage> getPages() {
        return Pages;
    }

    public Map<Integer, EventResponse> getCache() {
        return PageCache;
    }

    public Map<Integer, CatalogPage> getPrimary() {
        Map<Integer, CatalogPage> Primaries = new LinkedHashMap<>();

        for (CatalogPage Page : Pages.values()) {
            if (Page.Parent == -1) {
                Primaries.put(Page.ID, Page);
            }
        }

        return Primaries;
    }

    public Map<Integer, CatalogPage> getSecondary(int Parent) {
        Map<Integer, CatalogPage> Primaries = new LinkedHashMap<Integer, CatalogPage>();

        for (CatalogPage Page : Pages.values()) {
            if (Page.Parent == Parent) {
                Primaries.put(Page.ID, Page);
            }
        }

        return Primaries;
    }

    public CatalogPage getPageByID(int ID) {
        return Pages.get(ID);
    }


    public Map<Integer, CatalogItem> getItems(int ID) {
        Map<Integer, CatalogItem> Itemz = new HashMap<>();

        for (CatalogItem Item : Items.values()) {
            if (Item.Page == ID) {
                Itemz.put(Item.ID, Item);
            }
        }

        return Itemz;
    }

    public CatalogItem getItemByID(int ID) {
        for (CatalogItem Item : Items.values()) {
            if (Item.ID == ID){
                return Item;
            }
        }

        return null;
    }
    public void createTeleporters(int FurniID, int Pair){
        Grizzly.getStorage().execute("INSERT INTO server_teleporters (teleporter_one, teleporter_two) VALUES ('" + FurniID + "', '" + Pair + "')");

        Grizzly.getHabboHotel().getFurniture().getTeleporters().addPair(FurniID, Pair);
    }

    public void refresh(){

        try{
            Parents = new LinkedHashMap<>();

            DatabaseResult GrabParents = Grizzly.getStorage().query("SELECT * FROM server_store_parents ORDER BY 'order' ASC");

            ResultSet CatalogParents = GrabParents.getTable();

            while (CatalogParents.next()) {
                Parents.put(CatalogParents.getInt("id"), new CatalogParent(CatalogParents));
            }

            Pages = new HashMap<>();

            DatabaseResult GrabPages = Grizzly.getStorage().query("SELECT * FROM server_store_pages ORDER BY 'order' ASC");

            ResultSet CatalogPages = GrabPages.getTable();

            while (CatalogPages.next()) {
                Pages.put(CatalogPages.getInt("id"), new CatalogPage(CatalogPages));
            }

            Items = new HashMap<>();

            DatabaseResult GrabItems = Grizzly.getStorage().query("SELECT * FROM server_store_items");

            ResultSet CatalogItems = GrabItems.getTable();

            while (CatalogItems.next()) {
                Items.put(CatalogItems.getInt("id"), new CatalogItem(CatalogItems));
            }

            PageCache = new HashMap<>();
        } catch(Exception ignored) {}

    }
}
