import { ref } from 'vue';
import { defineStore } from 'pinia';
import {
  addImagePoint,
  addJuHePoint,
  closePoint,
  stopTrajectory,
  startBidding,
  addTrajectory,
  playTrajectory
} from '@/utils/map/mapOperation.js';
import { getTrajectorPointOnPeople, getTrajectorPointOnCar } from '@/service/api/mapRequest.js';
import breathe from '@/assets/json/breathe.json';
import car from '@/assets/json/car.json';
export const useMapStore = defineStore('mapStore', () => {
  //当前病例点位列表
  const currentToolSelectArray = ref(['newAdress', 'touch']);

  // 会商车绘制列表
  const flowCarList = ref([]);

  const pushFlowCarList = (value) => {
    flowCarList.value.push(value);
  };

  const delFlowCarList = () => {
    flowCarList.value = [];
  };

  //点亮地图工具图标
  const setCurrentToolSelectArray = async (value) => {
    currentToolSelectArray.value.push(value);
    try {
      switch (value) {
        case 'newAdress': {
          //新址图标
          addImagePoint(
            'newAdress',
            [
              { x: -14973.6499, y: -1759.0666 },
              { x: -24130.3138, y: -23044.9095 }
            ],
            {
              imageUrl: new URL(`@/assets/image/mapTools/newAdress.png`, import.meta.url).href
            }
          );
          break;
        }
        case 'touch': {
          const data = breathe.filter((item, index) => {
            return index % 128 == 8;
          });
          //密接图标
          addImagePoint('touch', data, {
            imageUrl: new URL(`@/assets/image/mapTools/2.png`, import.meta.url).href
          });
          // const res = await getTrajectorPointOnPeople({ type: 1 });
          // if (res?.code == 200) {
          //   addJuHePoint(value, res?.data || []);
          // } else {
          //   console.log('获取传染病数据错误');
          // }
          // addImagePoint(value, {
          //   imageUrl: new URL(`@/assets/image/mapTools/2.png`, import.meta.url).href
          // });
          break;
        }
        case 'infectious': {
          const data = breathe.filter((item, index) => {
            return index % 8 == 0;
          });
          addJuHePoint(value, data || [], {
            label: '传染病',
            color: 'rgba(230, 162, 60, 0.5)',
            borderColor: 'rgba(230, 162, 60, 1)'
          });
          //传染病图标
          // const res = await getTrajectorPointOnPeople({ type: 1 });
          // if (res?.code == 200) {
          //   addJuHePoint(value, res?.data || [], {
          //     label: '传染病',
          //     color: 'rgba(230, 162, 60, 0.5)',
          //     borderColor: 'rgba(230, 162, 60, 1)'
          //   });
          // } else {
          //   console.log('获取传染病数据错误');
          // }

          break;
        }
        case 'feverClinic': {
          const data = breathe.filter((item, index) => {
            return index % 8 == 4;
          });
          addJuHePoint(value, data || [], {
            label: '发热门诊',
            color: 'rgba(245, 108, 108, 0.5)',
            borderColor: 'rgba(245, 108, 108, 1)'
          });
          //发热门诊图标
          // const res = await getTrajectorPointOnPeople({ type: 2 });
          // if (res?.code == 200) {
          //   addJuHePoint(value, res?.data || [], {
          //     label: '发热门诊',
          //     color: 'rgba(245, 108, 108, 0.5)',
          //     borderColor: 'rgba(245, 108, 108, 1)'
          //   });
          // } else {
          //   console.log('获取发热数据错误');
          // }
          break;
        }
        case 'breathe': {
          const data = breathe.filter((item, index) => {
            return index % 8 == 6;
          });
          addJuHePoint(value, data || [], {
            label: '呼吸数据',
            color: 'rgba(148, 0, 211, 0.5)',
            borderColor: 'rgba(148, 0, 211, 1)'
          });
          //呼吸图标
          // const res = await getTrajectorPointOnPeople({ type: 3 });
          // if (res?.code == 200) {
          //   addJuHePoint(value, res?.data || [], {
          //     label: '呼吸数据',
          //     color: 'rgba(148, 0, 211, 0.5)',
          //     borderColor: 'rgba(148, 0, 211, 1)'
          //   });
          // } else {
          //   console.log('获取呼吸数据错误');
          // }
          break;
        }
        case 'flowCar': {
          addTrajectory(car, 1);
          pushFlowCarList('car_layer' + 1);
          setTimeout(() => {
            playTrajectory(1);
          }, 2000);
          // const res = await getTrajectorPointOnCar();
          // if (res?.code == 200) {
          //   addImagePoint(value, res?.data || [], {
          //     imageUrl: new URL(`@/assets/image/mapTools/6.png`, import.meta.url).href
          //   });
          // }
          //流调会商车图标
          // addTrajectory();
          // setTimeout(() => {
          //   playTrajectory();
          // }, 2000);
          break;
        }
        case 'Bidding': {
          break;
        }
      }
    } catch (error) {
      console.log(error);
    }
  };

  //关闭地图工具图标
  const delCurrentToolSelectArray = (value) => {
    currentToolSelectArray.value = currentToolSelectArray.value.filter((item) => item != value);
    if (value == 'Bidding') {
      startBidding(false);
      closePoint('drawLine');
      closePoint('drawPoint');
    } else if (value == 'flowCar') {
      flowCarList.value.forEach((item) => {
        stopTrajectory(item);
        closePoint(item);
      });
      closePoint(value);
      delFlowCarList();
    } else {
      closePoint(value);
    }
  };

  return {
    currentToolSelectArray,
    flowCarList,
    pushFlowCarList,
    delFlowCarList,
    setCurrentToolSelectArray,
    delCurrentToolSelectArray
  };
});
