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

Plane NodeImage::m_plane;
Sampler NodeImage::m_sampler;
Sampler NodeImage::m_sampler_mips;

void NodeImage::static_init()
{
    m_plane.create<1>(1, 1);
    m_sampler.create();
    m_sampler_mips.create();
    m_sampler_mips.set_filter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
}

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

void NodeImage::clone_copy(Node* dest) const
{
    Node::clone_copy(dest);
    NodeImage* n = static_cast<NodeImage*>(dest);
    n->m_use_atlas = m_use_atlas;
    n->m_region = m_region;
    n->m_off = m_off;
    n->m_sz = m_sz;
    n->m_path = m_path;
    n->m_tex_id = m_tex_id;
}

void NodeImage::create()
{
    Node::create();
    if (!m_path.empty() && TextureManager::load(m_path.c_str(), m_use_mipmaps))
    {
        //LOG("load image node %s", m_path.c_str());
        auto tex_sz = TextureManager::get(m_tex_id).size();
        m_off = xy(m_region) / tex_sz;
        m_sz = (zw(m_region) - xy(m_region)) / tex_sz;
        if (m_autosize)
            SetAspectRatio(tex_sz.x / tex_sz.y);
    }
}

void NodeImage::restore_context()
{
    Node::restore_context();
    create();
}

void NodeImage::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
{
    Node::parse_attributes(ka, attr);
    switch (ka)
    {
    case kAttribute::Mips:
        m_use_mipmaps = attr->BoolValue();
        break;
    case kAttribute::Path:
        m_path = attr->Value();
        m_tex_id = const_hash(attr->Value());
        break;
    case kAttribute::Region:
    {
        glm::vec4 v;
        int n = sscanf(attr->Value(), "%f %f %f %f", &v.x, &v.y, &v.z, &v.w);
        if (n == 4)
        {
            m_region = v;
            m_use_atlas = true;
        }
        break;
    }
    case kAttribute::AutoSize:
        m_autosize = attr->BoolValue();
        break;
    default:
        break;
    }
}

void NodeImage::draw()
{
    TextureManager::get(m_tex_id).bind();
    auto& sampler = m_use_mipmaps ? m_sampler_mips : m_sampler;
    sampler.bind(0);
    glEnable(GL_BLEND);
    if (m_use_atlas)
    {
        ShaderManager::use(kShader::Atlas);
        ShaderManager::u_vec2(kShaderUniform::Tof, m_off);
        ShaderManager::u_vec2(kShaderUniform::Tsz, m_sz);
    }
    else
    {
        ShaderManager::use(kShader::Texture);
    }
    ShaderManager::u_int(kShaderUniform::Tex, 0);
    ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
    m_plane.draw_fill();
    sampler.unbind();
    TextureManager::get(m_tex_id).unbind();
    glDisable(GL_BLEND);
}

bool NodeImage::set_image(const std::string& path)
{
    m_path = path;
    m_tex_id = const_hash(path.c_str());
    if (!m_path.empty() && TextureManager::load(m_path.c_str(), m_use_mipmaps))
    {
        auto tex_sz = TextureManager::get(m_tex_id).size();
        m_off = xy(m_region) / tex_sz;
        m_sz = (zw(m_region) - xy(m_region)) / tex_sz;
        if (m_autosize)
            SetAspectRatio(tex_sz.x / tex_sz.y);
        return true;
    }
    return false;
}
