#include "UIDialog.h"

#include "Button.h"
#include "Font.h"
#include "TextField.h"
#include "UIInitialization.h"

using namespace Framework;

UIDialog::UIDialog(Font* font)
    : Window()
{
    addStyle(Window::Style::normal | Window::Style::BodyBackground
             | Window::Style::TitleBackground
             | Window::Style::ClosingBackground);
    setBodyBgColor(0xFF000000);
    setTBgColor(0xFF000000);
    setCloseBgColor(0xFF000000);
    setMouseEvent(_ret1ME);
    setKeyboardEvent(_ret1TE);
    setBorderWidth(1);
    setBorderColor(0xFFFFFFFF);
    setTitel("");
    setTFontZ(font);
    setTFontColor(0xFFFFFFFF);
    zTTextField()->setSize(0, 20);
    zTTextField()->addStyle(TextField::Style::Visible | TextField::Style::Center
                            | TextField::Style::Border);
    setTBorderColor(0xFF00FF00);
    setTBorderWidth(1);
    setTAfColor(0x1000FF00);
    setTAfStrength(-15);
    setCloseClickAfColor(0xFF00FF00);
    setCloseClickAfStrength(30);
    setClosingMe([this](void* p, void* o, MouseEvent me) {
        if (me.id == ME_RLeft) removeStyle(Window::Style::Visible);
        return 1;
    });
}

UIDialog::~UIDialog() {}

void UIDialog::adjustSize()
{
    Point max = Point(0, 0);
    for (Drawable* z : getMembers())
    {
        if (z->hasStyle(Drawable::Style::Visible))
        {
            max.x = MAX(max.x, z->getX() + z->getWidth() + 5);
            max.y = MAX(max.y, z->getY() + z->getHeight() + 5);
        }
    }
    setSize(
        max
        + Point(hasStyle(Window::Style::Border) ? getBorderWidth() * 2 : 0,
            (hasStyle(Window::Style::Border) ? getBorderWidth() * 2 : 0)
                + (hasStyle(Window::Style::Title) ? zTTextField()->getHeight()
                                                  : 0)));
}

// Adds a drawing to the window
//  obj: the drawing
void UIDialog::addMember(Drawable* obj)
{
    Window::addMember(obj);
    adjustSize();
}

// Removes a drawing from the window
//  zObj: the drawing (without increased reference counter)
void UIDialog::removeMember(Drawable* zObj)
{
    Window::removeMember(zObj);
    adjustSize();
}

// Removes all drawings from the window
void UIDialog::removeAll()
{
    Window::removeAll();
    adjustSize();
}

UIMessageDialog::UIMessageDialog(
    const char* title, const char* message, Font* font)
    : UIDialog(font)
{
    setTitel(title);
    UIInit init = defaultUI(font, 0);
    TextField* msg = init.createTextField(init.initParam);
    msg->setStyle(TextField::Style::Text);
    msg->setText(message);
    msg->setPosition(5, 5);
    TextRenderer tr(dynamic_cast<Font*>(font->getThis()));
    tr.formatText(msg->zText(), 590);
    int br = tr.getTextWidth(message);
    int hoe = tr.getTextHeight(message);
    msg->setSize(br, hoe);
    Button* ok = init.createButton(init.initParam);
    ok->setText("Ok");
    ok->setMouseEvent([this](void* p, void* o, MouseEvent me) {
        if (me.id == ME_RLeft) removeStyle(Window::Style::Visible);
        return 1;
    });
    ok->setPosition(br - 100, hoe + 5);
    ok->setSize(100, 20);
    addMember(ok);
    addMember(msg);
    setSize(br + 10, hoe + 55);
}

UIMessageDialog::~UIMessageDialog() {}