#include "pch.h"
#include "log.h"
#include "node_slider.h"
#include "shader.h"

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

void NodeSliderH::clone_copy(Node* dest) const
{
    NodeBorder::clone_copy(dest);
    NodeSliderH* n = static_cast<NodeSliderH*>(dest);
    n->m_value = m_value;
}

void NodeSliderH::init()
{
    SetPadding(1, 1, 1, 1);
    SetWidthP(100);
    SetHeightP(100);
    m_color = glm::vec4(1);
}

void NodeSliderH::draw()
{
    NodeBorder::draw();

    auto sz = GetSize();
    glm::vec2 cur_size = sz * (1.f - m_mask) + m_mask * glm::vec2(10);
    glm::mat4 scale = glm::scale(glm::vec3(cur_size, 1.f));
    glm::mat4 pos = glm::translate(glm::vec3(m_value * m_mask * sz + m_pos + sz * .5f * (1.f - m_mask), 0));
    auto mvp = m_proj * pos * scale;

    ShaderManager::use(kShader::Color);
    ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
    ShaderManager::u_vec4(kShaderUniform::Col, { 0, 0, 0, 1 });
    m_plane.draw_fill();
}

void NodeSliderH::set_value(float value)
{
    m_value = glm::vec2(value) * m_mask;
    //if (on_value_changed)
    //    on_value_changed(this, glm::length(m_value));
}

float NodeSliderH::get_value()
{
    return glm::length(m_value * m_mask);
}

void NodeSliderH::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
{
    NodeBorder::parse_attributes(ka, attr);
    switch (ka)
    {
    case kAttribute::Value:
        m_value = glm::vec2(attr->FloatValue());
        break;
    default:
        break;
    }
}

kEventResult NodeSliderH::handle_event(Event* e)
{
    NodeBorder::handle_event(e);
    switch (e->m_type)
    {
    case kEventType::MouseDownL:
        dragging = true;
        mouse_capture();
        {
            m_old_value = m_value;
            auto sz = GetSize();
            auto pos = glm::clamp(((MouseEvent*)e)->m_pos - m_pos, { 0, 0 }, sz) * m_mask;
            m_value = pos / glm::max({ 1, 1 }, sz);
            if (on_value_changed)
                on_value_changed(this, glm::length(m_value));
        }
        break;
    case kEventType::MouseUpL:
        mouse_release();
        if (dragging && on_value_final)
            on_value_final(this, glm::length(m_value * m_mask));
        dragging = false;
        break;
    case kEventType::MouseMove:
        if (dragging)
        {
            auto sz = GetSize();
            auto pos = glm::clamp(((MouseEvent*)e)->m_pos - m_pos, { 0, 0 }, sz) * m_mask;
            m_value = pos / glm::max({ 1, 1 }, sz);
            if (on_value_changed)
                on_value_changed(this, glm::length(m_value * m_mask));
        }
        break;
    case kEventType::MouseCancel:
        mouse_release();
        if (dragging)
        {
            m_value = m_old_value;
            set_value(glm::length(m_value));
            if (on_value_changed)
                on_value_changed(this, glm::length(m_value * m_mask));
        }
        dragging = false;
        break;
    default:
        return kEventResult::Available;
        break;
    }
    return kEventResult::Consumed;
}

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

void NodeSliderHue::clone_finalize(Node* dest) const
{
    NodeSliderV::clone_finalize(dest);
    NodeSliderHue* n = static_cast<NodeSliderHue*>(dest);
    n->init_controls();
}

void NodeSliderHue::init()
{
    NodeSliderV::init();
    init_controls();
}

void NodeSliderHue::init_controls()
{
    on_value_changed = [this](Node*, float value) {
        m_color = glm::vec4(convert_hsv2rgb({ value, 1, 1 }), 1);
        if (on_hue_changed)
            on_hue_changed(this, m_color);
    };
}

glm::vec4 NodeSliderHue::get_hue()
{
    m_color = glm::vec4(convert_hsv2rgb({ glm::length(m_value * m_mask), 1, 1 }), 1);
    return m_color;
}

void NodeSliderHue::draw()
{
    ShaderManager::use(kShader::ColorHue);
    ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
    //ShaderManager::u_vec4(kShaderUniform::Col, m_color);
    ShaderManager::u_int(kShaderUniform::Direction, 1); // set vertical
    m_plane.draw_fill();
    NodeBorder::m_color = glm::vec4(0);
    NodeSliderH::draw();
}
