#include "Load.h"

#include <Array.h>
#include <AsynchronCall.h>
#include <File.h>
#include <FileSystem.h>
#include <GraphicsApi.h>
#include <M3File.h>
#include <Text.h>
#include <Texture.h>

#include "FactoryClient.h"
#include "Globals.h"
#include "Initialization.h"
#include "ServerSelection.h"

LoadMenu::LoadMenu(Screen* zScreen)
    : Menu(zScreen)
{
    Point center = zScreen->getBackBufferSize() / 2;
    all = initProgressBar(
        center.x - 100, center.y - 65, 200, 30, ProgressBar::Style::normal);
    stageTitle = initTextField(center.x - 100,
        center.y - 30,
        zScreen->getBackBufferSize().x - center.x + 100,
        15,
        TextField::Style::Text,
        "");
    stage = initProgressBar(
        center.x - 100, center.y - 15, 200, 30, ProgressBar::Style::normal);
    stage->removeStyle(ProgressBar::Style::Visible);
    stepTitle = initTextField(center.x - 100,
        center.y + 20,
        zScreen->getBackBufferSize().x - center.x + 100,
        15,
        TextField::Style::Text,
        "");
    step = initProgressBar(
        center.x - 100, center.y + 35, 200, 30, ProgressBar::Style::normal);
    step->removeStyle(ProgressBar::Style::Visible);
    elements.add(step);
    elements.add(stepTitle);
    elements.add(stage);
    elements.add(stageTitle);
    elements.add(all);
}

void LoadMenu::stepProgress(int stepSize)
{
    step->actionPlus(stepSize);
}

void LoadMenu::beginNextStep(const char* title, int nextStepSize)
{
    stepTitle->setText(title);
    step->setActionCount(nextStepSize);
    step->reset();
    step->addStyle(ProgressBar::Style::Visible);
}

void LoadMenu::stageProgress(int stepSize)
{
    stage->actionPlus(stepSize);
    step->removeStyle(ProgressBar::Style::Visible);
}

void LoadMenu::beginNextStage(const char* title, int nextStageSize)
{
    stageTitle->setText(title);
    stage->setActionCount(nextStageSize);
    stage->reset();
    stage->addStyle(ProgressBar::Style::Visible);
    step->removeStyle(ProgressBar::Style::Visible);
}

void LoadMenu::allProgress(int stepSize)
{
    all->actionPlus(stepSize);
    stage->removeStyle(ProgressBar::Style::Visible);
    step->removeStyle(ProgressBar::Style::Visible);
}

void LoadMenu::load(FactoryClient* client,
    Text name,
    Text secret,
    unsigned short port,
    std::function<void(int, Text)> onFinish)
{
    all->setActionCount(9);
    all->reset();
    stageTitle->setText("");
    stepTitle->setText("");
    step->removeStyle(ProgressBar::Style::Visible);
    stage->removeStyle(ProgressBar::Style::Visible);
    new AsynchronCall(
        "Load Menu", [this, client, name, secret, port, onFinish]() {
            Text tmp = secret;
            int result = client->join(name, tmp, port);
            onFinish(result, tmp);
        });
}

Image* loadImage(Framework::Text path)
{
    if (path.beginnsWith("itemType:"))
    {
        Text* typeIdName = path.getTeilText(9);
        for (int i = 0; i < itemTypeCount; i++)
        {
            if (itemTypes[i]->getName().isEqual(*typeIdName))
            {
                Image* img
                    = dynamic_cast<Image*>(itemTypes[i]->zIcon()->getThis());
                typeIdName->release();
                return img;
            }
        }
        typeIdName->release();
        return 0;
    }
    else
    {
        LTDBFile file;
        file.setFile(
            path.getTeilText(0, path.positionOf("/", path.countOf("/") - 1)));
        file.readData(0);
        return file.load(0,
            path.getTeilText(path.positionOf("/", path.countOf("/") - 1) + 1));
    }
}