#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      *dec_button;
static button_t      *inc_button;
static button_t      *back_button;
static check_t       *fullscreen_check;
static SDL_Rect       title_rect;
static SDL_Rect       decor_top_frame;
static SDL_Rect       decor_top_draw;
static SDL_Rect       volume_board_frame_rect;
static SDL_Rect       volume_board_draw_rect;
static SDL_Rect       volume_frame_rect;
static SDL_Rect       volume_draw_rect;
static SDL_Rect       label_volume_rect;
static SDL_Rect       developer_draw;

static void _Scene_Options_Resize(void)
{
    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) ;

    //Camera_Init();
    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(dec_button, center_x - 105, center_y - 78);
    UI_Button_SetPosition(inc_button, center_x + 72, center_y - 78);

    UI_Check_SetPosition(fullscreen_check, center_x - 105, center_y - 24);
    UI_Button_SetPosition(back_button, center_x - 42, center_y + 39);

    title_rect             = Utils_GetRect(center_x - 205, 0, 411, 72);
    decor_top_draw         = Utils_GetRect(center_x - 238, 0, 477, 15);
    volume_board_draw_rect = Utils_GetRect(center_x - 66, center_y - 75, 132, 27);
    volume_draw_rect       = Utils_GetRect(center_x - 63, center_y - 72, 126, 18);
    label_volume_rect      = Utils_GetRect(center_x - 55, center_y - 96, 111, 15);
    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_Options_Init(void)
{
    bool fullscreen = Settings_GetFullscreen();

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

    dec_button       = UI_Button_Create(btn_decTex, 0, 0, 11, 11, 33, 33);
    inc_button       = UI_Button_Create(btn_incTex, 0, 0, 11, 11, 33, 33);
    back_button      = UI_Button_Create(btn_backTex, 0, 0, 28, 13, 84, 39);
    fullscreen_check = UI_Check_Create(check_fullscreenTex, 0, 0, 61, 11, 183, 33, fullscreen);

    decor_top_frame         = Utils_GetRect(0, 0, 159, 5);
    volume_board_frame_rect = Utils_GetRect(0, 0, 44, 9);
    volume_frame_rect       = Utils_GetRect(1, 10, 42, 6);

    // ставит все на свои места, относительно размера экрана
    _Scene_Options_Resize();
}

void Scene_Options_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();
    UI_Button_Update(inc_button);
    UI_Button_Update(dec_button);
    UI_Button_Update(back_button);
    UI_Check_Update(fullscreen_check);

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

    if (UI_Button_IsClick(dec_button))
    {

        int volume = Settings_GetVolume();

        volume -= SPEED_VOLUME_CHANGE;

        if (volume < 0)
            volume = 0;

        Mix_Volume(-1, volume);

        Settings_SetVolume(volume);
        Settings_Save();

        Mix_PlayChannel(-1, clickSnd, 0);
    }

    if (UI_Button_IsClick(inc_button))
    {
        int volume = Settings_GetVolume();

        volume += SPEED_VOLUME_CHANGE;

        // MIX_MAX_VOLUME = 128
        if (volume > 126)
            volume = 126;

        Mix_Volume(-1, volume);

        Settings_SetVolume(volume);
        Settings_Save();

        Mix_PlayChannel(-1, clickSnd, 0);
    }

    if (UI_Check_IsClick(fullscreen_check))
    {
        if (UI_Check_IsCheck(fullscreen_check))
        {
            Application_SetFullscreen();
            Settings_SetFullscreen(true);
        }
        else
        {
            Application_SetWindowed();
            Settings_SetFullscreen(false);
        }

        _Scene_Options_Resize();
        Settings_Save();
        Mix_PlayChannel(-1, clickSnd, 0);
    }

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

    volume_draw_rect.w = Settings_GetVolume();
}

void Scene_Options_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, bar_volumeTex, &volume_board_frame_rect, &volume_board_draw_rect);
    SDL_RenderCopy(renderer, bar_volumeTex, &volume_frame_rect, &volume_draw_rect);
    SDL_RenderCopy(renderer, label_volumeTex, NULL, &label_volume_rect);
    SDL_RenderCopy(renderer, developerTex, NULL, &developer_draw);

    UI_Button_Draw(renderer, dec_button);
    UI_Button_Draw(renderer, inc_button);
    UI_Button_Draw(renderer, back_button);
    UI_Check_Draw(renderer, fullscreen_check);

    UI_Cursor_Draw(renderer);
}

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

    UI_Button_Free(dec_button);
    UI_Button_Free(inc_button);
    UI_Button_Free(back_button);
    UI_Check_Free(fullscreen_check);

    FactoryObjects_Clear();
    Level_Destroy();
    //Camera_Quit();
    //Tween_Free();
}
