/* tycoon.c —— 模拟经营（进货 / 卖货 / 过一天）
 * tycoon.c -- business sim (buy stock / sell / advance a day)
 *
 * 玩法：三个按钮——进货（花钱变多的存货）、卖货（清空存货换钱）、过一天（价格随机波动）。
 * Rules: three buttons -- buy (spend cash for stock), sell (turn stock into cash), next day (the price
 * moves randomly).
 * 目标：现金到 1000 就通关。这是"资源循环"最小的那套骨架。
 * Goal: reach 1000 cash. This is the smallest possible resource-loop skeleton.
 */
#include <vml_ui.h>
#include <stdlib.h>
#include <stdio.h>

#define MAX_FRAMES 30000
#define C_BG 0xFF101018

static int SW, SH, g_lang, g_frames;
static int cash = 100, stock = 0, price = 10, day = 1, won;
static int bx[3], by, bw, bh;

static unsigned int seed = 20261130;
static int rnd(int n) { seed = seed * 1103515245u + 12345u; return (int)((seed >> 16) % (unsigned int)n); }

static void relayout(void)
{
    SW = ui_scr_get_w(); SH = ui_scr_get_h();
    if (SW <= 0) SW = 360;
    if (SH <= 0) SH = 620;
    bw = (SW - 40) / 3;
    bh = SH / 10;
    if (bh < 44) bh = 44;
    by = SH - bh - 30;
}

static void draw(void)
{
    const char *names[3] = {"进货", "卖货", "过一天"};
    int i;
    char b[24];

    ui_2d_clear(C_BG);
    itoa(cash, b);
    ui_2d_text(16, 50, b, 0xFFFFD040, 26, VML_ANCHOR_LEFT);
    ui_2d_text(16, 80, g_lang == 0 ? "现金" : "cash", 0xFF9AA0B0, 12, VML_ANCHOR_LEFT);

    itoa(stock, b);
    ui_2d_text(16, 130, b, 0xFF51E86E, 20, VML_ANCHOR_LEFT);
    ui_2d_text(16, 156, g_lang == 0 ? "存货" : "stock", 0xFF9AA0B0, 12, VML_ANCHOR_LEFT);

    itoa(price, b);
    ui_2d_text(16, 206, b, 0xFFFFFFFF, 20, VML_ANCHOR_LEFT);
    ui_2d_text(16, 232, g_lang == 0 ? "今日价" : "price", 0xFF9AA0B0, 12, VML_ANCHOR_LEFT);

    itoa(day, b);
    ui_2d_text(SW - 16, 50, b, 0xFF9AA0B0, 20, VML_ANCHOR_RIGHT);
    ui_2d_text(SW - 16, 76, g_lang == 0 ? "第几天" : "day", 0xFF9AA0B0, 12, VML_ANCHOR_RIGHT);

    for (i = 0; i < 3; i++) {
        ui_2d_rect(16 + i * (bw + 4), by, bw, bh, i == 2 ? 0xFF4A90D9 : 0xFF2A2A36, 1, 0, 10);
        ui_2d_text(16 + i * (bw + 4) + bw / 2, by + bh / 2 + 6, (char *)names[i], 0xFFE8E8F0, 15, VML_ANCHOR_CENTER);
    }
    if (won)
        ui_2d_text(SW / 2, SH / 2 + 60, g_lang == 0 ? "赚到 1000 了！ESC 重开" : "You made 1000! ESC",
                   0xFFFFFFFF, 18, VML_ANCHOR_CENTER);
    ui_2d_present();
}

int main(void)
{
    int msg[4], tid, done = 0, i;

    g_lang = ui_scr_get_language();
    relayout();
    ui_win_open_ex("tycoon.c", 0, 0, VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
    relayout();
    draw();

    tid = ui_timer_set(200, 1);
    while (!done && ui_win_is_closed() == 0) {
        int t = ui_msg_wait(msg, 0);
        if (t == VML_MSG_NONE) continue;

        if (t == VML_MSG_TOUCHDOWN || t == VML_MSG_MOUSEDOWN) {
            for (i = 0; i < 3; i++)
                if (msg[2] >= by && msg[2] <= by + bh && msg[1] >= 16 + i * (bw + 4) && msg[1] <= 16 + i * (bw + 4) + bw) {
                    if (i == 0 && cash >= price) { cash = cash - price; stock = stock + 1; }
                    if (i == 1 && stock > 0) { stock = stock - 1; cash = cash + price; }
                    if (i == 2) {
                        price = 6 + rnd(15);                 /* 每天价格 6..20 */
                        day = day + 1;
                        if (stock > 0) cash = cash - 0;      /* 存货不折旧，简化 */
                    }
                    if (cash >= 1000) won = 1;
                    break;
                }
            draw(); continue;
        }
        if (t == VML_MSG_KEYDOWN) {
            if (msg[1] == VML_KEY_ESCAPE) { cash = 100; stock = 0; price = 10; day = 1; won = 0; draw(); }
            continue;
        }
        if (t == VML_MSG_TIMER) { g_frames = g_frames + 1; if (g_frames >= MAX_FRAMES) done = 1; continue; }
        if (t == VML_MSG_WINDOWRESIZE) { relayout(); draw(); continue; }
        if (t == VML_MSG_WINDOWCLOSE) break;
    }

    ui_timer_kill(tid);
    ui_win_close();
    printf("tycoon: cash=%d stock=%d day=%d\n", cash, stock, day);
    return 0;
}
