#pragma once
#include "node_border.h"
#include "node_button.h"
#include "node_image_texture.h"
#include "node_text.h"
#include "node_text_input.h"

#include <vector>
#include <stop_token>
#include <thread>

class NodeDialogCloudItem : public NodeBorder
{
public:
    NodeText* m_text;
    NodeImageTexture* m_thumb;
    glm::vec4 m_color_normal = glm::vec4(.4, .4, .4, 1);
    glm::vec4 m_color_selected = glm::vec4(.3, .3, .3, 1);
    glm::vec4 m_color_hover = glm::vec4(.5, .5, .5, 1);
    bool m_selected = false;
    std::string m_path;
    std::string m_file_name;
    std::function<void(NodeDialogCloudItem* target)> on_selected;
    virtual Node* clone_instantiate() const override;
    virtual void clone_finalize(Node* dest) const override;
    virtual void init() override;
    void init_controls();
    virtual void loaded() override;
    virtual void draw() override;
    virtual kEventResult handle_event(Event* e) override;
};

class NodeDialogCloud : public NodeBorder
{
public:
    bool closed = false;
    NodeButton* btn_cancel;
    NodeButton* btn_ok;
    NodeButton* btn_delete;
    NodeDialogCloudItem* current = nullptr;
    Node* container;
    std::string selected_path;
    std::string selected_file;
    std::string selected_name;
    virtual Node* clone_instantiate() const override;
    virtual void clone_finalize(Node* dest) const override;
    virtual void init() override;
    void init_controls();
    virtual void loaded() override;
    virtual void removed(Node* parent) override;
    void load_thumbs_thread(std::stop_token stop);
    NodeText* create_loading_status_text();
    bool load_cloud_file_list(CURL* curl, std::string& response, NodeText& status_text);
    std::vector<NodeDialogCloudItem*> create_cloud_file_items(const std::vector<std::string>& names);

private:
    std::jthread load_thumbs_worker_;
};
