#pragma once
#include "../../display/low_level_render.h"
#include "monopoly_board.h"
#include "player.h"
#include <cstring>

class MonopolyBoardRenderer {
public:
    static void draw_tile(LowLevelRenderer* renderer, int x, int y, int w, int h, int index, bool is_corner, Player* players, int players_count, int orientation = 0, int currentPlayerPos = -1, int observer_idx = -1) {
        if (index < 0 || index >= BOARD_SIZE) return;
        
        // Find owner
        int owner_id = -1;
        for (int i = 0; i < players_count; i++) {
            for (int j = 0; j < players[i].property_count; j++) {
                if (players[i].properties_owned[j] == index) {
                    owner_id = i;
                    break;
                }
            }
            if (owner_id != -1) break;
        }

        bool isInverted = false;
        if (observer_idx != -1) {
            isInverted = (owner_id == observer_idx);
        } else {
            isInverted = (index == currentPlayerPos);
        }

        if (isInverted) {
            renderer->draw_filled_rectangle(x, y, w, h, true, 1);
            renderer->set_text_color(false); // Black text on white background
        } else {
            renderer->draw_rectangle(x, y, w, h, true, 1);
        }
        
        const BoardTile& tile = MONOPOLY_BOARD[index];
        int content_x = x, content_y = y, content_w = w, content_h = h;

        if (!is_corner && tile.type == TILE_PROPERTY) {
            int bar_size = 10;
            int bx = x, by = y, bw = w, bh = h;
            
            if (orientation == 0) { // Bottom row (Bar on top)
                bh = bar_size;
                content_y += bar_size; content_h -= bar_size;
            } else if (orientation == 1) { // Left column (Bar on right)
                bx = x + w - bar_size; bw = bar_size;
                content_w -= bar_size;
            } else if (orientation == 2) { // Top row (Bar on bottom)
                by = y + h - bar_size; bh = bar_size;
                content_h -= bar_size;
            } else if (orientation == 3) { // Right column (Bar on left)
                bw = bar_size;
                content_x += bar_size; content_w -= bar_size;
            }

            if (isInverted) {
                // Background is white, so bar is black
                renderer->draw_filled_rectangle(bx, by, bw, bh, false, 0); 
                renderer->set_text_color(true); // White text
            } else {
                renderer->draw_filled_rectangle(bx, by, bw, bh, true, 1);
                renderer->set_text_color(false); // Black text
            }
            
            // Group number
            char gbuf[2] = { (char)('0' + tile.group[0]), '\0' };
            renderer->draw_string_scaled(bx + (bw - 6) / 2, by + (bh - 8) / 2, gbuf, 1);
            
            if (isInverted) renderer->set_text_color(false); 
            else renderer->set_text_color(true);
        }

        char short_name[10] = {0};
        int s_ptr = 0;
        bool isCurrentPos = (index == currentPlayerPos && observer_idx != -1);

        if (isCurrentPos) short_name[s_ptr++] = '-';
        
        // Add * if owned by someone else
        if (owner_id != -1 && observer_idx != -1 && owner_id != observer_idx) {
            short_name[s_ptr++] = '*';
        }

        const char* full_name = tile.name;
        if (is_corner) {
            int len = strlen(full_name);
            if (len > 3) len = 3;
            for(int i=0; i<len; i++) short_name[s_ptr++] = full_name[i];
        } else {
            short_name[s_ptr++] = full_name[0];
            const char* space = strchr(full_name, ' ');
            if (space && space[1] != '\0') short_name[s_ptr++] = space[1];
        }
        
        if (isCurrentPos) short_name[s_ptr++] = '-';
        short_name[s_ptr] = '\0';

        for (int i = 0; short_name[i]; i++) if(short_name[i] >= 'a' && short_name[i] <= 'z') short_name[i] -= 32;

        renderer->draw_string_scaled(content_x + (content_w - (int)strlen(short_name) * 6) / 2, content_y + (content_h - 8) / 2, short_name, 1);

        // Draw player markers
        int p_count = 0;
        for (int i = 0; i < players_count; ++i) {
            if (players[i].position == index) {
                char mark[2] = { (players[i].token ? players[i].token[0] : 'P'), '\0' };
                renderer->draw_string_scaled(content_x + 2 + (p_count * 8), content_y + 2, mark, 1);
                p_count++;
            }
        }

        if (isInverted) {
            renderer->set_text_color(true); 
        }
    }

    static void draw_board_perimeter(LowLevelRenderer* renderer, int width, int height, Player* players, int players_count, int currentPlayerPos = -1, int observer_idx = -1, int limit_start = -1, int limit_end = -1) {
        int cw = width / 7;   // Corner width
        int ch = height / 7;  // Corner height
        int rw = (width - 2 * cw) / 9;  // Regular tile width
        int rh = (height - 2 * ch) / 9; // Regular tile height

        auto should_draw = [&](int index) {
            if (limit_start == -1 || limit_end == -1) return true;
            if (limit_start <= limit_end) {
                return index >= limit_start && index <= limit_end;
            } else {
                return index >= limit_start || index <= limit_end;
            }
        };

        // --- Bottom Row: 0 to 10 (Right to Left) ---
        if (should_draw(0)) draw_tile(renderer, width - cw, height - ch, cw, ch, 0, true, players, players_count, 0, currentPlayerPos, observer_idx); // GO
        for (int i = 1; i < 10; ++i) {
            if (should_draw(i)) draw_tile(renderer, width - cw - i * rw, height - ch, rw, ch, i, false, players, players_count, 0, currentPlayerPos, observer_idx);
        }
        if (should_draw(10)) draw_tile(renderer, 0, height - ch, cw, ch, 10, true, players, players_count, 1, currentPlayerPos, observer_idx); // JAIL

        // --- Left Column: 11 to 19 (Bottom to Top) ---
        for (int i = 11; i < 20; ++i) {
            if (should_draw(i)) draw_tile(renderer, 0, height - ch - (i - 10) * rh, cw, rh, i, false, players, players_count, 1, currentPlayerPos, observer_idx);
        }

        // --- Top Row: 20 to 30 (Left to Right) ---
        if (should_draw(20)) draw_tile(renderer, 0, 0, cw, ch, 20, true, players, players_count, 2, currentPlayerPos, observer_idx); // FREE PARKING
        for (int i = 21; i < 30; ++i) {
            if (should_draw(i)) draw_tile(renderer, cw + (i - 21) * rw, 0, rw, ch, i, false, players, players_count, 2, currentPlayerPos, observer_idx);
        }
        if (should_draw(30)) draw_tile(renderer, width - cw, 0, cw, ch, 30, true, players, players_count, 3, currentPlayerPos, observer_idx); // GO TO JAIL

        // --- Right Column: 31 to 39 (Top to Bottom) ---
        for (int i = 31; i < 40; ++i) {
            if (should_draw(i)) draw_tile(renderer, width - cw, ch + (i - 31) * rh, cw, rh, i, false, players, players_count, 3, currentPlayerPos, observer_idx);
        }
    }
};
