#include "../resources.h"
#include "../level.h"
#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"

static tween_param_t *title_y_tween;
static tween_param_t *decor_top_y_tween;
static tween_param_t *story_height_tween;
static button_t      *start_button;
static button_t      *back_button;
static button_t      *newgame_button;
static button_t      *options_button;
static button_t      *exit_button;
static SDL_Rect       title_rect;
static SDL_Rect       decor_top_frame;
static SDL_Rect       decor_top_draw;
static SDL_Rect       decor_bottom_frame;
static SDL_Rect       decor_bottom_draw;
static SDL_Rect       story_frame;
static SDL_Rect       story_draw;
static SDL_Rect       developer_draw;

static bool story_show = false;

void Scene_Menu_Init(void)
{
    //Tween_Init();
    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);
    story_height_tween = Tween_ParamCreate(500, 0, 300, EASE_OUT_CUBIC);

    start_button   = UI_Button_Create(btn_startTex, 0, 0, 29, 13, 87, 39);
    back_button    = UI_Button_Create(btn_backTex, 0, 0, 28, 13, 84, 39);

    newgame_button = UI_Button_Create(btn_newgameTex, 0, 0, 45, 13, 135, 39);
    options_button = UI_Button_Create(btn_optionsTex, 0, 0, 43, 13, 129, 39);
    exit_button    = UI_Button_Create(btn_quitTex, 0, 0, 29, 13, 87, 39);

    decor_top_frame    = Utils_GetRect(0, 0, 159, 5);
    decor_bottom_frame = Utils_GetRect(0, 5, 159, 5);
    story_frame        = Utils_GetRect(0, 0, 159, 100);

    story_show = false;

    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) ;


    Level_CreateEmpty(level_width, level_height);


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

    UI_Button_SetPosition(newgame_button, center_x - 67, center_y - 70);
    UI_Button_SetPosition(options_button, center_x - 64, center_y + 4);
    UI_Button_SetPosition(exit_button, center_x - 43, center_y + 78);

    UI_Button_SetPosition(start_button, center_x + 110, center_y);
    UI_Button_SetPosition(back_button, center_x - 194, center_y);

    title_rect        = Utils_GetRect(center_x - 205, 0, 411, 72);
    decor_top_draw    = Utils_GetRect(center_x - 238, 0, 477, 15);
    decor_bottom_draw = Utils_GetRect(center_x - 238, 0, 477, 15);
    story_draw        = Utils_GetRect(center_x - 238, 0, 477, 300);
    developer_draw    = Utils_GetRect(center_x - 91, app_height - 20, 182, 10);

    Tween_ParamRun(title_y_tween);
    Tween_ParamRun(decor_top_y_tween);
}

void Scene_Menu_Update(float dt)
{
    //Tween_Update();
    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();

    if (story_show)
    {
        point_t start_pos = UI_Button_GetPosition(start_button);
        point_t back_pos  = UI_Button_GetPosition(back_button);

        story_draw.h        = (Uint32)Tween_ParamGetValue(story_height_tween);
        story_frame.h       = (int)(story_draw.h / 3);
        story_draw.y        = decor_top_draw.y + 15;
        decor_bottom_draw.y = decor_top_draw.y + story_draw.h + 15;

        start_pos.y = decor_bottom_draw.y + 32;
        back_pos.y  = decor_bottom_draw.y + 32;

        UI_Button_SetPosition(start_button, start_pos.x, start_pos.y);
        UI_Button_SetPosition(back_button, back_pos.x, back_pos.y);

        UI_Button_Update(start_button);
        UI_Button_Update(back_button);

        if (UI_Button_IsClick(back_button))
        {
            Mix_PlayChannel(-1, clickSnd, 0);

            if (Tween_ParamIsFinish(story_height_tween))
                story_show = false;
        }

        if (UI_Button_IsClick(start_button))
        {
            Scene_Game_SetLevel(1);

            Mix_PlayChannel(-1, clickSnd, 0);

            Scene_Set(SCENE_GAME);
        }

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

            if (Tween_ParamIsFinish(story_height_tween))
                story_show = false;
        }
    }
    else
    {
        UI_Button_Update(newgame_button);
        UI_Button_Update(options_button);
        UI_Button_Update(exit_button);

        if (UI_Button_IsClick(newgame_button))
        {
            Mix_PlayChannel(-1, clickSnd, 0);
            Tween_ParamStop(title_y_tween, true);
            Tween_ParamStop(decor_top_y_tween, true);
            Tween_ParamRun(story_height_tween);
            story_show = true;
        }

        if (UI_Button_IsClick(options_button))
        {
            Mix_PlayChannel(-1, clickSnd, 0);
            Scene_Set(SCENE_OPTIONS);
            return;
        }

        if (UI_Button_IsClick(exit_button))
        {
            Mix_PlayChannel(-1, clickSnd, 0);
            Application_Quit();
        }

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

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

    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);

    if (story_show)
    {
        SDL_RenderCopy(renderer, storyTex, &story_frame, &story_draw);
        SDL_RenderCopy(renderer, decor_storyTex, &decor_bottom_frame, &decor_bottom_draw);

        UI_Button_Draw(renderer, start_button);
        UI_Button_Draw(renderer, back_button);
    }
    else
    {
        UI_Button_Draw(renderer, newgame_button);
        UI_Button_Draw(renderer, options_button);
        UI_Button_Draw(renderer, exit_button);
    }

    UI_Cursor_Draw(renderer);
}

void Scene_Menu_Free(void)
{
    Tween_ParamDelete(title_y_tween);
    Tween_ParamDelete(decor_top_y_tween);
    Tween_ParamDelete(story_height_tween);

    UI_Button_Free(start_button);
    UI_Button_Free(back_button);
    UI_Button_Free(newgame_button);
    UI_Button_Free(options_button);
    UI_Button_Free(exit_button);

    FactoryObjects_Clear();
    Level_Destroy();
}
