#include "RecipieGroup.h"

#include <Bild.h>
#include <Schrift.h>

#include "Globals.h"

RecipieGroup::RecipieGroup()
    : Framework::ZeichnungHintergrund()
{
    setStyle(Framework::Zeichnung::Style::Erlaubt
             | Framework::Zeichnung::Style::Sichtbar);
    icon = 0;
    previousPage = uiFactory.createKnopf(uiFactory.initParam);
    previousPage->setText("<");
    previousPage->setToolTipText("Previous Page",
        uiFactory.initParam.bildschirm,
        uiFactory.initParam.schrift);
    previousPage->setSize(20, 20);
    previousPage->setPosition(0, 0);
    previousPage->setStyle(
        Framework::Knopf::Style::Normal & ~Framework::Knopf::Style::Sichtbar);
    previousPage->setMausEreignis([this](void* p, void* z, MausEreignis me) {
        if (me.id == ME_RLinks)
        {
            currentPage--;
        }
        return 1;
    });

    nextPage = uiFactory.createKnopf(uiFactory.initParam);
    nextPage->setText(">");
    nextPage->setToolTipText("Next Page",
        uiFactory.initParam.bildschirm,
        uiFactory.initParam.schrift);
    nextPage->setSize(20, 20);
    nextPage->setPosition(0, 0);
    nextPage->setStyle(
        Framework::Knopf::Style::Normal & ~Framework::Knopf::Style::Sichtbar);
    nextPage->setMausEreignis([this](void* p, void* z, MausEreignis me) {
        if (me.id == ME_RLinks)
        {
            currentPage++;
        }
        return 1;
    });
}

RecipieGroup::~RecipieGroup()
{
    if (icon) icon->release();
    previousPage->release();
    nextPage->release();
}

void RecipieGroup::addRecipie(Framework::Zeichnung* recipie)
{
    recipies.add(recipie);
}

void RecipieGroup::setIcon(Framework::Bild* icon)
{
    if (this->icon) this->icon->release();
    this->icon = icon;
}

void RecipieGroup::setToolTip(Framework::Text toolTip)
{
    this->toolTip = toolTip;
}

bool RecipieGroup::tick(double tickVal)
{
    int pi = 1;
    int y = 20;
    bool first = 1;
    for (Zeichnung* recipie : recipies)
    {
        if (recipie->getHeight() + y > getHeight() && !first)
        {
            pi++;
            y = 20;
        }
        recipie->setStyle(Zeichnung::Style::Sichtbar, pi == currentPage);
        recipie->setX(getBreite() / 2 - recipie->getBreite() / 2);
        recipie->setY(y);
        y += recipie->getHeight() + 50;
        first = 0;
        rend |= recipie->tick(tickVal);
    }
    nextPage->setX(getBreite() - 20);
    maxPage = pi;
    currentPage = MAX(1, MIN(pi, currentPage));
    previousPage->setStyle(Knopf::Style::Sichtbar, currentPage > 1);
    nextPage->setStyle(Knopf::Style::Sichtbar, currentPage < maxPage);
    rend |= previousPage->tick(tickVal);
    rend |= nextPage->tick(tickVal);
    return ZeichnungHintergrund::tick(tickVal);
}

void RecipieGroup::render(Framework::Bild& rObj)
{
    if (!hatStyle(Zeichnung::Style::Sichtbar)) return;
    ZeichnungHintergrund::render(rObj);
    if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
    Framework::TextRenderer tr(dynamic_cast<Framework::Schrift*>(uiFactory.initParam.schrift->getThis()));
    tr.setSchriftSize(12);
    Text t = Text("Recipie page ") + Text(currentPage) + Text(" of ") + Text(maxPage);
    int tbr = tr.getTextBreite(t);
    tr.renderText(getBreite() / 2 - tbr / 2, 4, t, rObj, 0xFFFFFFFF);
    for (Framework::Zeichnung* recipie : recipies)
    {
        recipie->render(rObj);
    }
    previousPage->render(rObj);
    nextPage->render(rObj);
    rObj.releaseDrawOptions();
}

void RecipieGroup::doMausEreignis(Framework::MausEreignis& me, bool userRet)
{
    previousPage->doPublicMausEreignis(me);
    nextPage->doPublicMausEreignis(me);
    for (Zeichnung* recipie : recipies)
    {
        recipie->doPublicMausEreignis(me);
    }
}

Framework::Bild* RecipieGroup::zIcon() const
{
    return icon;
}

Framework::Text RecipieGroup::getToolTip() const
{
    return toolTip;
}