///|
/// # 手写 OCR ocr/handwriting_ocr
///
/// 图像处理库子模块
/// 图像处理库子模块 Image processing library submodule
// 手写体识别
// 手写体专用模板、灵活的字符分割、手写体识别流水线

///|
/// 手写体识别配置
#derive(Eq, @debug.Debug)
pub(all) struct HandwritingOcrConfig {
  min_char_width : Int32
  max_char_width : Int32
  min_char_height : Int32
  max_char_height : Int32
  target_size : Int32
  distance_metric : String
  flexibility : Double // 0.0-1.0,越高越灵活
  use_slant_correction : Bool
}

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

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

///|
/// 创建默认手写体识别配置
pub fn handwriting_ocr_default_config() -> HandwritingOcrConfig {
  HandwritingOcrConfig::{
    min_char_width: 8,
    max_char_width: 80,
    min_char_height: 10,
    max_char_height: 100,
    target_size: 48,
    distance_metric: "euclidean",
    flexibility: 0.7,
    use_slant_correction: true,
  }
}

///|
/// 生成手写体数字模板(更灵活的形状)
pub fn generate_handwriting_templates(
  char_set~ : String = "digits",
) -> Array[(String, Array[Double])] {
  let templates : Array[(String, Array[Double])] = []
  let chars = if char_set == "digits+letters" {
    [
      "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E",
      "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
      "U", "V", "W", "X", "Y", "Z",
    ]
  } else {
    ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  }
  for char in chars {
    // 生成多个变体(手写体有多种写法)
    for variant in 0..<3 {
      let template = generate_handwriting_char_template(char, variant)
      let label = char + "_v" + variant.to_string()
      templates.push((label, template))
    }
  }
  templates
}

///|
/// 生成手写体字符模板(带变体)
fn generate_handwriting_char_template(
  char : String,
  variant : Int32,
) -> Array[Double] {
  let size = 16
  let features : Array[Double] = Array::make(size * size, 0.0)
  let seed = if char.length() > 0 {
    char[0].to_int() + variant * 100
  } else {
    0
  }
  // 手写体特征:更随机、更灵活的形状
  for y in 0.. @types.Image raise @types.LoadError {
  ignore(max_slant)
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "correct_slant: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "correct_slant: 仅支持单通道灰度图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "correct_slant: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }

  // 简化版:检测主要倾斜角度并校正
  let angle = estimate_slant_angle(img)
  if angle.abs() > 1.0 {
    rotate_document(img, -angle)
  } else {
    img
  }
}

///|
/// 估计倾斜角度(手写体专用)
fn estimate_slant_angle(img : @types.Image) -> Double {
  let w = img.width
  let h = img.height
  let data = img.data
  // 简化:基于垂直投影的倾斜估计
  let mut best_angle = 0.0
  let mut best_score = 0.0
  let mut angle = -15.0
  while angle <= 15.0 {
    let angle_rad = angle * 3.141592653589793 / 180.0
    let cos_a = @math.cos(angle_rad)
    let sin_a = @math.sin(angle_rad)
    // 计算旋转后的垂直投影方差
    let projection : Array[Int32] = Array::make(w, 0)
    for y in 0.. 128 {
          let new_x = ((x.to_double() - w.to_double() / 2.0) * cos_a -
          (y.to_double() - h.to_double() / 2.0) * sin_a +
          w.to_double() / 2.0).to_int()
          if new_x >= 0 && new_x < w {
            projection[new_x] += 1
          }
        }
      }
    }
    // 计算方差
    let mut sum = 0
    let mut sum_sq = 0
    for i in 0.. best_score {
      best_score = variance
      best_angle = angle
    }
    angle += 1.0
  }
  best_angle
}

///|
/// 手写体字符分割(更宽松的参数)
fn segment_handwriting_chars(
  img : @types.Image,
  cfg : HandwritingOcrConfig,
) -> Array[(Int32, Int32, Int32, Int32)] {
  let w = img.width
  let h = img.height
  let data = img.data
  // 简单连通域标记
  let labels : Array[Int32] = Array::make(w * h, 0)
  let mut next_label = 1
  for y in 0.. 128 {
        let mut label = 0
        if x > 0 && labels[y * w + x - 1] > 0 {
          label = labels[y * w + x - 1]
        }
        if y > 0 && labels[(y - 1) * w + x] > 0 {
          if label == 0 {
            label = labels[(y - 1) * w + x]
          }
        }
        if label == 0 {
          label = next_label
          next_label += 1
        }
        labels[y * w + x] = label
      }
    }
  }
  // 计算边界框
  let min_x : Array[Int32] = Array::make(next_label, w)
  let max_x : Array[Int32] = Array::make(next_label, 0)
  let min_y : Array[Int32] = Array::make(next_label, h)
  let max_y : Array[Int32] = Array::make(next_label, 0)
  let area : Array[Int32] = Array::make(next_label, 0)
  for y in 0.. 0 {
        area[label] += 1
        if x < min_x[label] {
          min_x[label] = x
        }
        if x > max_x[label] {
          max_x[label] = x
        }
        if y < min_y[label] {
          min_y[label] = y
        }
        if y > max_y[label] {
          max_y[label] = y
        }
      }
    }
  }
  // 过滤(手写体更宽松)
  let regions : Array[(Int32, Int32, Int32, Int32)] = []
  for label in 1.. 0 { rw.to_double() / rh.to_double() } else { 0.0 }
    let min_ratio = 0.3 * (1.0 + cfg.flexibility)
    let max_ratio = 3.0 * (1.0 + cfg.flexibility)
    if rw >= cfg.min_char_width &&
      rw <= cfg.max_char_width &&
      rh >= cfg.min_char_height &&
      rh <= cfg.max_char_height &&
      ratio > min_ratio &&
      ratio < max_ratio &&
      area[label] > 15 {
      regions.push((min_x[label], min_y[label], rw, rh))
    }
  }
  regions
}

///|
/// 手写体识别(完整流水线)
/// @deprecated 已废弃,请使用 recognize_handwriting_enhanced 代替
#deprecated("Use recognize_handwriting_enhanced instead", skip_current_package=true)
pub fn recognize_handwriting(
  img : @types.Image,
  config? : HandwritingOcrConfig,
) -> OcrResult raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "recognize_handwriting: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "recognize_handwriting: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "recognize_handwriting: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }

  let cfg = match config {
    Some(c) => c
    None => handwriting_ocr_default_config()
  }
  // 配置验证
  if cfg.min_char_width <= 0 {
    raise @types.LoadError::DecodeFailed(
      "recognize_handwriting: min_char_width必须大于0",
    )
  }
  if cfg.max_char_width < cfg.min_char_width {
    raise @types.LoadError::DecodeFailed(
      "recognize_handwriting: max_char_width不能小于min_char_width",
    )
  }
  if cfg.target_size <= 0 {
    raise @types.LoadError::DecodeFailed(
      "recognize_handwriting: target_size必须大于0",
    )
  }
  // 1. 预处理
  let mut preprocessed = ocr_preprocess(img)
  // 2. 倾斜校正(手写体专用)
  if cfg.use_slant_correction {
    preprocessed = correct_slant(preprocessed)
  }
  // 3. 字符分割(手写体更宽松)
  let char_regions = segment_handwriting_chars(preprocessed, cfg)
  // 4. 生成手写体模板
  let templates = generate_handwriting_templates(char_set="digits")
  // 5. 对每个字符进行识别
  let char_results : Array[OcrCharResult] = []
  let mut text = ""
  let mut total_confidence = 0.0
  for _tup0 in char_regions {
    let (x, y, w, h) = _tup0
    // 提取字符图像
    let char_img = extract_handwriting_char_region(
      preprocessed,
      x,
      y,
      w,
      h,
      cfg.target_size,
    )
    // 提取特征
    let features = extract_handwriting_char_features(char_img, 16)
    // 模板匹配(多变体投票)
    let result = match_handwriting_template(
      features,
      templates,
      cfg.distance_metric,
    )
    text += result.0
    total_confidence += result.1
    char_results.push(OcrCharResult::{
      char: result.0,
      confidence: result.1,
      x,
      y,
      width: w,
      height: h,
    })
  }
  let avg_confidence = if char_results.length() > 0 {
    total_confidence / char_results.length().to_double()
  } else {
    0.0
  }
  OcrResult::{ text, confidence: avg_confidence, characters: char_results, }
}

///|
/// 提取手写体字符区域
fn extract_handwriting_char_region(
  img : @types.Image,
  x : Int32,
  y : Int32,
  width : Int32,
  height : Int32,
  target_size : Int32,
) -> @types.Image {
  let w = img.width
  let h = img.height
  let crop_w = if x + width > w { w - x } else { width }
  let crop_h = if y + height > h { h - y } else { height }
  if crop_w <= 0 || crop_h <= 0 {
    let empty : Array[Byte] = Array::make(target_size * target_size, b'\x00')
    return @types.Image::{
      width: target_size,
      height: target_size,
      channels: 1,
      data: Bytes::from_array(empty),
    }
  }
  let out : Array[Byte] = Array::make(target_size * target_size, b'\x00')
  let data = img.data
  for py in 0.. Array[Double] {
  let w = char_img.width
  let h = char_img.height
  let data = char_img.data
  let cell_w = w / grid_size
  let cell_h = h / grid_size
  let features : Array[Double] = []
  for r in 0.. 128 {
              count += 1
            }
          }
        }
      }
      let density = if total > 0 {
        count.to_double() / total.to_double()
      } else {
        0.0
      }
      features.push(density)
    }
  }
  features
}

///|
/// 手写体模板匹配(多变体投票)
fn match_handwriting_template(
  features : Array[Double],
  templates : Array[(String, Array[Double])],
  distance_metric : String,
) -> (String, Double) {
  // 收集所有变体的预测
  let label_votes : Array[(String, Double)] = []
  for _tup1 in templates {
    let (label_full, template) = _tup1
    let dist = match distance_metric {
      "manhattan" => manhattan_hw(features, template)
      "cosine" => cosine_hw(features, template)
      _ => euclidean_hw(features, template)
    }
    // 取第一个码点作为基础标签(数字 0-9),按字符还原而不是码点数字
    let base_label = if label_full.length() > 0 {
      hw_char_str(label_full[0])
    } else {
      label_full
    }
    let confidence = if dist > 0.0 { 1.0 / (1.0 + dist) } else { 1.0 }
    // 投票
    let mut found = false
    for i in 0.. best_votes {
      best_votes = votes
      best_label = label
    }
  }
  let total_votes = if templates.length() > 0 {
    templates.length().to_double() / 3.0
  } else {
    1.0
  }
  let confidence = best_votes / total_votes
  (best_label, confidence)
}

///|
/// 把字符串索引(码点 UInt16)还原成单字符字符串
fn hw_char_str(raw : UInt16) -> String {
  raw.to_int().to_char().map(c => c.to_string()).unwrap_or("")
}

///|
/// 局部欧氏距离
fn euclidean_hw(a : Array[Double], b : Array[Double]) -> Double {
  let n = if a.length() < b.length() { a.length() } else { b.length() }
  let mut sum_sq = 0.0
  for i in 0.. Double {
  let n = if a.length() < b.length() { a.length() } else { b.length() }
  let mut sum = 0.0
  for i in 0.. Double {
  let n = if a.length() < b.length() { a.length() } else { b.length() }
  let mut dot = 0.0
  let mut norm_a = 0.0
  let mut norm_b = 0.0
  for i in 0.. 0.0 {
    1.0 - dot / denom
  } else {
    1.0
  }
}