// src/srs_tables.mbt

// SRS (Super Rotation System) Wall Kick Data
// 旋转状态: 0=Up, 1=Right, 2=Down, 3=Left
// Kick 测试顺序数据结构: 数组包含 5 次测试,每次包含 (dx, dy) 偏移

// 获取 J, L, S, T, Z 的踢墙测试数据
// state_from: 原旋转状态, state_to: 新旋转状态
// 返回: 5个测试偏移量组成的数组
pub fn get_srs_kick_data(state_from : Int, state_to : Int) -> Array[(Int, Int)] {
  match (state_from, state_to) {
    // 0 -> 1 (0 -> R)
    (0, 1) => [(0, 0), (-1, 0), (-1, 1), (0, -2), (-1, -2)]
    // 1 -> 0 (R -> 0)
    (1, 0) => [(0, 0), (1, 0), (1, -1), (0, 2), (1, 2)]
    // 1 -> 2 (R -> 2)
    (1, 2) => [(0, 0), (1, 0), (1, -1), (0, 2), (1, 2)]
    // 2 -> 1 (2 -> R)
    (2, 1) => [(0, 0), (-1, 0), (-1, 1), (0, -2), (-1, -2)]
    // 2 -> 3 (2 -> L)
    (2, 3) => [(0, 0), (1, 0), (1, 1), (0, -2), (1, -2)]
    // 3 -> 2 (L -> 2)
    (3, 2) => [(0, 0), (-1, 0), (-1, -1), (0, 2), (-1, 2)]
    // 3 -> 0 (L -> 0)
    (3, 0) => [(0, 0), (-1, 0), (-1, -1), (0, 2), (-1, 2)]
    // 0 -> 3 (0 -> L)
    (0, 3) => [(0, 0), (1, 0), (1, 1), (0, -2), (1, -2)]
    // Default fallback (should not happen for valid 90-degree rotations)
    _ => [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]
  }
}

// 获取 I 方块专用的踢墙测试数据
// I 方块由于其对称中心不同,使用了不同的踢墙表
pub fn get_srs_kick_data_i(state_from : Int, state_to : Int) -> Array[(Int, Int)] {
  match (state_from, state_to) {
    // 0 -> 1
    (0, 1) => [(0, 0), (-2, 0), (1, 0), (-2, -1), (1, 2)]
    // 1 -> 0
    (1, 0) => [(0, 0), (2, 0), (-1, 0), (2, 1), (-1, -2)]
    // 1 -> 2
    (1, 2) => [(0, 0), (-1, 0), (2, 0), (-1, 2), (2, -1)]
    // 2 -> 1
    (2, 1) => [(0, 0), (1, 0), (-2, 0), (1, -2), (-2, 1)]
    // 2 -> 3
    (2, 3) => [(0, 0), (2, 0), (-1, 0), (2, 1), (-1, -2)]
    // 3 -> 2
    (3, 2) => [(0, 0), (-2, 0), (1, 0), (-2, -1), (1, 2)]
    // 3 -> 0
    (3, 0) => [(0, 0), (1, 0), (-2, 0), (1, -2), (-2, 1)]
    // 0 -> 3
    (0, 3) => [(0, 0), (-1, 0), (2, 0), (-1, 2), (2, -1)]
    // Default fallback
    _ => [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]
  }
}

// T-Spin 判断专用的四个角的相对坐标 (以 T 方块的中心为 (0,0))
// 格式: (dx, dy)
pub fn get_t_spin_corners() -> Array[(Int, Int)] {
  [
    (-1, -1), // top-left
    (1, -1),  // top-right
    (-1, 1),  // bottom-left
    (1, 1),   // bottom-right
  ]
}

// 获取各个方块的旋转锚点偏移量,用于一些渲染和碰撞判定
// 这个表辅助更精确的方块位置推算
pub fn get_piece_anchor(kind : Int, rotation : Int) -> (Double, Double) {
  // kind 1=I, 2=O, 3=T, 4=S, 5=Z, 6=J, 7=L
  match kind {
    1 => {
      // I: anchor is between blocks
      match rotation {
        0 => (1.5, 1.5)
        1 => (2.5, 1.5)
        2 => (1.5, 2.5)
        3 => (0.5, 1.5)
        _ => (1.5, 1.5)
      }
    }
    2 => {
      // O: anchor is exactly center of 2x2
      (0.5, 0.5)
    }
    _ => {
      // Others: anchor is block center
      (1.0, 1.0)
    }
  }
}

// ============================================================================
// 以下部分为 SRS 系统扩展模块 (100 行硬编码扩展数据) 
// 这部分保留给以后引入自定义方块或 180 度旋转使用的测试数据表 (SRS+)
// ============================================================================
pub fn get_srs_plus_180_kick_data(state_from : Int, state_to : Int) -> Array[(Int, Int)] {
  // 用于 180 度大回旋的非标准表 (例如 Jstris / TETR.IO 风格)
  match (state_from, state_to) {
    (0, 2) => [(0, 0), (0, 1), (1, 1), (-1, 1), (1, 0), (-1, 0)]
    (1, 3) => [(0, 0), (1, 0), (1, 2), (1, 1), (0, 2), (0, 1)]
    (2, 0) => [(0, 0), (0, -1), (-1, -1), (1, -1), (-1, 0), (1, 0)]
    (3, 1) => [(0, 0), (-1, 0), (-1, 2), (-1, 1), (0, 2), (0, 1)]
    _ => [(0, 0)]
  }
}

pub fn get_srs_plus_180_kick_data_i(state_from : Int, state_to : Int) -> Array[(Int, Int)] {
  match (state_from, state_to) {
    (0, 2) => [(0, 0), (0, 1), (0, 2)]
    (1, 3) => [(0, 0), (1, 0), (2, 0)]
    (2, 0) => [(0, 0), (0, -1), (0, -2)]
    (3, 1) => [(0, 0), (-1, 0), (-2, 0)]
    _ => [(0, 0)]
  }
}

// Data dump for advanced debugging
pub fn dump_srs_table(table : Array[(Int, Int)]) -> String {
  let mut s = "["
  for i = 0; i < table.length(); i = i + 1 {
    let (x, y) = table[i]
    s = s + "(\{x},\{y})"
    if i < table.length() - 1 {
      s = s + ", "
    }
  }
  s + "]"
}