#include "OptionsWindow.h"

#include <DateiSystem.h>

#include "Globals.h"
#include "Initialisierung.h"

OptionsWindow::OptionsWindow(std::function<void()> onOptionsOpen)
    : Fenster(),
      onOptionsOpen(onOptionsOpen)
{
    LTDBDatei iconsDat;
    iconsDat.setDatei(new Text("data/images/gui_icons.ltdb"));
    iconsDat.leseDaten(0);

    setStyle(Style::Erlaubt | Style::Rahmen | Style::BodyHAlpha | Style::Titel
             | Style::TitelHAlpha | Style::Closable | Style::ClosingHAlpha
             | Style::CustomTitle | Style::ClosingBuffer
             | Style::ClosingKlickBuffer | Style::ClosingHBild
             | Style::TitelHintergrund | Style::BodyHintergrund
             | Style::ClosingHintergrund | Style::MEIgnoreInside
             | Style::HeightChangeable | Style::BreiteChangeable
             | Style::BodyMinBr | Style::BodyMinHi | Style::Beweglich);
    removeStyle(Style::Sichtbar);
    setMausEreignis(Framework::_ret1ME);
    setTastaturEreignis(Framework::_ret1TE);
    setRBreite(1);
    setRFarbe(0xFF52525E);
    setKBgFarbe(0xA0000000);
    setTBgFarbe(0xA0000000);
    setSBgFarbe(0xA0000000);
    setTSchriftZ(
        dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
    zTTextFeld()->setSize(0, 20);
    zTTextFeld()->addStyle(TextFeld::Style::Center);
    setSAfStrength(10);
    setSAfFarbe(0x5F9C0A0A);
    setSBgBildZ(iconsDat.laden(0, new Text("close.png")));
    setSKAfFarbe(0xFF9C0A0A);
    setSKAfStrength(10);

    optionsButton = initKnopf(0,
        0,
        20,
        20,
        Framework::Knopf::Style::HBild | Framework::Knopf::Style::HAlpha
            | Framework::Knopf::Style::Hintergrund,
        "");
    optionsButton->setAlphaFeldFarbe(0x5F337AB7);
    optionsButton->setToolTipText(
        "Options", uiFactory.initParam.bildschirm, uiFactory.initParam.schrift);
    optionsButton->setHintergrundBildZ(
        iconsDat.laden(0, new Text("options.png")));
    optionsButton->setMausEreignis([this](void* p, void* o, MausEreignis me) {
        if (me.id == ME_RLinks)
        {
            this->onOptionsOpen();
        }
        return 1;
    });
}

OptionsWindow::~OptionsWindow()
{
    optionsButton->release();
}

void OptionsWindow::doMausEreignis(Framework::MausEreignis& me, bool userRet)
{
    optionsButton->doPublicMausEreignis(me);
    Fenster::doMausEreignis(me, userRet);
}

bool OptionsWindow::tick(double time)
{
    rend |= optionsButton->tick(time);
    return Fenster::tick(time);
}

void OptionsWindow::render(Framework::Bild& rObj)
{
    if (!hatStyle(Style::Sichtbar)) return;
    int titleHeight = zTTextFeld()->getHeight();
    zTTextFeld()->setPosition(titleHeight, 0);
    int closingWidth = 0;
    if (hatStyle(Style::Closable)) closingWidth = titleHeight;
    zTTextFeld()->setWidth(getInnenBreite() - titleHeight - closingWidth);
    Fenster::render(rObj);
    optionsButton->setSize(titleHeight, titleHeight);
    int rbr = getRBreite();
    if (!rObj.setDrawOptions(
            pos.x + rbr, pos.y + rbr, gr.x - rbr * 2, titleHeight))
    {
        return;
    }
    optionsButton->render(rObj);
    rObj.releaseDrawOptions();
}