#include "eduwin.h"

static HWND canvas;
static int score = 0;
static int target_x, target_y, target_w, target_h;
static int target_r, target_g, target_b;
static int target_type; /* 0=rect, 1=ellipse */
static int target_alive = 0;
static int game_running = 0;

static void spawn_target(void)
{
    int cw = canvas_get_width(canvas);
    int ch = canvas_get_height(canvas);
    if (cw < 60 || ch < 60)
        return;

    target_w = rand_int(40, 80);
    target_h = rand_int(40, 80);
    target_x = rand_int(0, cw - target_w);
    target_y = rand_int(0, ch - target_h);
    target_r = rand_int(50, 255);
    target_g = rand_int(50, 255);
    target_b = rand_int(50, 255);
    target_type = rand_int(0, 1);
    target_alive = 1;
}

static void draw_scene(void)
{
    int cw = canvas_get_width(canvas);
    int ch = canvas_get_height(canvas);

    /* 背景 */
    canvas_set_brush(canvas, 30, 30, 35, 255);
    canvas_fill_rect(canvas, 0, 0, cw, ch);

    /* 得分始终显示在左上角 */
    canvas_draw_text(canvas, L"得分", 10, 10, 255, 255, 255, 255, 16);
    canvas_draw_text(canvas, itow(score), 10, 32, 255, 255, 0, 255, 24);

    if (game_running)
    {
        /* 目标 */
        if (target_alive)
        {
            canvas_set_pen(canvas, target_r / 2, target_g / 2, target_b / 2, 255, 3);
            canvas_set_brush(canvas, target_r, target_g, target_b, 255);
            if (target_type == 0)
                canvas_fill_rect(canvas, target_x, target_y, target_w, target_h);
            else
                canvas_fill_ellipse(canvas, target_x, target_y, target_w, target_h);
        }
    }
    else
    {
        if (score == 0)
            canvas_draw_text(canvas, L"点击画布开始游戏，ESC暂停",
                             cw / 2 - 130, ch / 2 - 10,
                             0, 200, 255, 255, 18);
        else
            canvas_draw_text(canvas, L"点击画布继续",
                             cw / 2 - 60, ch / 2 - 10,
                             0, 200, 255, 255, 18);
    }
}

static void on_timer(void *data)
{
    if (!target_alive)
        spawn_target();
    draw_scene();
}

static void on_mouse_down(int btn, int x, int y)
{
    if (!game_running)
    {
        game_running = 1;
        canvas_start_timer(canvas, 60, on_timer, NULL);
        if (score == 0)
            spawn_target();
        play_beep(880, 150);
        return;
    }

    if (!target_alive)
        return;

    if (x >= target_x && x <= target_x + target_w &&
        y >= target_y && y <= target_y + target_h)
    {
        score++;
        target_alive = 0;
        play_beep(1200, 80);
    }
}

static void on_key_down(int vkey)
{
    if (vkey == VK_ESCAPE && game_running)
    {
        game_running = 0;
        canvas_stop_timer(canvas);
        draw_scene();
        play_beep(440, 200);
    }
}

static void on_resize(int w, int h)
{
    draw_scene();
}

static int on_close(void *data)
{
    if (confirm(L"退出", L"确定要退出游戏吗？"))
    {
        quit_app();
        return 1;
    }
    return 0;
}

int main(void)
{
    if (init_app() != 0)
    {
        alert(L"错误", L"eduwin 初始化失败");
        return 1;
    }

    HWND win = create_window(L"eduwin 打靶游戏 — 点击形状得分", 700, 550);
    if (!win)
    {
        alert(L"错误", L"创建窗口失败");
        return 1;
    }
    on_window_close(win, on_close, NULL);
    on_window_resize(win, on_resize);

    log_clear();

    canvas = create_canvas(win, 0, 0, 700, 550);

    canvas_on_mouse(canvas, NULL, on_mouse_down, NULL);
    canvas_on_key(canvas, on_key_down, NULL);

    show_window(win);
    run_app();
    return 0;
}
