// ChanceModalGame.h
#pragma once
#include "../../lib/game.h"
#include "../../display/low_level_render.h"
#include "../../display/low_level_gui.h"
#include "input_manager.h"
#include "chance.h"
#include "player.h"

#include "MonopolyBoardRenderer.h"
#include "sprites.h"
#include "ModalButtonHelper.h"

class ChanceModalGame : public Game {
    const ChanceCard* card;
    bool dismissed;
    Player* players;
    int players_count;
    int player_pos;

public:
    ChanceModalGame(uint16_t width, uint16_t height, LowLevelRenderer* renderer, LowLevelGUI* gui, InputManager* input_manager, const ChanceCard* c, Player* p_list = nullptr, int p_count = 0, int p_pos = -1)
        : Game(width, height, renderer, gui, input_manager), card(c), dismissed(false), players(p_list), players_count(p_count), player_pos(p_pos) {}

    void init() override { 
        dismissed = false; 
        ModalButtonHelper::set_monopoly_regions(input_manager, width, height);
    }
    Type get_type() const override { return Type::MONOPOLY_CHANCE; }

    bool update(const InputEvent& event) override {
        if (event.type == INPUT_BUTTON_0 || event.type == INPUT_BUTTON_1) {
            dismissed = true;
            return true;
        }
        return false;
    }

    void draw() override {
        renderer->clear_buffer();

        if (players && players_count > 0) {
            MonopolyBoardRenderer::draw_board_perimeter(renderer, width, height, players, players_count, player_pos);
        }

        // --- Inner UI (Center Area) ---
        int cw = width / 7;
        int ch = height / 7;
        int ix = cw + 2, iy = ch + 2;
        int iw = width - 2 * cw - 4, ih = height - 2 * ch - 4;

        // Window background
        renderer->draw_filled_rectangle(ix, iy, iw, ih, false, 0);
        renderer->draw_rectangle(ix, iy, iw, ih, true, 1);

        // Header
        renderer->draw_filled_rectangle(ix + 2, iy + 2, iw - 4, 30, true, 1);
        renderer->set_text_color(false);
        renderer->draw_string_scaled(ix + (iw - 6 * 12) / 2, iy + 10, "CHANCE", 2);
        renderer->set_text_color(true);

        // Card Description (with simple word wrap)
        int text_y = iy + 45;
        int max_chars_per_line = (iw - 10) / 12;
        const char* text = card->description;
        char line[64];
        int start = 0;
        int len = strlen(text);

        while (start < len) {
            int count = 0;
            // Find how many chars fit - very basic wrapping
            while (count < max_chars_per_line && (start + count) < len) {
                count++;
            }
            // Try to find last space if not at end
            if (start + count < len) {
                while (count > 0 && text[start + count] != ' ') count--;
                if (count == 0) count = max_chars_per_line; // fallback
            }
            
            int copy_len = (count < 63) ? count : 63;
            strncpy(line, text + start, copy_len);
            line[copy_len] = '\0';
            renderer->draw_string_scaled(ix + (iw - copy_len * 12) / 2, text_y, line, 2);
            text_y += 18;
            start += count;
            if (text[start] == ' ') start++; // Skip the space
        }

        // Draw Chance sprite
        renderer->draw_bitmap(epd_bitmap_Chance, ix + iw - 105, iy + ih - 105, 100, 100, true);

        // Prompt
        renderer->draw_string_scaled(ix + (iw - 12 * 12) / 2, iy + ih - 20, "Press BUTTON", 2);

        ModalButtonHelper::draw_virtual_buttons(renderer, input_manager);
    }

    bool is_dismissed() const { return dismissed; }
};
