#include "pch.h"

#include "legacy_brush_panel_ui.h"

#include "app.h"
#include "app_core/brush_ui.h"
#include "asset.h"
#include "legacy_ui_overlay_services.h"

Node* NodePanelBrush::clone_instantiate() const
{
    return new NodePanelBrush();
}

void NodePanelBrush::init()
{
    pp::panopainter::LegacyBrushPanelUi::init(*this);
}

kEventResult NodePanelBrush::handle_event(Event* e)
{
    return pp::panopainter::LegacyBrushPanelUi::handle_event(*this, e);
}

void NodePanelBrush::handle_click(Node* target)
{
    pp::panopainter::LegacyBrushPanelUi::handle_click(*this, target);
}

void NodePanelBrush::added(Node* parent)
{
    (void)parent;
    pp::panopainter::LegacyBrushPanelUi::added(*this);
}

namespace pp::panopainter {

void LegacyBrushPanelUi::init(NodePanelBrush& owner)
{
    owner.init_template_file("data/dialogs/panel-brushes.xml", "tpl-panel-brushes");

    owner.m_btn_add = owner.find<NodeButtonCustom>("btn-add");
    owner.m_btn_add->on_click = [&owner](Node*) {
        App::I->pick_file({ "JPG", "PNG" }, [&owner](std::string path) {
            const auto plan = pp::app::plan_brush_texture_list_add(owner.m_dir_name, App::I->data_path, path);
            if (plan) {
                owner.execute_texture_list_plan(plan.value());
            }
        });
    };

    owner.m_btn_remove = owner.find<NodeButtonCustom>("btn-remove");
    owner.m_btn_remove->on_click = [&owner](Node*) {
        if (owner.m_current)
        {
            const int idx = owner.m_container->get_child_index(owner.m_current);
            const auto plan = pp::app::plan_brush_texture_list_remove(
                static_cast<int>(owner.m_container->m_children.size()),
                idx,
                owner.m_current->m_user_brush);
            if (plan) {
                owner.execute_texture_list_plan(plan.value());
            }
        }
    };

    owner.m_btn_up = owner.find<NodeButtonCustom>("btn-up");
    owner.m_btn_up->on_click = [&owner](Node*) {
        if (owner.m_current)
        {
            const int idx = owner.m_container->get_child_index(owner.m_current);
            const auto plan = pp::app::plan_brush_texture_list_move(
                static_cast<int>(owner.m_container->m_children.size()),
                idx,
                -1);
            if (plan) {
                owner.execute_texture_list_plan(plan.value());
            }
        }
    };

    owner.m_btn_down = owner.find<NodeButtonCustom>("btn-down");
    owner.m_btn_down->on_click = [&owner](Node*) {
        if (owner.m_current)
        {
            const int idx = owner.m_container->get_child_index(owner.m_current);
            const auto plan = pp::app::plan_brush_texture_list_move(
                static_cast<int>(owner.m_container->m_children.size()),
                idx,
                1);
            if (plan) {
                owner.execute_texture_list_plan(plan.value());
            }
        }
    };

    owner.m_container = owner.find<NodeScroll>("brushes");

    if (Asset::exist(App::I->data_path + "/settings/" + owner.m_dir_name + ".bin") && !owner.restore())
    {
        auto mb = App::I->message_box("Brushes", "Could not read brush textures file, it will be deleted.", true);
        mb->btn_ok->on_click = [&owner, mb](Node*) {
            Asset::delete_file(App::I->data_path + "/settings/" + owner.m_dir_name + ".bin");
            pp::panopainter::close_legacy_dialog_node(*mb);
        };
        mb->btn_cancel->on_click = [mb](Node*) {
            pp::panopainter::close_legacy_dialog_node(*mb);
        };
    }

    if (owner.m_container->m_children.empty() && !owner.m_dir_name.empty()) {
        owner.scan();
    }
    owner.save();
}

kEventResult LegacyBrushPanelUi::handle_event(NodePanelBrush& owner, Event* event)
{
    switch (event->m_type)
    {
    case kEventType::MouseLeave:
        if (!owner.m_interacted)
            break;
        // else fall through
    case kEventType::MouseUpL:
        if (!owner.m_mouse_inside)
        {
            pp::panopainter::release_legacy_mouse_capture(owner);
            if (owner.m_parent)
            {
                pp::panopainter::detach_legacy_node_from_parent(owner);
            }
            if (owner.on_popup_close)
            {
                owner.on_popup_close(&owner);
            }
        }
        break;
    default:
        return kEventResult::Available;
    }
    return kEventResult::Consumed;
}

void LegacyBrushPanelUi::handle_click(NodePanelBrush& owner, Node* target)
{
    if (target == owner.m_current)
        return;
    if (owner.m_current) {
        owner.m_current->m_selected = false;
    }
    owner.m_current = static_cast<NodeButtonBrush*>(target);
    owner.m_current->m_selected = true;
    if (owner.on_brush_changed) {
        owner.on_brush_changed(&owner, owner.m_container->get_child_index(target));
    }
    owner.m_interacted = true;
}

void LegacyBrushPanelUi::added(NodePanelBrush& owner)
{
    owner.m_interacted = false;
}

} // namespace pp::panopainter
