#include "RecipieGroup.h"

#include <Image.h>
#include <Font.h>

#include "Globals.h"

RecipieGroup::RecipieGroup()
    : Framework::DrawableBackground()
{
    setStyle(Framework::Drawable::Style::Allowed
             | Framework::Drawable::Style::Visible);
    icon = 0;
    previousPage = uiFactory.createButton(uiFactory.initParam);
    previousPage->setText("<");
    previousPage->setToolTipText("Previous Page",
        uiFactory.initParam.bildschirm,
        uiFactory.initParam.font);
    previousPage->setSize(20, 20);
    previousPage->setPosition(0, 0);
    previousPage->setStyle(
        Framework::Button::Style::Normal & ~Framework::Button::Style::Visible);
    previousPage->setMouseEvent([this](void* p, void* z, MouseEvent me) {
        if (me.id == ME_RLeft)
        {
            currentPage--;
        }
        return 1;
    });

    nextPage = uiFactory.createButton(uiFactory.initParam);
    nextPage->setText(">");
    nextPage->setToolTipText("Next Page",
        uiFactory.initParam.bildschirm,
        uiFactory.initParam.font);
    nextPage->setSize(20, 20);
    nextPage->setPosition(0, 0);
    nextPage->setStyle(
        Framework::Button::Style::Normal & ~Framework::Button::Style::Visible);
    nextPage->setMouseEvent([this](void* p, void* z, MouseEvent me) {
        if (me.id == ME_RLeft)
        {
            currentPage++;
        }
        return 1;
    });
}

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

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

void RecipieGroup::setIcon(Framework::Image* 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 (Drawable* recipie : recipies)
    {
        if (recipie->getHeight() + y > getHeight() && !first)
        {
            pi++;
            y = 20;
        }
        recipie->setStyle(Drawable::Style::Visible, pi == currentPage);
        recipie->setX(getWidth() / 2 - recipie->getWidth() / 2);
        recipie->setY(y);
        y += recipie->getHeight() + 50;
        first = 0;
        rend |= recipie->tick(tickVal);
    }
    nextPage->setX(getWidth() - 20);
    maxPage = pi;
    currentPage = MAX(1, MIN(pi, currentPage));
    previousPage->setStyle(Button::Style::Visible, currentPage > 1);
    nextPage->setStyle(Button::Style::Visible, currentPage < maxPage);
    rend |= previousPage->tick(tickVal);
    rend |= nextPage->tick(tickVal);
    return DrawableBackground::tick(tickVal);
}

void RecipieGroup::render(Framework::Image& rObj)
{
    if (!hasStyle(Drawable::Style::Visible)) return;
    DrawableBackground::render(rObj);
    if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y)) return;
    Framework::TextRenderer tr(dynamic_cast<Framework::Font*>(uiFactory.initParam.font->getThis()));
    tr.setFontSize(12);
    Text t = Text("Recipie page ") + Text(currentPage) + Text(" of ") + Text(maxPage);
    int tbr = tr.getTextWidth(t);
    tr.renderText(getWidth() / 2 - tbr / 2, 4, t, rObj, 0xFFFFFFFF);
    for (Framework::Drawable* recipie : recipies)
    {
        recipie->render(rObj);
    }
    previousPage->render(rObj);
    nextPage->render(rObj);
    rObj.releaseDrawOptions();
}

void RecipieGroup::doMouseEvent(Framework::MouseEvent& me, bool userRet)
{
    previousPage->doPublicMouseEvent(me);
    nextPage->doPublicMouseEvent(me);
    for (Drawable* recipie : recipies)
    {
        recipie->doPublicMouseEvent(me);
    }
}

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

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