// Ludo Board Map — Standard layout matching official Ludo board
// 15x15 grid. GREEN(TL), YELLOW(TR), RED(BL), BLUE(BR)
// Path goes CLOCKWISE. Player order: 0=Red(BL), 1=Green(TL), 2=Yellow(TR), 3=Blue(BR)

// The 52 shared squares going CLOCKWISE
// Starting from Red's entry (bottom center, going UP)
export const SHARED_PATH = [
  // Red enters: UP through bottom arm left edge — squares 0-4
  [6,13],[6,12],[6,11],[6,10],[6,9],
  // LEFT across left arm bottom edge — squares 5-10
  [5,8],[4,8],[3,8],[2,8],[1,8],[0,8],
  // Left edge corner — square 11
  [0,7],
  // RIGHT across left arm top edge — squares 12-17
  [0,6],[1,6],[2,6],[3,6],[4,6],[5,6],
  // UP through top arm left edge — squares 18-23
  [6,5],[6,4],[6,3],[6,2],[6,1],[6,0],
  // Top edge corner — square 24
  [7,0],
  // DOWN through top arm right edge — squares 25-30
  [8,0],[8,1],[8,2],[8,3],[8,4],[8,5],
  // RIGHT across right arm top edge — squares 31-36
  [9,6],[10,6],[11,6],[12,6],[13,6],[14,6],
  // Right edge corner — square 37
  [14,7],
  // LEFT across right arm bottom edge — squares 38-43
  [14,8],[13,8],[12,8],[11,8],[10,8],[9,8],
  // DOWN through bottom arm right edge — squares 44-49
  [8,9],[8,10],[8,11],[8,12],[8,13],[8,14],
  // Bottom edge corner — square 50
  [7,14],
  // Last square before Red wraps — square 51
  [6,14],
];

// Starting squares for each player (where piece lands on rolling 6)
// 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,13],[7,12],[7,11],[7,10],[7,9],[7,8]],   // Red: bottom center going UP
  [[1,7],[2,7],[3,7],[4,7],[5,7],[6,7]],       // Green: left center going RIGHT
  [[7,1],[7,2],[7,3],[7,4],[7,5],[7,6]],       // Yellow: top center going DOWN
  [[13,7],[12,7],[11,7],[10,7],[9,7],[8,7]],   // Blue: right center going LEFT
];

// The GLOBAL square at local position 51 (just before entering home column)
// Red: [6,14], Green: [0,6], Yellow: [8,0], Blue: [14,8]
export const HOME_ENTRY = [51, 12, 25, 38];

// Safe squares — each player's start + midpoints
export const SAFE_SQUARES = [0, 8, 13, 21, 26, 34, 39, 47];

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

  if (localPos >= 52) {
    const homeIdx = localPos - 52;
    if (homeIdx < 6) {
      const [col, row] = HOME_COLUMNS[playerIdx][homeIdx];
      return gridToPixel(col, row, cellSize);
    }
    return gridToPixel(7, 7, cellSize);
  }

  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);
}
