#include "../application.h"
#include "../scenes.h"
#include "../tween.h"
#include "../utils.h"
#include "../resources.h"
#include "../level.h"
#include "../camera.h"
#include "../ui.h"
#include "../factory_objects.h"
#include "../helper.h"
#include "../player.h"

static bool game_pause = false;
static int  game_level = 1;

bool Scene_Game_IsPause(void)
{
    return game_pause;
}

void Scene_Game_Pause(void)
{
    game_pause = true;
}

void Scene_Game_Play(void)
{
    game_pause = false;
}

void Scene_Game_SetLevel(int level)
{
    game_level = level;
}

int Scene_Game_GetLevel(void)
{
    return game_level;
}

void Scene_Game_NextLevel(void)
{
    game_level++;

    if (game_level > 3)
        game_level = 3;

    Scene_Set(SCENE_GAME);
}

void Scene_Game_Init(void)
{
    Application_SetBGColor(bg_color);

    Player_ReInit();

    if (game_level == 1)
        Level_Create("data/level-help.dat");

    if (game_level == 2)
        Level_Create("data/level-main.dat");

    if (game_level == 3)
    {
        Level_CreateEmpty(10, 10);
        Scene_Set(SCENE_END);
    }

    Helper_Init();
    Helper_SetHookShow();
    Helper_SetMoveHorizontal();

    Scene_Splash_Run(false, 6, bg_color);

    game_pause = false;
}

void Scene_Game_Update(float dt)
{
    UI_Cursor_Update();
    Player_Update();

    if (!Scene_Game_IsPause())
    {
        Camera_Update(dt);
        FactoryObjects_Update(dt);
    }

    if (Application_IsKeyDown(KEY_ESC))
    {
        Mix_PlayChannel(-1, clickSnd, 0);
        Player_ToggleVisibilityHudQuestion();
    }
}

void Scene_Game_Draw(SDL_Renderer *renderer)
{
    FactoryObjects_Draw(renderer);

    #ifdef DEBUG_DRAW_LEVEL_COLLISION
    Level_DrawCollisions(renderer);
    #endif // DEBUG_DRAW_LEVEL_COLLISION

    if (game_level == 1)
    {
        SDL_Rect start_help_frame = Utils_GetRect(0, 0, 429, 127);
        SDL_Rect start_help_draw  = Utils_GetRect(1265, 1265, 1287, 381);

        Camera_DrawNotCulling(renderer, start_helpTex, &start_help_frame, &start_help_draw);
    }

    Level_DrawFog(renderer);

    Player_DrawHUD(renderer);
    Helper_Draw(renderer);
    UI_Cursor_Draw(renderer);
}

void Scene_Game_Free(void)
{
    Helper_Quit();
    FactoryObjects_Clear();
    Level_Destroy();
}
