// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// 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 <https://www.gnu.org/licenses/>.

#pragma once

#include <IAEngine/Components/TextureComponent.hpp>

namespace ia::iae
{
    class SpriteComponent : public TextureComponent
    {
      public:
        struct AnimationKeyFrame
        {
            Handle Texture;
            INT32 Duration{100};
            BOOL ShouldInterpolate{};

            FLOAT32 Rotation{};
            Vec2 Position{};
            Vec2 Scale{1.0f, 1.0f};
            Color ColorOverlay{0xFF, 0xFF, 0xFF, 0xFF};
        };

        struct Animation
        {
            BOOL ShouldLoop{false};
            Vector<AnimationKeyFrame> Keys;
        };

      public:
        SpriteComponent(IN Node2D *node);

      public:
        VIRTUAL VOID Draw();
        VIRTUAL VOID DebugDraw();

        VIRTUAL VOID Update();
        VIRTUAL VOID FixedUpdate();

      public:
        Handle AddAnimation(IN CONST Animation &animation);

        VOID SetActiveAnimation(IN Handle animation);
        VOID ForceSetActiveAnimation(IN Handle animation);

        VOID SetAnimationFrame(IN INT32 animation, IN INT32 frame);

      public:
        Vector<Animation> &Animations()
        {
            return m_animations;
        }

        CONST Vector<Animation> &Animations() CONST
        {
            return m_animations;
        }

      private:
        VOID UpdateAnimation();

        AnimationKeyFrame GetKeyFrame(IN INT32 index);

      private:
        FLOAT32 m_timelinePosition{};
        Animation m_activeAnimation{};
        Handle m_activeAnimationHandle{INVALID_HANDLE};
        Vector<Animation> m_animations;
        AnimationKeyFrame m_currentAnimationState{};
        AnimationKeyFrame m_nextAnimationKeyFrame{};
        AnimationKeyFrame m_prevAnimationKeyFrame{};
        INT32 m_prevAnimationKeyFrameIndex{};
    };
} // namespace ia::iae