#include <AsyncWorldCloner/AsyncWorldCloner.hpp>
#include <Display/WidgetDisplay/MessageDisplay/MessageDisplay.hpp>
#include <Display/WidgetDisplay/WidgetDisplay.hpp>
#include <Display/WidgetDisplay/WorldCloneProgress/WorldCloneProgress.hpp>
#include <Widget/Label/Label.hpp>
#include <Widget/Layout/Center/Center.hpp>
#include <Widget/Layout/Column/Column.hpp>
#include <Widget/Layout/Frame/Frame.hpp>
#include <Widget/Panel/Panel.hpp>
#include <Widget/ProgressBar/ProgressBar.hpp>

WorldCloneProgress::WorldCloneProgress(
    const std::string &oldPath,
    const std::string &newPath,
    bool enableCheats,
    WorldAvatar avatar
) : cloner(oldPath, newPath)
{
    if (cloner.getError().empty())
    {
        cloner.start(enableCheats, avatar);
    }

    setWidget(
        new Center(
            new Frame(
                new Column(    
                    new Label("Cloning world..."),

                    new Panel(glm::ivec2(150, 2)),

                    new ProgressBar([this]() { return cloner.getProgress(); })
                )
            )
        )
    );
}

void WorldCloneProgress::update()
{
    WidgetDisplay::update();

    if (!cloner.isDone())
    {
        return;
    }

    const std::string &error = cloner.getError();
    if (!error.empty())
    {
        Display::setCurrent<MessageDisplay>(Material::alert, "Error", error);
        return;
    }

    Display::setCurrent<MessageDisplay>(Material::accept, "Cloned", "World cloned successfully.");
}