local MonkeyGames = LibStub("AceAddon-3.0"):GetAddon("MonkeyGames")
local Theme = MonkeyGames.Theme

local BoardWidget = {}
MonkeyGames.BoardWidget = BoardWidget

--- Create an NxN grid of clickable cells.
-- @param parent  Parent frame
-- @param rows    Number of rows
-- @param cols    Number of columns
-- @param cellSize  Pixel size per cell (default 48)
-- @return board  Board object with cells and methods
function BoardWidget:Create(parent, rows, cols, cellSize)
    cellSize = cellSize or 48

    local board = CreateFrame("Frame", nil, parent)
    board:SetSize(cols * cellSize, rows * cellSize)

    board.cells = {}
    board.rows = rows
    board.cols = cols
    board.cellSize = cellSize
    board.onCellClick = nil

    for row = 1, rows do
        board.cells[row] = {}
        for col = 1, cols do
            local cell = CreateFrame("Button", nil, board)
            cell:SetSize(cellSize, cellSize)
            cell:SetPoint("TOPLEFT", board, "TOPLEFT", (col - 1) * cellSize, -(row - 1) * cellSize)

            -- Background texture (for checkerboard coloring)
            local bg = cell:CreateTexture(nil, "BACKGROUND")
            bg:SetAllPoints(cell)
            bg:SetTexture(Theme.TEX_WHITE8X8)

            -- Highlight overlay (for legal moves, selection, etc.)
            local highlight = cell:CreateTexture(nil, "BORDER")
            highlight:SetAllPoints(cell)
            highlight:SetTexture(Theme.TEX_WHITE8X8)
            highlight:Hide()

            -- Content text (piece display)
            local content = cell:CreateFontString(nil, "ARTWORK")
            content:SetFont(Theme.BASE_FONT, Theme.FONT_SIZE_PIECE, "OUTLINE")
            content:SetPoint("CENTER", 0, 0)
            content:SetText("")

            -- Piece texture
            local pieceTexture = cell:CreateTexture(nil, "ARTWORK")
            pieceTexture:SetSize(cellSize * 0.8, cellSize * 0.8)
            pieceTexture:SetPoint("CENTER", 0, 0)
            pieceTexture:Hide()

            -- Secondary content layer (for dots/indicators)
            local indicator = cell:CreateTexture(nil, "ARTWORK", nil, 1)
            indicator:SetSize(cellSize * 0.3, cellSize * 0.3)
            indicator:SetPoint("CENTER", 0, 0)
            indicator:SetTexture(Theme.TEX_WHITE8X8)
            indicator:Hide()

            -- Click handler
            local r, c = row, col
            cell:SetScript("OnClick", function()
                if board.onCellClick then
                    board:onCellClick(r, c)
                end
            end)

            board.cells[row][col] = {
                frame = cell,
                bg = bg,
                content = content,
                highlight = highlight,
                indicator = indicator,
                pieceTexture = pieceTexture,
            }
        end
    end

    return board
end

--- Set the text content and color of a cell.
function BoardWidget:SetCellContent(board, row, col, text, color)
    local cell = board.cells[row] and board.cells[row][col]
    if not cell then return end
    cell.content:SetText(text or "")
    if color then
        cell.content:SetTextColor(unpack(color))
    end
end

--- Set the font size of a cell's content.
function BoardWidget:SetCellFontSize(board, row, col, size)
    local cell = board.cells[row] and board.cells[row][col]
    if not cell then return end
    cell.content:SetFont(Theme.BASE_FONT, size, "OUTLINE")
end

--- Show/hide a highlight overlay on a cell.
function BoardWidget:SetCellHighlight(board, row, col, color)
    local cell = board.cells[row] and board.cells[row][col]
    if not cell then return end
    if color then
        cell.highlight:SetVertexColor(unpack(color))
        cell.highlight:Show()
    else
        cell.highlight:Hide()
    end
end

--- Show a small dot indicator on a cell (for legal move dots).
function BoardWidget:SetCellIndicator(board, row, col, color)
    local cell = board.cells[row] and board.cells[row][col]
    if not cell then return end
    if color then
        cell.indicator:SetVertexColor(unpack(color))
        cell.indicator:Show()
    else
        cell.indicator:Hide()
    end
end

--- Clear all highlights and indicators from the board.
function BoardWidget:ClearAllHighlights(board)
    for row = 1, board.rows do
        for col = 1, board.cols do
            local cell = board.cells[row][col]
            cell.highlight:Hide()
            cell.indicator:Hide()
        end
    end
end

--- Set a texture on a cell (for piece images).
-- @param texturePath  The WoW texture path, or nil to hide
function BoardWidget:SetCellTexture(board, row, col, texturePath)
    local cell = board.cells[row] and board.cells[row][col]
    if not cell then return end
    if texturePath then
        cell.pieceTexture:SetTexture(texturePath)
        cell.pieceTexture:SetTexCoord(0, 1, 0, 1)
        cell.pieceTexture:Show()
    else
        cell.pieceTexture:Hide()
    end
end

--- Set the background color of a cell.
function BoardWidget:SetCellBgColor(board, row, col, color)
    local cell = board.cells[row] and board.cells[row][col]
    if not cell then return end
    cell.bg:SetVertexColor(unpack(color))
end
