#include "OptionsWindow.h"

#include <FileSystem.h>

#include "Globals.h"
#include "Initialization.h"

OptionsWindow::OptionsWindow(std::function<void()> onOptionsOpen)
    : Window(),
      onOptionsOpen(onOptionsOpen)
{
    LTDBFile iconsDat;
    iconsDat.setFile(new Text("data/images/gui_icons.ltdb"));
    iconsDat.readData(0);

    setStyle(Style::Allowed | Style::Border | Style::BodyBAlpha | Style::Title
             | Style::TitleBAlpha | Style::Closable | Style::ClosingBAlpha
             | Style::CustomTitle | Style::ClosingBuffer | Style::CustomTitle
             | Style::ClosingBuffer | Style::ClosingBImage
             | Style::TitleBackground | Style::BodyBackground
             | Style::ClosingBackground | Style::MEIgnoreInside
             | Style::HeightChangeable | Style::WidthChangeable
             | Style::BodyMinWidth | Style::BodyMinHeight | Style::Movable);
    removeStyle(Style::Visible);
    setMouseEvent(Framework::_ret1ME);
    setKeyboardEvent(Framework::_ret1TE);
    setBorderWidth(1);
    setBorderColor(0xFF52525E);
    setBodyBgColor(0xA0000000);
    setTBgColor(0xA0000000);
    setCloseBgColor(0xA0000000);
    setTFontZ(dynamic_cast<Font*>(uiFactory.initParam.font->getThis()));
    zTTextField()->setSize(0, 20);
    zTTextField()->addStyle(TextField::Style::Center);
    setCloseAfStrength(10);
    setCloseAfColor(0x5F9C0A0A);
    setCloseBgImageZ(iconsDat.load(0, new Text("close.png")));
    setCloseClickAfColor(0xFF9C0A0A);
    setCloseClickAfStrength(10);

    optionsButton = initButton(0,
        0,
        20,
        20,
        Framework::Button::Style::BImage | Framework::Button::Style::BAlpha
            | Framework::Button::Style::Background,
        "");
    optionsButton->setAlphaFieldColor(0x5F337AB7);
    optionsButton->setToolTipText(
        "Options", uiFactory.initParam.bildschirm, uiFactory.initParam.font);
    optionsButton->setBackgroundImageZ(
        iconsDat.load(0, new Text("options.png")));
    optionsButton->setMouseEvent([this](void* p, void* o, MouseEvent me) {
        if (me.id == ME_RLeft)
        {
            this->onOptionsOpen();
        }
        return 1;
    });
}

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

void OptionsWindow::doMouseEvent(Framework::MouseEvent& me, bool userRet)
{
    optionsButton->doPublicMouseEvent(me);
    Window::doMouseEvent(me, userRet);
}

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

void OptionsWindow::render(Framework::Image& rObj)
{
    if (!hasStyle(Style::Visible)) return;
    int titleHeight = zTTextField()->getHeight();
    zTTextField()->setPosition(titleHeight, 0);
    int closingWidth = 0;
    if (hasStyle(Style::Closable)) closingWidth = titleHeight;
    zTTextField()->setWidth(getInnerWidth() - titleHeight - closingWidth);
    Window::render(rObj);
    optionsButton->setSize(titleHeight, titleHeight);
    int rbr = getBorderWidth();
    if (!rObj.setDrawOptions(
            pos.x + rbr, pos.y + rbr, gr.x - rbr * 2, titleHeight))
    {
        return;
    }
    optionsButton->render(rObj);
    rObj.releaseDrawOptions();
}