package net.cobem.grizzly.habbohotel.catalog;

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

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CatalogParent {
    public int ID;
    public int Order;
    public int IconColor;
    public int IconImage;
    public int Rank;

    public CatalogLayout Layout;

    public String Title;

    public List<String> Headers = new ArrayList<>();
    public List<String> Texts = new ArrayList<>();

    public boolean Enabled;

    public CatalogParent(ResultSet Page) {
        //Grizzly.GrabDatabase().SetQuery("SELECT * FROM server_store_pages WHERE id = '" + ID + "'");

        //ResultSet Page = Grizzly.GrabDatabase().GrabRow();

        FillClass(Page);
    }

    private boolean FillClass(ResultSet Set) {
        try {
            this.ID = Set.getInt("id");
            this.Order = Set.getInt("order");
            this.IconColor = Set.getInt("icon_color");
            this.IconImage = Set.getInt("icon_image");
            this.Rank = Set.getInt("minimum_rank");

            this.Layout = CatalogLayout.valueOf(Set.getString("layout"));

            this.Title = Set.getString("title");

            this.Enabled = (Set.getInt("enabled") == 1);
            initOtherData();
            return true;
        } catch (SQLException Ex) {
            return false;
        }
    }

    private boolean initOtherData() {
        DatabaseResult GetHeaders = Grizzly.getStorage().preparedQuery("SELECT * FROM server_store_page_texts WHERE page_id = ? AND is_parent = ?", Arrays.asList(String.valueOf(this.ID), String.valueOf(1)));

        ResultSet Headers = GetHeaders.getTable();

        try {
            while (Headers.next()) {
                this.Headers.add(Headers.getString("value"));
            }
        } catch (SQLException e) {
        }

        DatabaseResult GetTexts = Grizzly.getStorage().preparedQuery("SELECT * FROM server_store_page_texts WHERE page_id = ? AND is_parent = ?", Arrays.asList(String.valueOf(this.ID), String.valueOf(1)));


        ResultSet Texts = GetTexts.getTable();

        try {
            while (Texts.next()) {
                this.Texts.add(Texts.getString("value"));
            }
        } catch (SQLException e) {
        }

        return true;
    }

    public boolean containsLTD() {
        for (CatalogItem Item : Grizzly.getHabboHotel().getCatalog().getItems(ID).values()) {
            if (Item.LTD) {
                return true;
            }
        }

        return false;
    }
}
