import { aiApi } from './http';
import type {
  AIAbnormalResponse,
  AIBlankResponse,
  AIFoldedResponse,
  AICorrectionResponse,
  AISegmentResponse,
  AIExamOptionResponse,
  AIBarCodeResponse,
  AIOCRResponse,
  AISelectOptionsResponse,
  AITemplateRecognizeRequest,
  AITemplateResults,
  AITemplateModifyRequest,
  AIUpdateFrontBackAreaRequest,
  AIUpdateFrontBackAreaResponse,
  AIUpdateUpsideDownAreaRequest,
  AIUpdateUpsideDownAreaResponse,
  AIBatchRecognizeRequest,
  AIBatchRecognizeResponse,
  AITemplateListResponse,
  AITemplateGetResponse,
  AITemplateDeleteResponse,
  AITemplateCreateRequest,
  AITemplateCreateResponse,
  AIDirectRecognizeRequest,
  AIDirectRecognizeResponse,
  AIApplyOffsetAndScaleRequest,
  AIApplyOffsetAndScaleResponse
} from './types';

/**
 * AI阅卷接口客户端
 */
export class AIClient {
  /**
   * 异常检测 - 同时检测空白和折角
   */
  async detectAbnormal(file: File): Promise<AIAbnormalResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AIAbnormalResponse>('/abnormal', formData);
  }

  /**
   * 空白检测
   */
  async detectBlank(file: File): Promise<AIBlankResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AIBlankResponse>('/abnormal/blank', formData);
  }

  /**
   * 折角检测
   */
  async detectFolded(file: File): Promise<AIFoldedResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AIFoldedResponse>('/abnormal/folded_template', formData);
  }

  /**
   * 倾斜和颠倒矫正
   */
  async correctImage(file: File): Promise<AICorrectionResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AICorrectionResponse>('/correction', formData);
  }

  /**
   * 版面分割 - 返回zip文件
   */
  async layoutSegment(files: File[]): Promise<Blob> {
    const formData = new FormData();
    files.forEach((file) => formData.append('files', file));
    return aiApi.upload<Blob>('/layout', formData, { responseType: 'blob' });
  }

  /**
   * 客观题子题分割
   */
  async segmentObjective(file: File): Promise<AISegmentResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AISegmentResponse>('/segement/crop_obj', formData);
  }

  /**
   * 主观题子题分割
   */
  async segmentSubjective(file: File): Promise<AISegmentResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AISegmentResponse>('/segement/crop_sub', formData);
  }

  /**
   * 填涂考号识别
   */
  async recognizeExamOption(file: File): Promise<AIExamOptionResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AIExamOptionResponse>('/examOption/', formData);
  }

  /**
   * 条形码识别
   */
  async recognizeBarCode(file: File): Promise<AIBarCodeResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AIBarCodeResponse>('/bar_code/', formData);
  }

  /**
   * OCR识别
   */
  async recognizeOCR(file: File): Promise<AIOCRResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AIOCRResponse>('/ocr/', formData);
  }

  /**
   * 选择题识别
   */
  async recognizeSelectOptions(file: File): Promise<AISelectOptionsResponse> {
    const formData = new FormData();
    formData.append('file', file);
    return aiApi.upload<AISelectOptionsResponse>('/select_area/', formData);
  }

  /**
   * 模板生成
   */
  async recognizeTemplate(request: AITemplateRecognizeRequest): Promise<AITemplateResults> {
    return aiApi.post<AITemplateResults>('/template/recognize', request);
  }

  /**
   * 模板修改
   */
  async modifyTemplate(template: { results: AITemplateModifyRequest }): Promise<AITemplateResults> {
    return aiApi.post<AITemplateResults>('/template/modify', template);
  }

  /**
   * 文字定位区更新
   */
  async updateFrontBackArea(
    request: AIUpdateFrontBackAreaRequest
  ): Promise<AIUpdateFrontBackAreaResponse> {
    return aiApi.post<AIUpdateFrontBackAreaResponse>('/template/update_front_back_area', request);
  }

  /**
   * 颠倒识别定位区更新
   */
  async updateUpsideDownArea(
    request: AIUpdateUpsideDownAreaRequest
  ): Promise<AIUpdateUpsideDownAreaResponse> {
    return aiApi.post<AIUpdateUpsideDownAreaResponse>('/template/update_upside_down_area', request);
  }

  /**
   * 内容识别
   */
  async batchRecognize(request: AIBatchRecognizeRequest): Promise<AIBatchRecognizeResponse> {
    return aiApi.post<AIBatchRecognizeResponse>('/batch/recognize', request);
  }

  /**
   * 列出所有模板
   */
  async listTemplates(): Promise<AITemplateListResponse> {
    return aiApi.get<AITemplateListResponse>('/template');
  }

  /**
   * 获取模板信息
   */
  async getTemplate(templateId: string): Promise<AITemplateGetResponse> {
    return aiApi.get<AITemplateGetResponse>(`/template/${templateId}`);
  }

  /**
   * 删除模板
   */
  async deleteTemplate(templateId: string): Promise<AITemplateDeleteResponse> {
    return aiApi.delete<AITemplateDeleteResponse>(`/template/${templateId}`);
  }

  /**
   * 新增模板
   */
  async createTemplate(template: {
    results: AITemplateCreateRequest;
  }): Promise<AITemplateCreateResponse> {
    return aiApi.post<AITemplateCreateResponse>('/template/', template);
  }

  /**
   * 直接识别
   */
  async directRecognize(request: AIDirectRecognizeRequest): Promise<AIDirectRecognizeResponse> {
    return aiApi.post<AIDirectRecognizeResponse>('/direct/recognize', request);
  }

  /**
   * 对模板坐标应用偏移和缩放
   */
  async applyOffsetAndScale(
    request: AIApplyOffsetAndScaleRequest
  ): Promise<AIApplyOffsetAndScaleResponse> {
    return aiApi.post<AIApplyOffsetAndScaleResponse>('/template/apply_offset_and_scale', request);
  }
}

// 创建单例实例
export const aiClient = new AIClient();

// 默认导出
export default aiClient;
