import { maxNum, minNum } from "@/utils/tool";
import { abc } from "@/constants/enumerate";

// recognize ---start >
// 扫描数据相关
// 参考：./data.json
export type RecogAreaType = "absent" | "breach" | "paperType" | "question";

export interface RecogOptionSize {
  x: number;
  y: number;
  w: number;
  h: number;
  filled: boolean;
}
export interface RecognizeArea {
  index: number;
  type: RecogAreaType;
  fillPosition: Array<{ x: number; y: number }>;
  fillSize: {
    w: number;
    h: number;
  };
  fillArea: { x: number; y: number; w: number; h: number };
  options: string[];
  optionSizes: RecogOptionSize[];
  result: string[];
  multiple: boolean;
}

export interface RecogDataField {
  field: string;
  index: number;
  content: string;
  wrong_flag: number;
  fill_result: RecogDataFillResult[];
  type: string;
  rect?: [number, number, number, number];
}

export interface RecogDataFillResult {
  main_number: number;
  sub_number: number;
  // 所有试题的统一序号
  index?: number;
  single: number;
  fill_option: number[];
  suspect_flag: number;
  fill_position: string[];
  fill_size: [number, number];
}

export interface RecogDataType {
  algorithm: number;
  page_index: number;
  error_flag: number;
  blank_flag: number;
  angle: number;
  examNumber: RecogDataField;
  absent: RecogDataField;
  breach: RecogDataField;
  pageNumber: RecogDataField;
  paperType: RecogDataField;
  question: RecogDataField[];
  block_struct: number[];
}
// recognize ---end >

// extends

export interface RecogEditData extends RecognizeArea {
  areaSrc: string;
  [k: string]: any;
}
export interface ImageRecogData extends RecognizeArea {
  [k: string]: any;
}

export interface RecogBlock extends RecognizeArea {
  areaImg: string;
  fillAreaStyle: Record<string, any>;
  fillOptionStyles: Array<Record<string, any>>;
  [k: string]: any;
}

// functions
export function parseRecogData(data: string) {
  if (!data) return null;
  const precogData = window.atob(data);
  const recogData: RecogDataType | null = precogData
    ? JSON.parse(precogData)
    : null;
  return recogData;
}

export function parseDetailSize(
  data: RecogDataFillResult,
  type: RecogAreaType,
  index: number,
  fillResult: number[]
): RecognizeArea {
  const result: RecognizeArea = {
    index,
    type,
    fillPosition: [],
    fillSize: { w: 0, h: 0 },
    fillArea: { x: 0, y: 0, w: 0, h: 0 },
    options: [],
    optionSizes: [],
    result: [],
    multiple: false,
  };

  if (!data) return result;

  result.fillPosition = data.fill_position.map((item) => {
    const size = item.split(",");
    return {
      x: size[0] ? Number(size[0]) : 0,
      y: size[1] ? Number(size[1]) : 0,
    };
  });
  result.fillSize = {
    w: data.fill_size[0],
    h: data.fill_size[1],
  };
  const xs = result.fillPosition.map((item) => item.x);
  const maxX = maxNum(xs);
  const minX = minNum(xs);
  result.fillArea = {
    x: minX,
    y: result.fillPosition[0].y,
    w: maxX - minX,
    h: result.fillPosition[0].y,
  };

  result.optionSizes = result.fillPosition.map((item, index) => {
    let filled = false;
    if (type !== "paperType") {
      filled = fillResult[index] === 1;
    }
    return {
      x: item.x - result.fillArea.x,
      y: item.y - result.fillArea.y,
      filled,
      ...result.fillSize,
    };
  });

  if (type === "question") {
    const options = abc.substring(0, data.fill_position.length).split("");
    // 空用“#”表示
    result.options = ["#", ...options];
    result.result = options.filter((r, ind) => fillResult[ind] === 1);
    result.multiple = true;
  }

  return result;
}
