#include "Dialog.h"

#include "AsynchronCall.h"
#include "SelectionBox.h"
#include "Screen.h"
#include "Window.h"
#include "Button.h"
#include "RenderThread.h"
#include "Font.h"
#include "Text.h"

using namespace Framework;

// Contents of the MultiplChoiceDialog class from Dialog.h
MultiplChoiceDialog::MultiplChoiceDialog()
    : ReferenceCounter()
{
    entrys = new RCArray<Text>();
    ids = new Array<void*>();
}

MultiplChoiceDialog::~MultiplChoiceDialog()
{
    entrys->release();
    ids->release();
}

// Adds a selection option
void MultiplChoiceDialog::addChoice(const char* text, void* id)
{
    entrys->add(new Text(text));
    ids->add(id);
}

// shows the dialog and waits for user input
void* MultiplChoiceDialog::show(Font* zFont)
{
    void* result = 0;
    bool ex = 0;

    WNDCLASS wc = F_Normal(0);
    wc.lpszClassName = "Dialog";
    NativeWindow* f = new NativeWindow();
    f->setPreCloseAction([&ex](void* p, void* o) { ex = true; });
    f->setMouseAction(_ret1ME);
    f->setKeyboardAction(_ret1TE);
    f->create(WS_OVERLAPPEDWINDOW, wc);
    f->setSize(200, 200);
    f->setPosition(ScreenCenter(dynamic_cast<NativeWindow*>(f->getThis())));
    f->setMovable(1);
    f->setDisplayMode(1);
    Screen* b = new Screen2D(dynamic_cast<NativeWindow*>(f->getThis()));
    f->setScreen(dynamic_cast<Screen*>(b->getThis()));
    b->update();

    RenderTh* r = new RenderTh();
    r->setScreen(dynamic_cast<Screen*>(b->getThis()));
    r->beginn();

    SelectionBox* ab = new SelectionBox();
    ab->setPosition(10, 10);
    ab->setSize(180, 20);
    ab->setBackgroundColor(0xFF000000);
    ab->setBorderWidth(1);
    ab->setBorderColor(0xFFFFFFFF);
    ab->setMaxAuskappHeight(120);
    ab->setMouseBorderWidth(1);
    ab->setMouseBorderColor(0xFF005500);
    ab->setMouseAlphaFieldColor(0x00008700);
    ab->setMouseAlphaFieldStrength(-8);
    ab->setSelBorderWidth(1);
    ab->setSelBorderColor(0xFF00FF00);
    ab->setSelAlphaFieldColor(0x0000FF00);
    ab->setSelAlphaFieldStrength(-8);
    ab->setStyle(SelectionBox::Style::Normal);
    ab->setFontZ(dynamic_cast<Font*>(zFont->getThis()));
    for (Text* i : *entrys)
        ab->addEntry(i->getText());
    ab->setMouseEvent(_ret1ME);
    b->addMember(ab);

    Button* ok = new Button();
    ok->setStyle(Button::Style::Normal);
    ok->setPosition(50, 150);
    ok->setSize(100, 20);
    ok->setFontZ(dynamic_cast<Font*>(zFont->getThis()));
    ok->setText("Ok");
    ok->setMouseEvent(
        [this, &ex, &result, ab](void* p, void* o, MouseEvent me) {
            if (me.id == ME_RLeft)
            {
                result = ids->get(ab->getSelection());
                ex = true;
            }
            return 1;
        });
    b->addMember(ok);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0 && !ex)
    {
        if (!ex)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    r->terminate();
    r->release();

    b->release();
    f->setScreen(0);
    f->setDisplayMode(0);
    f->destroy();
    f->release();

    ok->release();
    ab->release();

    return result;
}