#include "ui.h"
#include "utils.h"
#include "resources.h"

static SDL_Rect cursor_frame_rect;
static SDL_Rect cursor_draw_rect;

void UI_Cursor_Init(void)
{
    cursor_frame_rect = Utils_GetRect(0, 0, 13, 21);
    cursor_draw_rect = Utils_GetRect(0, 0, 26, 42);
    SDL_ShowCursor(SDL_DISABLE);
}

void UI_Cursor_Update(void)
{
    SDL_GetMouseState(&cursor_draw_rect.x, &cursor_draw_rect.y);

    //cursor origin
    cursor_draw_rect.x -= 8;
    cursor_draw_rect.y -= 4;
}

void UI_Cursor_Draw(SDL_Renderer *renderer)
{
    SDL_RenderCopy(renderer, cursorTex, &cursor_frame_rect, &cursor_draw_rect);
}

button_t* UI_Button_Create(SDL_Texture *texture, const int x, const int y, const int frame_width, const int frame_height, const int width, const int height)
{
    unsigned int i   = 0;
    button_t *button = (button_t*) malloc(sizeof(button_t));

    button->texture = texture;
    button->rect.x  = x;
    button->rect.y  = y;
    button->rect.w  = width;
    button->rect.h  = height;

    for (i = 0; i < UI_BUTTON_COUNT_FRAMES; ++i)
    {
        button->frames[i].x = 0;
        button->frames[i].y = i * frame_height;
        button->frames[i].w = frame_width;
        button->frames[i].h = frame_height;
    }

    button->num_frame = 0;
    button->down      = false;
    button->click     = false;

    return button;
}

point_t UI_Button_GetPosition(button_t *button)
{
    point_t result = Utils_GetPoint(0, 0);

    if (button != NULL)
        result = Utils_GetPoint(button->rect.x, button->rect.y);

    return result;
}

void UI_Button_SetPosition(button_t *button, const int x, const int y)
{
    if (button == NULL)
        return;

    button->rect.x = x;
    button->rect.y = y;
}

bool UI_Button_IsClick(button_t *button)
{
    bool result = false;

    if (button != NULL)
        result = button->click;

    return result;
}

void UI_Button_Update(button_t *button)
{
    if (button == NULL)
        return;

    int  cursor_x  = 0;
    int  cursor_y  = 0;

    SDL_GetMouseState(&cursor_x, &cursor_y);

    button->click = false;

    if ((cursor_x >= button->rect.x) && (cursor_x <= button->rect.x + button->rect.w) &&
        (cursor_y >= button->rect.y) && (cursor_y <= button->rect.y + button->rect.h))
    {
        button->num_frame = 1;

        if (mouse_button.down)
            button->down = true;

        if (mouse_button.up && button->down)
			button->click = true;

        if (mouse_button.press && button->down)
			button->num_frame = 2;
    }
    else button->num_frame = 0;

    if (mouse_button.up)
        button->down = false;
}

void UI_Button_Draw(SDL_Renderer *renderer, button_t *button)
{
    if (button == NULL)
        return;

    SDL_RenderCopy(renderer, button->texture, &button->frames[button->num_frame], &button->rect);
}

void UI_Button_Free(button_t *button)
{
    if (button == NULL)
        return;

    free(button);
    button = NULL;
}

check_t* UI_Check_Create(SDL_Texture *texture, const int x, const int y, const int frame_width, const int frame_height, const int width, const int height, const bool checked)
{
    unsigned int i = 0;
    check_t *check = (check_t*) malloc(sizeof(check_t));

    check->texture = texture;
    check->rect.x  = x;
    check->rect.y  = y;
    check->rect.w  = width;
    check->rect.h  = height;

    for (i = 0; i < UI_CHECK_COUNT_FRAMES; ++i)
    {
        check->frames[i].x = 0;
        check->frames[i].y = i * frame_height;
        check->frames[i].w = frame_width;
        check->frames[i].h = frame_height;
    }

    check->num_frame = 0;
    check->down      = false;
    check->click     = false;
    check->check     = checked;

    return check;
}

point_t UI_Check_GetPosition(check_t *check)
{
    point_t result = Utils_GetPoint(0, 0);

    if (check == NULL)
        result = Utils_GetPoint(check->rect.x, check->rect.y);

    return result;
}

void UI_Check_SetPosition(check_t *check, const int x, const int y)
{
    if (check == NULL)
        return;

    check->rect.x = x;
    check->rect.y = y;
}

bool UI_Check_IsClick(check_t *check)
{
    bool result = false;

    if (check != NULL)
        result = check->click;

    return result;
}

bool UI_Check_IsCheck(check_t *check)
{
    bool result = false;

    if (check != NULL)
        result = check->check;

    return result;
}

void UI_Check_SetCheck(check_t *check, const bool checked)
{
    if (check == NULL)
        return;

    check->check = checked;
}

void UI_Check_Update(check_t *check)
{
    if (check == NULL)
        return;

    int  cursor_x  = 0;
    int  cursor_y  = 0;

    SDL_GetMouseState(&cursor_x, &cursor_y);

    check->click = false;

    if ((cursor_x >= check->rect.x) && (cursor_x <= check->rect.x + check->rect.w) &&
        (cursor_y >= check->rect.y) && (cursor_y <= check->rect.y + check->rect.h))
    {
        if (check->check)
             check->num_frame = 4;
        else check->num_frame = 1;

        if (mouse_button.down)
            check->down = true;

        if (mouse_button.up && check->down)
        {
            check->click = true;

            if (check->check)
                 check->check = false;
            else check->check = true;
        }

        if (mouse_button.press && check->down)
        {
            if (check->check)
                 check->num_frame = 5;
            else check->num_frame = 2;
        }
    }
    else
    {
        if (check->check)
             check->num_frame = 3;
        else check->num_frame = 0;
    }

    if (mouse_button.up)
        check->down = false;
}

void UI_Check_Draw(SDL_Renderer *renderer, check_t *check)
{
    if (check == NULL)
        return;

    SDL_RenderCopy(renderer, check->texture, &check->frames[check->num_frame], &check->rect);
}

void UI_Check_Free(check_t *check)
{
    if (check == NULL)
        return;

    free(check);
    check = NULL;
}
