import {
  getHDIncidentList,
  IncidentItem,
  IncidentItemDetail,
} from '@/api/incident';
import {
  getAllResources,
  getAllsingleDevice,
  ResourceItemDetail,
  SingleDeviceItem,
  getAllvideoDevice,
  searchWarehouseByGoods,
} from '@/api/resource';
import {
  PENDING_DISPOSAL_INCIDENT,
  PENDING_INCIDENT,
  // EMERGENCY_VEHICLES,
  VIDEO_SURVEILLANCE,
  WARNING_INCIDENT,
  SINGLE_PAWN,
  // EMERGENCY_TEAM,
} from '@/components/MarkerMap/constants';
import { type } from 'os';
import { defineStore } from 'pinia';

type AllResources = NonNullable<IncidentItemDetail['baseInfo']> &
  NonNullable<SingleDeviceItem> & ResourceItemDetail;

type Simply<T> = { [K in keyof T]: T[K] };

export interface MarkerType extends Simply<AllResources> {
  locations?: string;
  marker?: any;
  popup?: any;
  [key: string]: any;
}

export interface MarkerStateType {
  warningIncident: MarkerType[];
  pendingIncident: MarkerType[];
  pendingDisposalIncident: MarkerType[];
  videoSurveillance: MarkerType[];
  emergencyVehicles: MarkerType[];
  emergencyTeam: MarkerType[];
  emergencyWarehouse: MarkerType[];
  singleDevice: SingleDeviceItem[];
  /** 详情页面 点击监控点位 打开视频 */
  livevideovisible: boolean;
  livevideos: MarkerType[];
  incident: MarkerType[];
  loading: boolean;
  currentIncident: MarkerType;
}

export interface MainActionsType {
  toggleLoading(): void;
  getHDIncidentList(): void;
  getAllResources(): void;
  getAllsingleDevice(): void;
  searchWarehouseByGoods(name?: string): void;
}

export default defineStore<'marker', MarkerStateType, {}, MainActionsType>(
  'marker',
  {
    state: () => ({
      currentIncident: {},
      incident: [],
      warningIncident: [],
      pendingIncident: [],
      pendingDisposalIncident: [],
      videoSurveillance: [],
      emergencyVehicles: [],
      emergencyTeam: [],
      singleDevice: [],
      emergencyWarehouse: [],
      livevideos: [],
      livevideovisible: false,
      loading: false,
    }),
    actions: {
      toggleLoading() {
        this.loading = !this.loading;
      },
      async getHDIncidentList() {
        try {
          this.toggleLoading();
          const { data } = await getHDIncidentList();
          this.incident = (data as MarkerType[]) ?? [];
        } finally {
          this.toggleLoading();
        }
      },
      async getAllsingleDevice() {
        try {
          this.toggleLoading();
          const { data } = await getAllsingleDevice();

          this.singleDevice = [];

          this.singleDevice = Object.values(data).map((item) => ({
            ...item,
            locations: `${item.gpsX},${item.gpsY}`,
          }));
          // console.log(this.singleDevice);
        } finally {
          this.toggleLoading();
        }
      },

      async getAllvideoDevice() {
        try {
          this.toggleLoading();
          const { data } = await getAllvideoDevice();
          let videoData = [];
          for (var index in data) {
            if (data[index].gpsX) {
              videoData.push(data[index]);
            }
          }
          this.videoSurveillance = [];

          this.videoSurveillance = Object.values(videoData).map((item) => ({
            ...item,
            locations: `${item.gpsX},${item.gpsY}`,
          }));
          // console.log(this.videoSurveillance);
        } finally {
          this.toggleLoading();
        }
      },

      async getAllResources() {
        try {
          this.toggleLoading();
          const { data } = await getAllResources();
          // @ts-ignore
          this.emergencyVehicles =
            data.应急车队.map((i) => ({
              ...i,
              locations: `${i.longitude},${i.latitude}`,
            })) ?? [];
          // @ts-ignore
          this.emergencyTeam =
            data.应急队伍.map((i) => ({
              ...i,
              locations: `${i.longitude},${i.latitude}`,
            })) ?? [];
          // @ts-ignore
          this.emergencyWarehouse =
            data.应急仓库.map((i) => ({
              ...i,
              locations: `${i.longitude},${i.latitude}`,
            })) ?? [];
        } finally {
          this.toggleLoading();
        }
      },
      async searchWarehouseByGoods(name) {
        try {
          this.toggleLoading();
          const { data } = await searchWarehouseByGoods(name);
          this.emergencyWarehouse =
            data.map((i) => ({
              ...i,
              locations: `${i.longitude},${i.latitude}`,
            })) ?? [];
        } finally {
          this.toggleLoading();
        }
      },
    },
  },
);
