// Ludo Board Map — CORRECT layout matching official Ludo board
// 15x15 grid. Colors: Red(TL), Blue(TR), Green(BL), Yellow(BR)
// Path goes CLOCKWISE starting from Red's entry

// Player order: 0=Red(TL), 1=Green(BL), 2=Yellow(BR), 3=Blue(TR)
// Standard Ludo: clockwise from Red → Green → Yellow → Blue

// The 52 shared squares going CLOCKWISE
// Starting from Red's entry (top-center going down)
export const SHARED_PATH = [
  // Red's start: top-center going DOWN — squares 0-4
  [6,1],[6,2],[6,3],[6,4],[6,5],
  // Turn left across top of Green zone — squares 5-10
  [5,6],[4,6],[3,6],[2,6],[1,6],[0,6],
  // Left corner — square 11
  [0,7],
  // Left side going down (toward Green's start) — squares 12-13
  [0,8],[1,8],
  // GREEN'S START (square 13): bottom-left going RIGHT — squares 13-17
  [2,8],[3,8],[4,8],[5,8],
  // Green's column going DOWN — squares 18-23
  [6,9],[6,10],[6,11],[6,12],[6,13],[6,14],
  // Bottom corner — square 24
  [7,14],
  // Bottom going UP (toward Yellow's start) — squares 25-26
  [8,14],[8,13],
  // YELLOW'S START (square 26): bottom-right going UP — squares 26-30
  [8,12],[8,11],[8,10],[8,9],
  // Turn right across bottom of Blue zone — squares 31-36
  [9,8],[10,8],[11,8],[12,8],[13,8],[14,8],
  // Right corner — square 37
  [14,7],
  // Right side going up (toward Blue's start) — squares 38-39
  [14,6],[13,6],
  // BLUE'S START (square 39): top-right going LEFT — squares 39-43
  [12,6],[11,6],[10,6],[9,6],
  // Blue's column going UP — squares 44-49
  [8,5],[8,4],[8,3],[8,2],[8,1],[8,0],
  // Top corner — square 50
  [7,0],
  // Back to Red's home entry — square 51
  [6,0],
];

// Starting squares for each player (where piece enters the shared path)
// Clockwise: Red=0, Green=13, Yellow=26, Blue=39
export const START_SQUARES = [0, 13, 26, 39];

// Home column paths (6 squares each, going toward center)
export const HOME_COLUMNS = [
  [[7,1],[7,2],[7,3],[7,4],[7,5],[7,6]],     // Red: top-center going down
  [[1,7],[2,7],[3,7],[4,7],[5,7],[6,7]],      // Green: left-center going right
  [[7,13],[7,12],[7,11],[7,10],[7,9],[7,8]],  // Yellow: bottom-center going up
  [[13,7],[12,7],[11,7],[10,7],[9,7],[8,7]],  // Blue: right-center going left
];

// The GLOBAL square just before entering home column
// Red: after square 51 [6,0] → enters home [7,1]
// Green: after square 11 [0,7] → enters home [1,7]
// Yellow: after square 24 [7,14] → enters home [7,13]
// Blue: after square 37 [14,7] → enters home [13,7]
export const HOME_ENTRY = [51, 11, 24, 37];

// Safe squares — entry squares + midpoints between starts
export const SAFE_SQUARES = [0, 8, 13, 21, 26, 34, 39, 47];

// Home base piece positions (where 4 pieces sit before entering the board)
// Order: Red(TL), Green(BL), Yellow(BR), Blue(TR)
export const HOME_BASES = [
  [[1.5,1.5],[3.5,1.5],[1.5,3.5],[3.5,3.5]],           // Red (top-left)
  [[1.5,10.5],[3.5,10.5],[1.5,12.5],[3.5,12.5]],       // Green (bottom-left)
  [[10.5,10.5],[12.5,10.5],[10.5,12.5],[12.5,12.5]],   // Yellow (bottom-right)
  [[10.5,1.5],[12.5,1.5],[10.5,3.5],[12.5,3.5]],       // Blue (top-right)
];

// Convert grid [col,row] to pixel center
export function gridToPixel(col, row, cellSize) {
  return {
    x: col * cellSize + cellSize / 2,
    y: row * cellSize + cellSize / 2
  };
}

// Get pixel position for a piece given its local position and player index
export function getPiecePosition(localPos, playerIdx, cellSize) {
  if (localPos === -1) return null; // In home base

  if (localPos >= 52) {
    // In home column (positions 52-57)
    const homeIdx = localPos - 52;
    if (homeIdx < 6) {
      const [col, row] = HOME_COLUMNS[playerIdx][homeIdx];
      return gridToPixel(col, row, cellSize);
    }
    // Finished — center of board
    return gridToPixel(7, 7, cellSize);
  }

  // On shared path — convert local position to global
  const globalPos = (localPos + START_SQUARES[playerIdx]) % 52;
  const [col, row] = SHARED_PATH[globalPos];
  return gridToPixel(col, row, cellSize);
}

// Get pixel position for a piece still in home base
export function getHomeBasePosition(playerIdx, pieceIdx, cellSize) {
  const [col, row] = HOME_BASES[playerIdx][pieceIdx];
  return gridToPixel(col, row, cellSize);
}
