// 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/Nodes/CameraNode.hpp>
#include <IAEngine/Nodes/SpriteObjectNode.hpp>
#include <IAEngine/Nodes/TextureObjectNode.hpp>
#include <IAEngine/Nodes/TileMapNode.hpp>

#include <IAEngine/Components/CameraComponent.hpp>
#include <IAEngine/Components/SoundEmitterComponent.hpp>

#include <IAEngine/Scene.hpp>
#include <IAEngine/UI.hpp>
#include <IAEngine/Utils.hpp>

namespace ia::iae
{
    class Engine
    {
      public:
        // Event Functions
        STATIC Handle CreateEvent(IN CONST String &name);
        STATIC VOID DestroyEvent(IN Handle event);
        STATIC Handle GetEventByName(IN CONST String &name);
        STATIC VOID AddEventListener(IN Handle event, IN std::function<VOID()> callback);
        STATIC VOID AddEventListener(IN CONST String &eventName, IN std::function<VOID()> callback);
        STATIC VOID BroadcastEvent(IN Handle event);
        STATIC VOID BroadcastEvent(IN CONST String &eventName);

        // Renderer Functions
        STATIC Handle GetGeometry_Quad();
        STATIC Handle CreateGeometry(IN CONST Vector<GeometryVertex> &vertices, IN CONST Vector<INT32> &indices);
        STATIC VOID DestroyGeometry(IN Handle geometry);
        STATIC VOID DrawGeometry(IN Handle handle);
        STATIC IVec2 GetDisplayExtent();
        STATIC VOID ResizeDisplay(IN INT32 newWidth, IN INT32 newHeight);

        // Renderer State Functions
        STATIC VOID SetRenderState_Scissor(IN IVec4 rect);
        STATIC VOID SetRenderState_FlippedH(IN BOOL value);
        STATIC VOID SetRenderState_FlippedV(IN BOOL value);
        STATIC VOID SetRenderState_TextureOffset(IN Vec2 off);
        STATIC VOID SetRenderState_YSortingEnabled(IN BOOL value);
        STATIC VOID SetRenderState_ColorOverlay(IN Color color);
        STATIC VOID SetRenderState_CameraRelative(IN BOOL value);
        STATIC VOID SetRenderState_Texture(IN Handle image);
        STATIC VOID SetRenderState_Transform(IN Vec2 position, IN Vec2 scale, IN FLOAT32 rotation, IN UINT8 layer,
                                             IN INT16 sortIndex);

        // Debug Draw Functions
        STATIC VOID DebugDraw_SetColor(IN Color color);
        STATIC VOID DebugDraw_SetStrokeWidth(IN FLOAT32 width);
        STATIC VOID DebugDraw_Line(IN Vec2 from, IN Vec2 to);
        STATIC VOID DebugDraw_FillRect(IN Vec2 position, IN Vec2 size);
        STATIC VOID DebugDraw_StrokeRect(IN Vec2 position, IN Vec2 size);

        // Resource Functions
        STATIC Handle CreateSoundFromFile(IN CONST String &name, IN CONST String &path);
        STATIC Handle CreateImageFromFile(IN CONST String &name, IN CONST String &path, IN INT32 resizeToWidth = 0,
                                          IN INT32 resizeToHeight = 0);
        STATIC Handle CreateImage(IN CONST String &name, IN PCUINT8 encodedData, IN SIZE_T encodedDataSize);
        STATIC Handle CreateImage(IN CONST String &name, IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height);
        STATIC Handle CreateSound(IN CONST String &name, IN PCUINT8 encodedData, IN SIZE_T encodedDataSize);
        STATIC Handle GetImage(IN CONST String &name);
        STATIC Handle GetSound(IN CONST String &name);
        STATIC VOID DestroyImage(IN Handle image);
        STATIC VOID DestroySound(IN Handle sound);
        STATIC IVec2 GetImageExtent(IN Handle image);
        STATIC Handle ResizeImage(IN Handle image, IN INT32 newWidth, IN INT32 newHeight);
        STATIC Handle ResizeImage(IN CONST String &name, IN INT32 newWidth, IN INT32 newHeight);
        STATIC VOID RescaleAllImages(IN FLOAT32 factorX, IN FLOAT32 factorY);
        STATIC Handle CombineImages(IN CONST Vector<Handle> &images, IN INT32 unitWidth, IN INT32 unitHeight,
                                    IN INT32 unitCountX, IN INT32 unitCountY);

        // Game Functions
        STATIC VOID SetTimeScale(IN FLOAT32 scale);
        STATIC VOID SetActiveCamera(IN CameraComponent *cameraComponent);
        STATIC CameraComponent *GetActiveCamera();

        // Scene Functions
        STATIC RefPtr<Scene> CreateSceneFromFile(IN CONST String &path);
        STATIC Scene *GetActiveScene();
        STATIC VOID ChangeActiveScene(IN RefPtr<Scene> scene);
        STATIC VOID AddNodeToActiveScene(IN RefPtr<INode> node);
        STATIC INode *GetNodeFromActiveScene(IN CONST String &name);
        STATIC VOID RemoveNodeFromActiveScene(IN CONST String &name);

        // Input Functions
        STATIC Vec2 GetInputAxis();
        STATIC VOID SwitchInputModeToText();
        STATIC VOID SwitchInputModeToAction();
        STATIC Vec2 GetInputPointerPosition();
        STATIC BOOL IsInputKeyDown(IN InputKey key);
        STATIC BOOL WasInputKeyPressed(IN InputKey key);
        STATIC BOOL WasInputKeyReleased(IN InputKey key);
        STATIC BOOL IsInputActionDown(IN Handle action);
        STATIC BOOL WasInputActionPressed(IN Handle action);
        STATIC BOOL WasInputActionReleased(IN Handle action);
        STATIC BOOL IsInputActionDown(IN CONST String &action);
        STATIC BOOL WasInputActionPressed(IN CONST String &action);
        STATIC BOOL WasInputActionReleased(IN CONST String &action);
        STATIC Handle BindInputAction(IN CONST String &name, IN InputKey key);
        STATIC VOID BindInputAxis(IN InputKey upKey, IN InputKey downKey, IN InputKey leftKey, IN InputKey rightKey);

        // Utility Functions
        STATIC Direction GetVectorPointingDirection(IN Vec2 v);
        STATIC Vector<UINT8> ReadBinaryAsset(IN CONST String& path);

        // Random Functions
        STATIC FLOAT32 GetRandomFloat();
        STATIC INT32 GetRandomInRange(IN INT32 min, IN INT32 max);

        // Time Functions
        STATIC INT64 GetTickCount();
        STATIC INT64 GetUnixSecond();
        STATIC INT64 GetUnixMillisecond();
        STATIC FLOAT32 GetFrameDeltaTime();

        // Engine Functions
        STATIC BOOL IsDebugMode();
        STATIC String GetUniqueResourceName();
    };
} // namespace ia::iae