import { ref } from 'vue';
import { defineStore } from 'pinia';
import {
  CameraQueryParams,
  getCameraList,
  addCameraItem,
  updateCameraItem,
  getCameraState,
  addSRSCameraItem,
  addNVRCameraItem,
} from '@/api/camera/camera-overview';
import { CameraIPItem, CameraNVRItem, CameraSRS, CameraShowItem } from '../type';
import { useRequest } from 'vue-hooks-plus';

export const useCameraOverview = defineStore('camera-overview', () => {
  const queryType = ref('');
  const queryTypeContent = ref('');
  const queryCameraType = ref('');
  const queryWorkSpace = ref('');
  const querySourceType = ref('');
  const networkingState = ref<number>(-1);
  const integrationState = ref<number>(-1);
  const total = ref(0);
  const page = ref(1);
  const size = ref(10);

  let interval;

  const cameraItems = ref<CameraShowItem[]>([]);

  // 条件查询事件
  const conditionSearch = () => {
    const params: CameraQueryParams = {
      pageNumber: page.value,
      pageSize: size.value,
    };
    if (queryType.value) {
      switch (queryType.value) {
        case 'cameraIp':
          params.ip = queryTypeContent.value;
          break;

        case 'name':
          params.code = queryTypeContent.value;
          break;
      }
    }
    if (queryCameraType.value) {
      params.cameraType = queryCameraType.value;
    }
    if (queryWorkSpace.value) {
      params.sceneCode = queryWorkSpace.value;
    }
    if (querySourceType.value) {
      console.log('queryCameraType.value', querySourceType.value);
      params.sourceType = querySourceType.value;
    }
    if (networkingState.value != -1) {
      params.networkingState = networkingState.value;
    }
    if (integrationState.value != -1) {
      params.integrationState = integrationState.value;
    }
    return getCameraList(params).then((res) => {
      return res;
    });
  };

  const { loading, run: getCameraItems } = useRequest(conditionSearch, {
    manual: true,
    onSuccess: (res) => {
      cameraItems.value = [...res.records];
      total.value = res.totalRow;
    },
  });

  const addCamera = (data: CameraIPItem) => {
    console.log(data);

    return addCameraItem(data).then(() => {
      getCameraItems();
    });
  };

  const addSRSCamera = (data: CameraSRS) => {
    return addSRSCameraItem(data).then(() => {
      getCameraItems();
    });
  };

  const addNVRCamera = (data: CameraNVRItem) => {
    return addNVRCameraItem(data).then(() => {
      getCameraItems();
    });
  };

  const editCamera = (data: Partial<CameraShowItem>) => {
    return updateCameraItem(data);
    // .then(() => {
    //   getCameraItems();
    // });
  };

  const getState = () => {
    getCameraState({ cameraCodeList: cameraItems.value.map((camera) => camera.code) }).then(
      (res) => {
        res.forEach((item) => {
          cameraItems.value.find((camera) => camera.code === item.cameraCode)!.networkingState =
            item.status;
        });
      },
    );
  };

  const openInterval = () => {
    setTimeout(() => {
      getState();
    }, 500);
    interval = setInterval(() => {
      getState();
    }, 30000);
  };

  const closeInterval = () => {
    clearInterval(interval);
  };

  const reset = () => {
    queryType.value = '';
    queryTypeContent.value = '';
    queryCameraType.value = '';
    queryWorkSpace.value = '';
    total.value = 0;
    page.value = 1;
    size.value = 10;
    cameraItems.value = [];
  };

  return {
    queryType,
    queryTypeContent,
    queryCameraType,
    querySourceType,
    queryWorkSpace,
    integrationState,
    networkingState,
    cameraItems,
    loading,
    total,
    size,
    page,
    getCameraItems,
    addCamera,
    editCamera,
    openInterval,
    closeInterval,
    getState,
    addSRSCamera,
    addNVRCamera,
    reset,
  };
});

export default useCameraOverview;
