#include "ItemList.h"

#include <DateiSystem.h>
#include <Scroll.h>
#include <TextFeld.h>

#include "Game.h"
#include "Globals.h"
#include "UIMLToolTip.h"
#include "World.h"

ItemList::ItemList()
    : ZeichnungHintergrund(),
      currentTooltipSlot(-1)
{
    setStyle(ZeichnungHintergrund::Style::Sichtbar
             | ZeichnungHintergrund::Style::Erlaubt);
    slotList = new int[itemTypeCount];
    memset(slotList, 0, sizeof(int) * itemTypeCount);
    slotCount = 0;
    setNeedToolTipEvent([this](Zeichnung* z, Punkt p) {
        int slot = getSlotByLocalPos(p);
        if (currentTooltipSlot != slot && currentTooltipSlot != -1)
        {
            std::cout << "closing tooltip\n";
            this->setToolTipZ(0);
            currentTooltipSlot = -1;
        }
        if (slot != -1)
        {
            std::cout << "requesting tooltip for slot " << slot << "\n";
            UIMLToolTip* tip = new UIMLToolTip();
            tip->setUIML(itemTypes[slot]->getTooltipUIML());
            tip->setWarten(0);
            tip->setPosition(mausPos.x, mausPos.y + 15);
            setToolTipZ(tip);
            currentTooltipSlot = slot;
            return 1;
        }
        return 0;
    });
}

ItemList::~ItemList()
{
    delete[] slotList;
}

int ItemList::getSlotByLocalPos(Framework::Punkt pos)
{
    int x = 0;
    int y = 0;
    for (int i = 0; i < slotCount; i++)
    {
        if (pos.x >= x && pos.x < x + 50 && pos.y >= y && pos.y < y + 50)
            return slotList[i];
        x += 60;
        if (x >= getBreite())
        {
            y += 60;
            x = 0;
        }
    }
    return -1;
}

void ItemList::doMausEreignis(Framework::MausEreignis& me, bool userRet)
{
    mausPos.x = me.originalX;
    mausPos.y = me.originalY;
    if (me.id == ME_Bewegung)
    {
        if (getSlotByLocalPos(Punkt(me.mx, me.my)) != currentTooltipSlot)
        {
            if (currentTooltipSlot != -1)
            {
                std::cout << "closing tooltip\n";
                setToolTipZ(0);
            }
            else
                toolTipRequested = 0;
            currentTooltipSlot = -1;
        }
    }
    else if (me.id == ME_RLinks)
    {
        int pos = getSlotByLocalPos(Punkt(me.mx, me.my));
        if (pos >= 0)
        {
            World::INSTANCE->zClient()->craftingUIMLRequest(
                itemTypes[pos]->getId());
        }
    }
    ZeichnungHintergrund::doMausEreignis(me, userRet);
}

bool ItemList::tick(double time)
{
    return ZeichnungHintergrund::tick(time);
}

void ItemList::adjustSize(int parentWidth, int parentHeight)
{
    int ipr = (parentWidth + 10) / 60;
    if (ipr == 0)
    {
        ipr = 1;
    }
    setSize(ipr * 60 - 10,
        (itemTypeCount / ipr + (itemTypeCount % ipr == 0 ? 0 : 1)) * 60 - 10);
}

void ItemList::render(Framework::Bild& rObj)
{
    const Text* filter
        = dynamic_cast<Game*>((Menu*)menuRegister->get("game"))->zFilterText();
    ZeichnungHintergrund::render(rObj);
    if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
    int x = 0;
    int y = 0;
    int index = 0;
    for (int i = 0; i < itemTypeCount; i++)
    {
        if (filter->getLength() > 0
            && !itemTypes[i]->getName().hat(filter->getText()))
        {
            continue;
        }
        slotList[index] = i;
        rObj.fillRegion(x, y, 50, 50, 0xFF222222);
        rObj.alphaBild(x, y, 50, 50, *itemTypes[i]->zIcon());
        x += 60;
        if (x >= getBreite())
        {
            x = 0;
            y += 60;
        }
        index++;
    }
    slotCount = index;
    rObj.releaseDrawOptions();
}

ItemListContainer::ItemListContainer()
    : Fenster()
{
    LTDBDatei iconsDat;
    iconsDat.setDatei(new Text("data/images/gui_icons.ltdb"));
    iconsDat.leseDaten(0);

    setStyle(
        Fenster::Style::Erlaubt | Fenster::Style::Rahmen
        | Fenster::Style::BodyHAlpha | Fenster::Style::Beweglich
        | Fenster::Style::Titel | Fenster::Style::TitelHAlpha
        | Fenster::Style::TitelHintergrund | Fenster::Style::BodyHintergrund
        | Fenster::Style::MEIgnoreInside | Fenster::Style::VScroll
        | Fenster::Style::BreiteChangeable | Fenster::Style::BodyMinBr
        | Fenster::Style::BodyMinHi | Fenster::Style::HeightChangeable
        | Fenster::Style::Closable | Fenster::Style::ClosingHAlpha
        | Fenster::Style::ClosingKlickBuffer | Fenster::Style::ClosingHAlpha
        | Fenster::Style::ClosingHintergrund | Style::ClosingHBild
        | Style::ClosingBuffer);
    setKBgFarbe(0xA0000000);
    setTBgFarbe(0xA0000000);
    setSBgFarbe(0xA0000000);
    setSize(329, window->zBildschirm()->getBackBufferSize().y - 20);
    setPosition(
        window->zBildschirm()->getBackBufferSize().x - getBreite() - 10, 10);
    setRBreite(2);
    setRFarbe(0xFF52525E);
    setTitel("Recipies");
    setTSchriftZ(
        dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
    zTTextFeld()->setSize(0, 20);
    zTTextFeld()->addStyle(TextFeld::Style::Center);
    setTastaturEreignis(_ret1TE);
    setKMin(70, 70);
    setVSBScroll(0);
    zVScrollBar()->setBgFarbe(0xA0000000, 1);
    setClosingMe([this](void* p, void* o, MausEreignis me) {
        if (me.id == ME_RLinks) removeStyle(Style::Sichtbar);
        return 1;
    });
    setSAfStrength(10);
    setSAfFarbe(0x5F9C0A0A);
    setSBgBildZ(iconsDat.laden(0, new Text("close.png")));
    setSKAfFarbe(0xFF9C0A0A);
    setSKAfStrength(10);

    list = new ItemList();
    list->setPosition(10, 10);
    addMember(list);
}

bool ItemListContainer::tick(double time)
{
    list->adjustSize(getInnenBreite() - 35, getInnenHeight());
    if (zVScrollBar()->getScrollData()->max != list->getHeight() + 20)
        setVSBMax(list->getHeight() + 20);
    return Fenster::tick(time);
}