#include "Initialization.h"

#include <FileSystem.h>
#include <List.h>
#include <MouseEvent.h>
#include <Progress.h>
#include <Screen.h>
#include <ToolTip.h>
#include <Window.h>

#include "Globals.h"

Button* createButton(const Framework::UIInitParam& params)
{
    Button* ret = Framework::defaultUI(params.font, params.bildschirm)
                      .createButton(params);
    ret->setBackgroundColor(0xFF000000);
    ret->setBorderColor(0xFF2E6DA4);
    ret->setFontColor(0xFFFFFFFF);
    ret->setBorderWidth(1);
    ret->setAlphaFieldStrength(10);
    ret->setAlphaFieldColor(0xFF337AB7);
    return ret;
}

TextField* createTextField(const Framework::UIInitParam& params)
{
    TextField* ret = Framework::defaultUI(params.font, params.bildschirm)
                         .createTextField(params);
    ret->setBorderColor(0xFF6d6d6d);
    ret->setBackgroundColor(0xFF000000);
    ret->setAlphaFieldColor(0xFF3d3d3d);
    ret->setAlphaFieldStrength(10);
    return ret;
}

CheckBox* createCheckBox(const Framework::UIInitParam& params)
{
    CheckBox* ret = Framework::defaultUI(params.font, params.bildschirm)
                        .createCheckBox(params);
    ret->removeStyle(CheckBox::Style::Border);
    ret->setBorderColor(0xFF2E6DA4);
    ret->setBackgroundColor(0xFF000000);
    ret->setClickAFColor(0xFF3d3d3d);
    ret->setClickAFStrength(10);
    ret->loadData("data/images/system.ltdb");
    return ret;
}

ObjTable* createObjTable(const Framework::UIInitParam& params)
{
    ObjTable* ret = Framework::defaultUI(params.font, params.bildschirm)
                        .createObjTable(params);
    ret->setBorderColor(0xFF6d6d6d);
    ret->setBackgroundColor(0xFF000000);
    ret->setGridColor(0xFF6d6d6d);
    ret->setVerticalScrollPos(0);
    ret->setHorizontalScrollPos(0);
    return ret;
}

Window* createWindow(const Framework::UIInitParam& params)
{
    Window* ret = Framework::defaultUI(params.font, params.bildschirm)
                      .createWindow(params);
    ret->setTitel("");
    ret->zTTextField()->setBorderColor(0xFF52525E);
    ret->setBorderColor(0xFF52525E);
    return ret;
}

Button* initButton(
    int x, int y, int br, int height, __int64 style, const char* titel)
{
    Button* ret = uiFactory.createButton(uiFactory.initParam);
    ret->addStyle(style);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    ret->setText(titel);
    return ret;
}

CheckBox* initCheckBox(
    int x, int y, int br, int height, __int64 style, const char* txt)
{
    CheckBox* ret = uiFactory.createCheckBox(uiFactory.initParam);
    ret->addStyle(style);
    ret->setText(txt);
    ret->setSText(txt);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    return ret;
}

Window* initWindow(
    int x, int y, int br, int height, __int64 style, const char* titel)
{
    Window* ret = uiFactory.createWindow(uiFactory.initParam);
    ret->addStyle(style);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    if (ret->hasStyle(Window::Style::Title))
    {
        ret->setTitel(titel);
    }
    return ret;
}

TextField* initTextField(
    int x, int y, int br, int height, __int64 style, const char* txt)
{
    TextField* ret = uiFactory.createTextField(uiFactory.initParam);
    ret->setStyle(style);
    ret->setText(txt);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    return ret;
}

ImageView* initImageView(
    int x, int y, int br, int height, __int64 style, Image* b)
{
    ImageView* ret = uiFactory.createImageView(uiFactory.initParam);
    ret->setStyle(style);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    if (b) ret->setImageZ(b);
    return ret;
}

SelectionBox* initSelectionBox(int x,
    int y,
    int br,
    int height,
    __int64 style,
    std::initializer_list<const char*> values)
{
    SelectionBox* ret = uiFactory.createSelectionBox(uiFactory.initParam);
    ret->addStyle(style);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    for (auto i = values.begin(); i != values.end(); i++)
        ret->addEntry(*i);
    return ret;
}

ObjTable* initObjTable(int x,
    int y,
    int br,
    int height,
    __int64 style,
    std::initializer_list<OBJTabelleSpalteIni> spalten,
    int titleHeight)
{
    ObjTable* ret = uiFactory.createObjTable(uiFactory.initParam);
    ret->addStyle(style);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    for (auto i = spalten.begin(); i != spalten.end(); i++)
    {
        ret->addColumn(i->name);
        ret->setColumnWidth(i->name, i->breite);
        if ((style | ObjTable::Style::ColumnWidthMin) == style)
            ret->setMinColumnWidth(i->name, i->minBreite);
        if ((style | ObjTable::Style::ColumnWidthMax) == style)
            ret->setMaxColumnWidth(i->name, i->maxBreite);
        if (titleHeight)
        {
            if (ret->getRowNumber("title") < 0)
            {
                ret->addRow(0, "title");
                ret->setRowHeight(0, 20);
            }
            ret->setDrawableZ(i->name,
                "title",
                initTextField(0,
                    0,
                    i->breite,
                    20,
                    TextField::Style::Text | TextField::Style::Center,
                    i->name));
        }
    }
    return ret;
}

LineDiagram* initLinieDiagram(
    int x, int y, int br, int height, __int64 style, DiagramData* data)
{
    LineDiagram* ret = uiFactory.createLineDiagram(uiFactory.initParam);
    ret->addStyle(style);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    if (data) ret->setDiagramDataZ(data);
    return ret;
}

SelectionList* initSelectionList(int x,
    int y,
    int br,
    int height,
    __int64 style,
    std::initializer_list<const char*> values)
{
    SelectionList* ret = uiFactory.createSelectionList(uiFactory.initParam);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    ret->addStyle(style);
    for (auto i = values.begin(); i != values.end(); i++)
        ret->addEntry(*i);
    ret->update();
    return ret;
}

DrawableBackground* initDrawingBackground(
    int x, int y, int br, int height, __int64 style, int farbe)
{
    DrawableBackground* ret = new DrawableBackground();
    ret->setStyle(style);
    ret->setBackgroundColor(farbe);
    if (ret->hasStyle(DrawableBackground::Style::Border))
    {
        ret->setBorderWidth(1);
        ret->setBorderColor(0xFF00FF00);
    }
    ret->setPosition(x, y);
    ret->setSize(br, height);
    return ret;
}

void initToolTip(Drawable* obj, const char* txt)
{
    obj->setToolTipText(
        txt, uiFactory.initParam.bildschirm, uiFactory.initParam.font);
    obj->zToolTip()->addStyle(TextField::Style::Multiline);
}

ProgressBar* initProgressBar(int x, int y, int br, int height, __int64 style)
{
    ProgressBar* ret = uiFactory.createProgressBar(uiFactory.initParam);
    ret->addStyle(style);
    ret->setPosition(x, y);
    ret->setSize(br, height);
    LTDBFile* file = new LTDBFile();
    file->setFile(new Text("data/images/system.ltdb"));
    file->readData(0);
    Image* fbhb = file->load(0, new Text("fortschrittleh"));
    ret->setBackgroundImageZ(fbhb);
    Image* fbfhb = file->load(0, new Text("fortschrittvol"));
    file->release();
    ret->setFBgImageZ(fbfhb);
    return ret;
}