package net.cobem.grizzly.communication.composers.catalog;

import net.cobem.grizzly.Grizzly;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.HeaderLibrary;
import net.cobem.grizzly.habbohotel.catalog.CatalogItem;
import net.cobem.grizzly.habbohotel.catalog.CatalogPage;
import net.cobem.grizzly.habbohotel.catalog.CatalogParent;

public class SendCatalogPageComposer {
    public static EventResponse compose(int PageID) {
        EventResponse Message = new EventResponse();

        CatalogPage Page = Grizzly.getHabboHotel().getCatalog().getPageByID(PageID);

        if (!Page.Enabled) {
            return new EventResponse();
        }

        Message.Initialize(HeaderLibrary.ShowCatalogPageEvent);
        Message.addInt(Page.ID);

        switch(Page.Layout.toString()) {
            case "frontpage":
                Message.addString("frontpage3");
                Message.addInt(3);
                Message.addString(Page.Header);
                Message.addString("");
                Message.addString("");
                Message.addInt(11);
                Message.addString(""); //Special
                Message.addString(Page.Description); //Text1
                Message.addString("");
                Message.addString(""); //Text2
                Message.addString(""); //Details
                Message.addString(""); //Tease2
                Message.addString("Rares");
                Message.addString("#FEFEFE");
                Message.addString("#FEFEFE");
                Message.addString("Click here for more info..");
                Message.addString("magic.credits");
                break;

            case "spaces":
                Message.addString("spaces_new");
                Message.addInt(1);
                Message.addString(Page.Header);
                Message.addInt(1);
                Message.addString(Page.Header); //Supposed to be Text1 but who needs it?
                break;

            case "trophies":
                Message.addString("trophies");
                Message.addInt(1);
                Message.addString(Page.Header);
                Message.addInt(2);
                Message.addString(Page.Description);
                Message.addString(""); //Details
                Message.addInt(0);
                Message.addInt(0);
                Message.addInt(-1);
                break;

            case "pets":
                Message.addString("pets");
                Message.addInt(2);
                Message.addString(Page.Header);
                Message.addString(Page.Description);
                Message.addInt(4);
                Message.addString(""); //Text1
                Message.addString("Give a name:");
                Message.addString("Pick a color:");
                Message.addString("Pick a race:");
                Message.addInt(0);
                Message.addInt(0);
                Message.addInt(-1);
                break;

            default:
                Message.addString(Page.Layout);
                Message.addInt(3);
                Message.addString(Page.Header);
                Message.addString(""); //desc?
                Message.addString(""); //Special
                Message.addInt(3);
                Message.addString(Page.Description); //Text1
                Message.addString(""); //Details
                Message.addString(""); //Teaser2
                break;
        }

        Message.addInt(Grizzly.getHabboHotel().getCatalog().getItems(Page.ID).size());

        for (CatalogItem Item : Grizzly.getHabboHotel().getCatalog().getItems(Page.ID).values()) {
            Message.addInt(Item.ID);
            Message.addString(Item.getBase().PublicTitle);
            Message.addInt(Item.Cost);

            if (Item.PixelCost > 0) {
                Message.addInt(Item.PixelCost);
                Message.addInt(0);
            } else if (Item.CurrencyCost > 0) {
                Message.addInt(Item.CurrencyCost);
                Message.addInt(new Integer(Grizzly.getConfig().get("habbo.currency.default.id")));
            } else {
                Message.addInt(0);
                Message.addInt(0);
            }

            Message.addBool(true);
            Message.addInt(1);
            Message.addString(Item.getBase().Type);
            Message.addInt(Item.getBase().Sprite);

            Message.addString(Item.getPresetFlag());

            Message.addInt(Item.Quantity);
            Message.addBool(Item.LTD); //IS_LTD

            if (Item.LTD) {
                Message.addInt(Item.LTD_Stock); //LTD_STOCK
                Message.addInt(Item.LTD_Stock - Item.LTD_Purchased); //LTD_LEFT
            }

            Message.addInt(0);
            Message.addBool(!Item.Title.contains("cf_") && !Item.LTD); //Multi-Purchase!!
        }

        Message.addInt(-1);
        Message.addBool(false);

        if (!Grizzly.getHabboHotel().getCatalog().getCache().containsKey(Page.ID) && !Page.containsLTD()) {
            Grizzly.getHabboHotel().getCatalog().getCache().put(Page.ID, Message);
        }

        return Message;
    }
}

