#include "pch.h"
#include "app.h"
#include "util.h"
#include "node_progress_bar.h"
#include "node_dialog_cloud.h"

void App::cloud_upload()
{
    if (!canvas)
        return;
    if (Canvas::I->m_newdoc)
    {
        auto msgbox = new NodeMessageBox();
        msgbox->m_manager = &layout;
        msgbox->init();
        msgbox->m_title->set_text("Warning");
        msgbox->m_message->set_text("This document needs to be saved before upload.");
        layout[main_id]->add_child(msgbox);
        layout[main_id]->update();
    }
    else
    {
        auto upload_thread = [this] {
            if (Canvas::I->m_unsaved)
            {
                Canvas::I->project_save_thread(doc_path);
            }
            
            async_start();
            auto pb = show_progress("Uploading");
            async_redraw();
            async_end();

            upload(doc_path, doc_filename, [this,pb](float p){
                async_start();
                pb->m_progress->SetWidthP(p * 100.f);
                async_redraw();
                async_end();
            });
            
            async_start();
            pb->destroy();
            auto msgbox = new NodeMessageBox();
            msgbox->m_manager = &layout;
            msgbox->init();
            msgbox->m_title->set_text("Success");
            msgbox->m_message->set_text("This document has been succesfully uploaded.");
            layout[main_id]->add_child(msgbox);
            layout[main_id]->update();
            async_redraw();
            async_end();
        };

        auto m = layout[main_id]->add_child<NodeMessageBox>();
        m->m_title->set_text("Publish document");
        m->m_message->set_text("Would you like to upload to the public domain?");
        m->btn_ok->m_text->set_text("Yes");
        m->btn_cancel->m_text->set_text("No");
        m->btn_ok->on_click = [this, m, upload_thread](Node*) {
            std::thread(upload_thread).detach();
            async_start();
            m->destroy();
            async_end();
        };
        m->btn_cancel->on_click = [this, m, upload_thread](Node*) {
            async_start();
            m->destroy();
            async_end();
        };

    }
}

void App::cloud_upload_all()
{
    std::thread([this] {
        auto names = Asset::list_files(data_path, false, ".*\\.ppi");

        gl_state gl;
        std::shared_ptr<NodeProgressBar> pb;
        if (layout.m_loaded)
        {
            async_start();
            pb = show_progress("Export Pano Image");
            async_redraw();
            async_end();
        }

        int progress = 0;
        int total = (int)names.size();

        for (const auto& n : names)
        {
            std::string path = data_path + "/" + n;
            upload(path);

            progress++;
            float p = (float)progress / total * 100.f;
            LOG("progress: %f", p);

            if (layout.m_loaded)
            {
                async_start();
                pb->m_progress->SetWidthP(p);
                gl.save();
                async_redraw();
                gl.restore();
                async_end();
            }
        }

        if (layout.m_loaded)
        {
            async_start();
            pb->destroy();
            async_redraw();
            async_end();
        }
    }).detach();
}

void App::cloud_browse()
{
    if (!canvas)
        return;

    // load thumbnail test
    auto dialog = std::make_shared<NodeDialogCloud>();
    dialog->m_manager = &layout;
    dialog->init();
    dialog->create();
    dialog->loaded();

    layout[main_id]->add_child(dialog);
    layout[main_id]->update();

    dialog->btn_ok->on_click = [this, dialog](Node*)
    {
        if (dialog->selected_file.empty())
            return;
        dialog->destroy();
        std::thread([this, dialog] {
            async_start();
            auto* m = layout[main_id]->add_child<NodeMessageBox>();
            m->m_title->set_text("Downloading");
            m->m_message->set_text("Download in progress");
            async_redraw();
            async_end();
            std::string url = "https://panopainter.com/cloud/cloud-dwl.php?file=" + dialog->selected_file;
            download(url, dialog->selected_path, [this,m](float p){
                static char progress[256];
                sprintf(progress, "Download in progress %.2f%%", p * 100.f);
                async_start();
                m->m_message->set_text(progress);
                async_redraw();
                async_end();
            });
            
            async_start();
            canvas->reset_camera();
            layers->clear();
            async_end();
            
            canvas->m_canvas->project_open_thread(dialog->selected_path);
            
            async_start();
            doc_name = dialog->selected_name;
            title_update();
            for (auto& i : canvas->m_canvas->m_order)
                layers->add_layer(canvas->m_canvas->m_layers[i]->m_name.c_str());
            ActionManager::clear();
            m->destroy();
            async_redraw();
            async_end();
        }).detach();
    };
}
