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.CatalogPage;
import net.cobem.grizzly.habbohotel.catalog.CatalogParent;

import java.util.Map;

public class SendCatalogComposer {
    public static EventResponse compose(int Rank) {
        EventResponse Message = new EventResponse();

        Map<Integer, CatalogPage> Primary = Grizzly.getHabboHotel().getCatalog().getPrimary();

        int Size = 0;

        for (CatalogPage Parent : Primary.values()) {
            if (Parent.Rank <= Rank) //Easy permissions.
            {
                Size++;
            }
        }

        Message.Initialize(HeaderLibrary.ShowCatalogIndexEvent);
        Message.addBool(true);
        Message.addInt(0);
        Message.addInt(0);
        Message.addInt(-1);
        Message.addString("root");
        Message.addBool(false);
        Message.addBool(false);
        Message.addInt(Size);

        for (CatalogPage Parent : Primary.values()) {
            if (Parent.Rank > Rank) //Easy permissions.
            {
                continue;
            }

            Map<Integer, CatalogPage> Secondary = Grizzly.getHabboHotel().getCatalog().getSecondary(Parent.ID);

            Message.addBool(true);
            Message.addInt(Parent.IconColor);
            Message.addInt(Parent.IconImage);
            Message.addInt(Parent.ID);
            Message.addString(Parent.Title.toLowerCase().replace(" ", "_"));
            Message.addString(Parent.Title);
            Message.addInt(Secondary.size());

            for (CatalogPage Kid : Secondary.values()) {
                Message.addBool(true);
                Message.addInt(Kid.IconColor);
                Message.addInt(Kid.IconImage);
                Message.addInt(Kid.ID);
                Message.addString(Kid.Title.toLowerCase().replace(" ", "_"));
                Message.addString(Kid.Title);
                Message.addInt(0);
            }
        }

        Message.addBool(true);

        return Message;
    }
}
