/*
 * This file is part of the Colobot: Gold Edition source code
 * Copyright (C) 2001-2023, Daniel Roux, EPSITEC SA & TerranovaTeam
 * http://epsitec.ch; http://colobot.info; http://github.com/colobot
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://gnu.org/licenses
 */


#include "ui/controls/shortcut.h"

#include "common/event.h"

#include "graphics/core/renderers.h"
#include "graphics/core/transparency.h"

#include "graphics/engine/engine.h"

#include "math/func.h"

#include <cmath>

namespace Ui
{

// Object's constructor.

CShortcut::CShortcut() : CControl()
{
    m_time = 0.0f;
}

// Object's destructor.

CShortcut::~CShortcut()
{
}


// Creates a new button.

bool CShortcut::Create(const glm::vec2& pos, const glm::vec2& dim, int icon, EventType eventType)
{
    if ( eventType == EVENT_NULL )  eventType = GetUniqueEventType();

    CControl::Create(pos, dim, icon, eventType);
    return true;
}


// Management of an event.

bool CShortcut::EventProcess(const Event &event)
{
    CControl::EventProcess(event);

    if ( event.type == EVENT_FRAME )
    {
        m_time += event.rTime;
    }

    if (event.type == EVENT_MOUSE_BUTTON_DOWN  &&
        event.GetData<MouseButtonEventData>()->button == MOUSE_BUTTON_LEFT)
    {
        if ( CControl::Detect(event.mousePos) )
        {
            m_event->AddEvent(Event(m_eventType));
            return false;
        }
    }

    return true;
}


// Draws the button.

void CShortcut::Draw()
{
    float       zoom;
    int         icon;

    icon = 0;
    zoom = 0.8f;
    Gfx::TransparencyMode mode = Gfx::TransparencyMode::WHITE;
    if ( m_state & STATE_HILIGHT )
    {
        icon = 4;
        zoom = 0.9f;
        mode = Gfx::TransparencyMode::NONE;
    }
    if ( m_state & STATE_CHECK )
    {
        icon = 1;
        zoom = 0.8f;
        mode = Gfx::TransparencyMode::NONE;
    }
    if ( m_state & STATE_PRESS )
    {
        icon = 1;
        zoom = 1.0f;
        mode = Gfx::TransparencyMode::NONE;
    }
    if ( m_icon == 128+6 || m_icon == 128+7 || m_icon == 58 )  // pause or film?
    {
        icon = -1;  // no bottom
        zoom = 1.0f;
    }

    auto renderer = m_engine->GetUIRenderer();

    auto texture = m_engine->LoadTexture("textures/interface/button3.png");
    renderer->SetTexture(texture);

    if ( icon != -1 )
    {
        renderer->SetTransparency(mode);
        DrawVertex(icon, 0.95f);
    }

    icon = SetButtonTextureForIcon(m_icon);
    if (m_icon == 58)
    {
        renderer->SetTransparency(Gfx::TransparencyMode::WHITE);
    }
    else
    {
        renderer->SetTransparency(Gfx::TransparencyMode::BLACK);
    }
    DrawVertex(icon, zoom);

    if ( m_state & STATE_FRAME )
    {
        glm::vec2 p1, p2, c, uv1, uv2;
        float   dp;

        auto texture = m_engine->LoadTexture("textures/interface/button2.png");
        renderer->SetTexture(texture);
        renderer->SetTransparency(Gfx::TransparencyMode::WHITE);

        zoom = 0.9f+sinf(m_time*8.0f)*0.1f;

        p1.x = m_pos.x;
        p1.y = m_pos.y;
        p2.x = m_pos.x + m_dim.x;
        p2.y = m_pos.y + m_dim.y;

        c.x = (p1.x+p2.x)/2.0f;
        c.y = (p1.y+p2.y)/2.0f;  // center

        p1.x = (p1.x-c.x)*zoom + c.x;
        p1.y = (p1.y-c.y)*zoom + c.y;
        p2.x = (p2.x-c.x)*zoom + c.x;
        p2.y = (p2.y-c.y)*zoom + c.y;

        p2.x -= p1.x;
        p2.y -= p1.y;

        uv1.x = 176.0f/256.0f;
        uv1.y = 224.0f/256.0f;
        uv2.x = 192.0f/256.0f;
        uv2.y = 240.0f/256.0f;

        dp = 0.5f/256.0f;
        uv1.x += dp;
        uv1.y += dp;
        uv2.x -= dp;
        uv2.y -= dp;

        DrawIcon(p1, p2, uv1, uv2);
    }

    if ( (m_state & STATE_RUN) && Math::Mod(m_time, 0.7f) >= 0.3f )
    {
        glm::vec2 uv1, uv2;
        float   dp;

        auto texture = m_engine->LoadTexture("textures/interface/button3.png");
        renderer->SetTexture(texture);
        renderer->SetTransparency(Gfx::TransparencyMode::WHITE);

        uv1.x = 160.0f/256.0f;
        uv1.y =   0.0f/256.0f;
        uv2.x = 192.0f/256.0f;
        uv2.y =  32.0f/256.0f;

        dp = 0.5f/256.0f;
        uv1.x += dp;
        uv1.y += dp;
        uv2.x -= dp;
        uv2.y -= dp;

        DrawIcon(m_pos, m_dim, uv1, uv2);
    }
    if ( (m_state & STATE_DAMAGE) && Math::Mod(m_time, 0.7f) >= 0.3f )
    {
        glm::vec2 uv1, uv2;
        float   dp;

        auto texture = m_engine->LoadTexture("textures/interface/button2.png");
        renderer->SetTexture(texture);
        renderer->SetTransparency(Gfx::TransparencyMode::BLACK);

        uv1.x = 159.0f / 256.0f;
        uv1.y = 240.0f / 256.0f;
        uv2.x = 145.0f / 256.0f;
        uv2.y = 256.0f / 256.0f;

        dp = 0.5f / 256.0f;
        uv1.x += dp;
        uv1.y += dp;
        uv2.x -= dp;
        uv2.y -= dp;

        DrawIcon(m_pos, m_dim, uv1, uv2);
    }
}

// Draw the vertex array.

void CShortcut::DrawVertex(int icon, float zoom)
{
    glm::vec2   p1, p2, c;
    float       u1, u2, v1, v2, dp;

    p1.x = m_pos.x;
    p1.y = m_pos.y;
    p2.x = m_pos.x + m_dim.x;
    p2.y = m_pos.y + m_dim.y;

    c.x = (p1.x+p2.x)/2.0f;
    c.y = (p1.y+p2.y)/2.0f;  // center

    p1.x = (p1.x-c.x)*zoom + c.x;
    p1.y = (p1.y-c.y)*zoom + c.y;

    p2.x = (p2.x-c.x)*zoom + c.x;
    p2.y = (p2.y-c.y)*zoom + c.y;

    u1 = (32.0f/256.0f)*(icon%8);
    v1 = (32.0f/256.0f)*(icon/8);  // u-v texture
    u2 = (32.0f/256.0f)+u1;
    v2 = (32.0f/256.0f)+v1;

    dp = 0.5f/256.0f;
    u1 += dp;
    v1 += dp;
    u2 -= dp;
    v2 -= dp;

    auto renderer = m_engine->GetUIRenderer();
    auto vertices = renderer->BeginPrimitive(Gfx::PrimitiveType::TRIANGLE_STRIP, 4);

    vertices[0] = { { p1.x, p1.y }, { u1, v2 } };
    vertices[1] = { { p1.x, p2.y }, { u1, v1 } };
    vertices[2] = { { p2.x, p1.y }, { u2, v2 } };
    vertices[3] = { { p2.x, p2.y }, { u2, v1 } };

    renderer->EndPrimitive();
    m_engine->AddStatisticTriangle(2);
}

}