// TurnModalGame.h
#pragma once
#include "../../lib/game.h"
#include "../../display/low_level_render.h"
#include "../../display/low_level_gui.h"
#include "input_manager.h"
#include "player.h"
#include "ModalButtonHelper.h"
#include <string.h>
#include <stdio.h>

class TurnModalGame : public Game {
    Player* next_player;
    bool dismissed;

public:
    TurnModalGame(uint16_t width, uint16_t height, LowLevelRenderer* renderer, LowLevelGUI* gui, InputManager* input_manager, Player* next)
        : Game(width, height, renderer, gui, input_manager), next_player(next), dismissed(false) {}
    
    void init() override { 
        dismissed = false; 
        ModalButtonHelper::set_monopoly_regions(input_manager, width, height);
    }
    Type get_type() const override { return Type::MONOPOLY_TURN; }
    
    bool update(const InputEvent& event) override {
        if (event.type == INPUT_BUTTON_1) {
            dismissed = true;
            return true;
        }
        return false;
    }
    
    void draw() override {
        renderer->clear_buffer();
        
        // Window background
        int cw = width / 6;
        int ch = height / 6;
        int ix = cw, iy = ch;
        int iw = width - 2 * cw, ih = height - 2 * ch;
        
        renderer->draw_filled_rectangle(ix, iy, iw, ih, false, 0); // White background
        renderer->draw_rectangle(ix, iy, iw, ih, true, 2); // Border
        
        // Header
        renderer->draw_filled_rectangle(ix + 2, iy + 2, iw - 4, 40, true, 1);
        renderer->set_text_color(false);
        renderer->draw_string_scaled(ix + (iw - (int)strlen("NEXT TURN") * 12) / 2, iy + 15, "NEXT TURN", 2);
        renderer->set_text_color(true);

        // Player Name
        char buf[64];
        snprintf(buf, sizeof(buf), "%s", next_player->name);
        int name_len = (int)strlen(buf);
        renderer->draw_string_scaled(ix + (iw - name_len * 18) / 2, iy + 60, buf, 3);

        // Message
        const char* msg = "IT'S YOUR TURN!";
        renderer->draw_string_scaled(ix + (iw - (int)strlen(msg) * 12) / 2, iy + 100, msg, 2);

        const char* submsg = "Please hold the device";
        renderer->draw_string_scaled(ix + (iw - (int)strlen(submsg) * 6) / 2, iy + 130, submsg, 1);

        // Prompt
        const char* prompt = "Press B to start";
        renderer->draw_string_scaled(ix + (iw - (int)strlen(prompt) * 6) / 2, iy + ih - 30, prompt, 1);

        ModalButtonHelper::draw_virtual_buttons(renderer, input_manager);
    }
    
    bool is_dismissed() const { return dismissed; }
};
