///|
/// # 表格识别 ocr/table_recognition
///
/// 图像处理库子模块
/// 图像处理库子模块 Image processing library submodule
// 表格识别
// 表格线检测、单元格分割、表格结构分析

///|
/// 表格线结构体
#derive(Eq, @debug.Debug)
pub(all) struct TableLine {
  x1 : Int32
  y1 : Int32
  x2 : Int32
  y2 : Int32
  line_type : TableLineType
  thickness : Int32
}

///|
pub extend TableLine with Eq::{not_equal, equal}

///|
pub extend TableLine with @debug.Debug::{to_repr}

///|
/// 表格线类型
/// `#non_exhaustive` 表示未来可能添加新构造器
#derive(Eq, @debug.Debug)
#non_exhaustive
pub(all) enum TableLineType {
  Horizontal
  Vertical
}

///|
pub extend TableLineType with @debug.Debug::{to_repr}

///|
pub extend TableLineType with Eq::{not_equal, equal}

///|
/// 单元格结构体
#derive(Eq, @debug.Debug)
pub(all) struct TableCell {
  x : Int32
  y : Int32
  width : Int32
  height : Int32
  row : Int32
  col : Int32
  row_span : Int32
  col_span : Int32
}

///|
pub extend TableCell with Eq::{not_equal, equal}

///|
pub extend TableCell with @debug.Debug::{to_repr}

///|
/// 表格结构结构体
#derive(Eq, @debug.Debug)
pub(all) struct TableStructure {
  rows : Int32
  cols : Int32
  cells : Array[TableCell]
  horizontal_lines : Array[TableLine]
  vertical_lines : Array[TableLine]
  bounding_box : (Int32, Int32, Int32, Int32)
}

///|
pub extend TableStructure with Eq::{not_equal, equal}

///|
pub extend TableStructure with @debug.Debug::{to_repr}

///|
/// 检测水平表格线
pub fn detect_horizontal_lines(
  img : @types.Image,
  min_length~ : Int32 = 20,
  max_gap~ : Int32 = 2,
) -> Array[TableLine] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_horizontal_lines: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "detect_horizontal_lines: 仅支持单通道二值图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "detect_horizontal_lines: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if min_length <= 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_horizontal_lines: min_length必须大于0",
    )
  }
  if max_gap < 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_horizontal_lines: max_gap不能为负数",
    )
  }
  let w = img.width
  let h = img.height
  let data = img.data
  let lines : Array[TableLine] = []
  // 逐行检测水平线
  for y in 0..= min_length {
          lines.push(TableLine::{
            x1: start_x,
            y1: y,
            x2: end_x,
            y2: y,
            line_type: TableLineType::Horizontal,
            thickness: 1,
          })
        }
      } else {
        x += 1
      }
    }
  }
  // 合并相邻的水平线(同一表格线可能有多行像素)
  merge_horizontal_lines(lines, 3)
}

///|
/// 检测垂直表格线
pub fn detect_vertical_lines(
  img : @types.Image,
  min_length~ : Int32 = 20,
  max_gap~ : Int32 = 2,
) -> Array[TableLine] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_vertical_lines: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "detect_vertical_lines: 仅支持单通道二值图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "detect_vertical_lines: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if min_length <= 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_vertical_lines: min_length必须大于0",
    )
  }
  if max_gap < 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_vertical_lines: max_gap不能为负数",
    )
  }
  let w = img.width
  let h = img.height
  let data = img.data
  let lines : Array[TableLine] = []
  // 逐列检测垂直线
  for x in 0..= min_length {
          lines.push(TableLine::{
            x1: x,
            y1: start_y,
            x2: x,
            y2: end_y,
            line_type: TableLineType::Vertical,
            thickness: 1,
          })
        }
      } else {
        y += 1
      }
    }
  }
  // 合并相邻的垂直线
  merge_vertical_lines(lines, 3)
}

///|
/// 合并相邻的水平线
fn merge_horizontal_lines(
  lines : Array[TableLine],
  max_y_diff : Int32,
) -> Array[TableLine] {
  if lines.length() == 0 {
    return lines
  }
  let merged : Array[TableLine] = []
  let mut current = lines[0]
  for i in 1.. 0 {
      // 合并
      current = TableLine::{
        x1: @types.int_min(current.x1, next.x1),
        y1: (current.y1 + next.y1) / 2,
        x2: @types.int_max(current.x2, next.x2),
        y2: (current.y2 + next.y2) / 2,
        line_type: TableLineType::Horizontal,
        thickness: current.thickness + 1,
      }
    } else {
      merged.push(current)
      current = next
    }
  }
  merged.push(current)
  merged
}

///|
/// 合并相邻的垂直线
fn merge_vertical_lines(
  lines : Array[TableLine],
  max_x_diff : Int32,
) -> Array[TableLine] {
  if lines.length() == 0 {
    return lines
  }
  let merged : Array[TableLine] = []
  let mut current = lines[0]
  for i in 1.. 0 {
      current = TableLine::{
        x1: (current.x1 + next.x1) / 2,
        y1: @types.int_min(current.y1, next.y1),
        x2: (current.x2 + next.x2) / 2,
        y2: @types.int_max(current.y2, next.y2),
        line_type: TableLineType::Vertical,
        thickness: current.thickness + 1,
      }
    } else {
      merged.push(current)
      current = next
    }
  }
  merged.push(current)
  merged
}

///|
/// 分析表格结构(基于水平线和垂直线)
pub fn analyze_table_structure(
  horizontal_lines : Array[TableLine],
  vertical_lines : Array[TableLine],
) -> TableStructure raise @types.LoadError {
  // 输入验证
  if horizontal_lines.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "analyze_table_structure: 水平线列表不能为空",
    )
  }
  if vertical_lines.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "analyze_table_structure: 垂直线列表不能为空",
    )
  }
  if horizontal_lines.length() < 2 || vertical_lines.length() < 2 {
    return TableStructure::{
      rows: 0,
      cols: 0,
      cells: [],
      horizontal_lines,
      vertical_lines,
      bounding_box: (0, 0, 0, 0),
    }
  }
  // 提取水平线的y坐标(按y排序)
  let h_ys : Array[Int32] = []
  for line in horizontal_lines {
    h_ys.push(line.y1)
  }
  // 排序
  for _ in 0.. h_ys[j + 1] {
        let temp = h_ys[j]
        h_ys[j] = h_ys[j + 1]
        h_ys[j + 1] = temp
      }
    }
  }
  // 提取垂直线的x坐标(按x排序)
  let v_xs : Array[Int32] = []
  for line in vertical_lines {
    v_xs.push(line.x1)
  }
  for _ in 0.. v_xs[j + 1] {
        let temp = v_xs[j]
        v_xs[j] = v_xs[j + 1]
        v_xs[j + 1] = temp
      }
    }
  }
  // 计算边界框
  let min_x = v_xs[0]
  let max_x = v_xs[v_xs.length() - 1]
  let min_y = h_ys[0]
  let max_y = h_ys[h_ys.length() - 1]
  // 生成单元格
  let cells : Array[TableCell] = []
  let rows = h_ys.length() - 1
  let cols = v_xs.length() - 1
  for row in 0.. 0 && cell_h > 0 {
        cells.push(TableCell::{
          x: cell_x,
          y: cell_y,
          width: cell_w,
          height: cell_h,
          row,
          col,
          row_span: 1,
          col_span: 1,
        })
      }
    }
  }
  TableStructure::{
    rows,
    cols,
    cells,
    horizontal_lines,
    vertical_lines,
    bounding_box: (min_x, min_y, max_x - min_x, max_y - min_y),
  }
}

///|
/// 完整表格识别流水线
pub fn recognize_table(
  img : @types.Image,
  min_line_length~ : Int32 = 20,
) -> TableStructure raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "recognize_table: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "recognize_table: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "recognize_table: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if min_line_length <= 0 {
    raise @types.LoadError::DecodeFailed(
      "recognize_table: min_line_length必须大于0",
    )
  }
  // 1. 预处理(二值化)
  let binary = if img.channels == 1 { img } else { to_grayscale_table(img) }
  // 2. 检测水平线
  let h_lines = detect_horizontal_lines(binary, min_length=min_line_length)
  // 3. 检测垂直线
  let v_lines = detect_vertical_lines(binary, min_length=min_line_length)
  // 4. 分析表格结构
  analyze_table_structure(h_lines, v_lines)
}

///|
/// 提取单元格图像
pub fn extract_cell_image(
  img : @types.Image,
  cell : TableCell,
  padding~ : Int32 = 1,
) -> @types.Image raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_cell_image: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "extract_cell_image: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "extract_cell_image: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if cell.width <= 0 || cell.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_cell_image: 单元格尺寸必须大于0",
    )
  }
  if padding < 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_cell_image: padding不能为负数",
    )
  }
  let w = img.width
  let h = img.height
  let ch = img.channels
  let x = @types.int_max(0, cell.x - padding)
  let y = @types.int_max(0, cell.y - padding)
  let width = @types.int_min(w - x, cell.width + padding * 2)
  let height = @types.int_min(h - y, cell.height + padding * 2)
  if width <= 0 || height <= 0 {
    let empty : Array[Byte] = Array::make(1 * 1 * ch, b'\x00')
    return @types.Image::{
      width: 1,
      height: 1,
      channels: ch,
      data: Bytes::from_array(empty),
    }
  }
  let out : Array[Byte] = Array::make(width * height * ch, b'\x00')
  let data = img.data
  for yy in 0.. Array[@types.Image] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_all_cells: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "extract_all_cells: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "extract_all_cells: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if padding < 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_all_cells: padding不能为负数",
    )
  }
  let images : Array[@types.Image] = []
  for cell in structure.cells {
    let cell_img = extract_cell_image(img, cell, padding~)
    images.push(cell_img)
  }
  images
}

///|
/// 转灰度(表格识别专用)
fn to_grayscale_table(img : @types.Image) -> @types.Image {
  if img.channels == 1 {
    return img
  }
  let n = img.width * img.height
  let ch = img.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  let data = img.data
  for i in 0..