import { getAllAlgosApi, getCameraAlgoListApi, CameraAlgoItem } from '@/api/camera/camera-preview';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import { useRequest } from 'vue-hooks-plus';
import { TimePeriodItem } from '../components/AlgorithmsSetting/types';
import { DetectionJSON } from '../components/AlgorithmsSetting/utils';

interface CameraAlgoItemInCard extends CameraAlgoItem {
  // detectionJSON: DetectionJSON;
  inferCode: string;
  enableCardBool: boolean;
  electronicFenceBool: boolean;
  regionJudge: number;
  timeRangeArr: TimePeriodItem[];
  metaValues: AlgoParamMetaItem[];
  judge: number; // 0-小于 1-大于 2-等于
  detectionFrequency: number;
  timeWindow?: number;
}

export interface AlgoParamMetaItem {
  id: string;
  label: string;
  criticalCount: number;
  confidence: number;
  'min-width': number;
  'max-width': number;
  nextObjs: AlgoParamMetaItem[];
}

const defaultSelectedAlgoDetail = {
  // detectionJSON: { detectionNum: 0, detectionUnit: 1 },
  regionJudge: 0,
};

const useCameraAlgoStore = defineStore('cameraAlgo', () => {
  const {
    data: cameraAlgoList,
    runAsync: getCameraAlgoList,
    mutate: mutateCameraAlgoList,
  } = useRequest(
    (cameraId: number) => {
      return getCameraAlgoListApi(cameraId);
    },
    { manual: true },
  );

  const {
    data: allAlgoList,
    runAsync: getAllAlgoList,
    mutate: mutateAllAlgoList,
  } = useRequest(getAllAlgosApi, {
    manual: true,
  });

  // 标记的paramCard集合
  const markedParamCardIds = ref<string[]>([]);

  // 标记的timeRange集合
  const markedTimeRangeIds = ref<string[]>([]);

  /** 所有算法列表中选定的算法id */
  const selectedAlgoId = ref<number | null | undefined>();

  const selectedAlgoDetail = ref<CameraAlgoItemInCard>({
    ...defaultSelectedAlgoDetail,
  } as CameraAlgoItemInCard);

  //计算原始模板数据
  const metaObjList = computed(() => {
    const extra = selectedAlgoDetail.value.algoInfo?.extra;
    if (!extra) return [];
    const extraObj = JSON.parse(extra);
    const params = extraObj?.inferParams;
    if (!params || (params && params.length == 0)) return [];
    const metaObjs = params[0]?.metaObjs;

    return metaObjs ? metaObjs : [];
  });

  const getAlgoDetail = (algoId: number): null | CameraAlgoItem => {
    const detail = cameraAlgoList.value?.find((x) => x.algoId === algoId);
    if (!detail) return null;
    return detail;
  };

  /** 算法是否已经绑定到相机 */
  const isAlgoBind = (algoId: number) => {
    return cameraAlgoList.value?.find((x) => x.algoId === algoId);
  };

  const deleteParam = (id: string) => {
    selectedAlgoDetail.value.metaValues = selectedAlgoDetail.value.metaValues.filter(
      (x) => x.id !== id,
    );
  };

  const deleteTimeRange = (id: string) => {
    selectedAlgoDetail.value.timeRangeArr = selectedAlgoDetail.value.timeRangeArr.filter(
      (item) => item.id !== id,
    );
  };

  const clear = () => {
    mutateCameraAlgoList();
    mutateAllAlgoList();
    selectedAlgoDetail.value = { ...defaultSelectedAlgoDetail } as CameraAlgoItemInCard;
  };

  return {
    cameraAlgoList,
    getCameraAlgoList,
    selectedAlgoId,
    metaObjList,
    allAlgoList,
    markedParamCardIds,
    markedTimeRangeIds,
    getAllAlgoList,
    getAlgoDetail,
    selectedAlgoDetail,
    isAlgoBind,
    clear,
    deleteParam,
    deleteTimeRange,
  };
});

export default useCameraAlgoStore;
