#include "../application.h"
#include "../settings.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"

static tween_param_t *title_y_tween;
static tween_param_t *decor_top_y_tween;
static button_t      *ok_button;
static SDL_Rect       title_rect;
static SDL_Rect       decor_top_frame;
static SDL_Rect       decor_top_draw;
static SDL_Rect       end_story_frame;
static SDL_Rect       end_story_draw;
static SDL_Rect       developer_draw;

void Scene_End_Init(void)
{
    Application_SetBGColor(bg_color);
    Scene_Splash_Run(false, 6, bg_color);

    title_y_tween     = Tween_ParamCreate(1500, 0, 50, EASE_OUT_CUBIC);
    decor_top_y_tween = Tween_ParamCreate(900, 0, 50, EASE_OUT_CUBIC);

    ok_button     = UI_Button_Create(btn_okTex, 0, 0, 28, 13, 84, 39);
    decor_top_frame = Utils_GetRect(0, 0, 159, 5);

    int app_width  = Application_GetWidth();
    int app_height = Application_GetHeight();
    int center_x   = app_width / 2;
    int center_y   = app_height / 2;

    int level_width  = (int)((app_width + CELL_WIDTH) / CELL_WIDTH) ;
    int level_height = (int)((app_height + CELL_HEIGHT) / CELL_HEIGHT) ;

    FactoryObjects_Clear();
    Level_Destroy();
    Level_CreateEmpty(level_width, level_height);

    Camera_SetLevelBounds(app_width, app_height);

    Tween_ParamResetValues(title_y_tween, 0, center_y - 220);
    Tween_ParamResetValues(decor_top_y_tween, 72, center_y - 160);

    UI_Button_SetPosition(ok_button, center_x - 42, center_y + 200);

    title_rect      = Utils_GetRect(center_x - 205, 0, 411, 72);
    decor_top_draw  = Utils_GetRect(center_x - 238, 0, 477, 15);
    end_story_frame = Utils_GetRect(0, 0, 480, 300);
    end_story_draw  = Utils_GetRect(center_x - 240, center_y - 120, 480, 300);
    developer_draw  = Utils_GetRect(center_x - 91, app_height - 20, 182, 10);

    Tween_ParamRun(title_y_tween);
    Tween_ParamRun(decor_top_y_tween);

    SDL_WarpMouseInWindow(Application_GetWindow(), center_x, center_y);
}

void Scene_End_Update(float dt)
{
    Camera_Update(dt);
    FactoryObjects_Update(dt);

    title_rect.y     = (Uint32)Tween_ParamGetValue(title_y_tween);
    decor_top_draw.y = (Uint32)Tween_ParamGetValue(decor_top_y_tween);

    UI_Cursor_Update();
    UI_Button_Update(ok_button);

    if (UI_Button_IsClick(ok_button))
    {
        Mix_PlayChannel(-1, clickSnd, 0);
        Scene_Set(SCENE_MENU);
    }

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

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

    SDL_RenderCopy(renderer, end_storyTex, &end_story_frame, &end_story_draw);

    SDL_RenderCopy(renderer, decor_storyTex, &decor_top_frame, &decor_top_draw);
    SDL_RenderCopy(renderer, titleTex, NULL, &title_rect);

    SDL_RenderCopy(renderer, developerTex, NULL, &developer_draw);
    UI_Button_Draw(renderer, ok_button);
    UI_Cursor_Draw(renderer);
}

void Scene_End_Free(void)
{
    Tween_ParamDelete(title_y_tween);
    Tween_ParamDelete(decor_top_y_tween);

    UI_Button_Free(ok_button);

    FactoryObjects_Clear();
    Level_Destroy();
}
