import 'package:flyinsono/lab/manager/interfaces/analysis.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_common/logger/logger.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/rpc.dart';

import 'base_manager.dart';

class URMFunctionManager extends BaseManager implements IURMFunctionManager {
  /// URM 后处理
  @override
  Future<URMProcessResult> runPostProcess(
      {required URMProcessRequest request}) async {
    final requestWithToken = request;
    requestWithToken.token = token;
    final URMProcessResult result =
        await rpc.aIDiagnosis.uRMProcessAsync(requestWithToken);
    return result;
  }

  /// URM 视频后处理
  @override
  Future<String> runVideoPostProcess(
      {required URMVideoProcessRequest request}) async {
    final requestWithToken = request;
    requestWithToken.token = token;
    final URMVideoProcessResult result =
        await rpc.aIDiagnosis.uRMVideoProcessAsync(requestWithToken);
    return result.base64String ?? "";
  }

  ///URM 视频后处理:保存视频
  @override
  Future<String> saveAnalysisVideoByTask(
      SaveAnalysisVideoRequest request) async {
    try {
      request.token = token;
      return await rpc.researchEdition.saveAnalysisVideoByTaskAsync(request);
    } catch (e) {
      logger.e("URMFunctionManager saveAnalysisVideoByTask ex:", e);
      return '';
    }
  }

  ///批量分析
  @override
  Future<String> batchAnalysis({
    required List<AnalysisItem> analysisList,
    String? businessCode,
  }) async {
    String result = '';
    try {
      result =
          await rpc.researchEdition.batchAnalysisAsync(BatchAnalysisRequest(
        token: token,
        analysisList: analysisList,
      ));
    } catch (e) {
      logger.e("AnalysisManager batchAnalysisAsync ex:", e);
    }
    return result;
  }

  bool checkIfType(String url, String type) {
    if (url.toLowerCase().endsWith(type)) {
      return true;
    } else {
      return false;
    }
  }

  @override
  Future<bool> cancelAnalysis(String taskCode) async {
    bool result = false;
    try {
      result = await rpc.researchEdition
          .cancelBatchAnalysisAsync(BatchAnalysisTaskRequest(
        token: token,
        mainTaskCode: taskCode,
      ));
    } catch (e) {
      logger.e("AnalysisManager batchAnalysisAsync ex:", e);
    }
    return result;
  }

  @override
  ImageType getImageType(SampleRemedicalInfoDTO element) {
    if (element.isURM) {
      if (element.isAnalysisResult) {
        return ImageType.URM_Result;
      } else {
        return ImageType.URM;
      }
    } else {
      if (checkIfType(element.imageUrl ?? '', '.mp4')) {
        return ImageType.Video;
      }
      if (checkIfType(element.imageUrl ?? '', '.vid')) {
        return ImageType.Vid;
      }

      return ImageType.Measure_Result;
    }
  }

  @override
  String? findURMResultVid(SampleRemedicalInfoDTO element) {
    if (element.isURM && element.isAnalysisResult) {
      // 遍历 element.imgList 找到 type ==5 的 vid
      if (element.imgList != null && element.imgList!.isNotEmpty)
        for (ImgItem img in element.imgList!) {
          if (img.imgType == 5) {
            return img.fileToken;
          }
        }
    }
    return null;
  }
}
