import * as React from 'react';
import Request from '../utils/request';

export const DeviceState = {
  offLine: 0,
  onLine: 1,
};

export const DeviceStateString = {
  [DeviceState.onLine]: '在线',
  [DeviceState.offLine]: '离线',
};
/**
 * @description 获取扬尘监测设备列表
 */
export const useEnvironmentalDevicesList = () => {
  const [state, setState] = React.useState<{
    loading: boolean;
    dataSource: {
      offline: number;
      online: number;
      list: {
        name: string;
        state: keyof typeof DeviceStateString;
        sn: string;
        id: number;
        checked: boolean;
      }[];
    };
  }>({
    loading: false,
    dataSource: { list: [], offline: 0, online: 0 },
  });
  const request = () => {
    setState((preState) => ({ ...preState, loading: true }));
    Request.sendRequest({
      url: '/v1/dust/list',
    })
      .then((data: any) => {
        setState((preState) => ({
          ...preState,
          loading: false,
          dataSource: {
            ...data,
            list: data.list.map((element, index) => {
              return { ...element, checked: index === 0 };
            }),
          },
        }));
      })
      .catch((error) => {
        setState((preState) => ({ ...preState, loading: false }));
      });
  };
  return [state, setState, request] as const;
};

/**
 * @description 搜索某个扬尘设备的历史数据
 */
export const useDeviceHistoryData = () => {
  const [state, setState] = React.useState<{
    loading: boolean;
    dataSource: {
      offline: number;
      online: number;
      list: {
        date: string;
        [propIndex: string]: string;
      }[];
    };
  }>({
    loading: false,
    dataSource: { list: [], offline: 0, online: 0 },
  });
  const request = (params: { type: 0 | 1 | 2; time: number; sn: string }) => {
    setState((preState) => ({ ...preState, loading: true }));
    Request.sendRequest({
      url: '/v1/dust/history',
      params,
    })
      .then((data: any) => {
        setState((preState) => ({
          ...preState,
          loading: false,
          dataSource: data,
        }));
      })
      .catch((error) => {
        setState((preState) => ({ ...preState, loading: false }));
      });
  };
  return [state, request] as const;
};
/**
 * @description 获取某个扬尘设备的数据信息
 */
export const useEnvironmentalDeviceInfo = () => {
  const [state, setState] = React.useState<{
    loading: boolean;
    dataSource: {
      b03: number;
      date: number;
      h01: number;
      p01: number;
      pm10: number;
      pm25: number;
      r01: number;
      t01: number;
      tsp: number;
      w01: number;
      w02: number;
    };
  }>({
    loading: false,
    dataSource: {
      b03: 0,
      date: 0,
      h01: 0,
      p01: 0,
      pm10: 0,
      pm25: 0,
      r01: 0,
      tsp: 0,
      w01: 0,
      t01: 0,
      w02: 0,
    },
  });
  const request = (params: { sn: string }) => {
    setState((preState) => ({ ...preState, loading: true }));
    Request.sendRequest({
      url: '/v1/dust/last',
      params,
    })
      .then((data: any) => {
        setState((preState) => ({
          ...preState,
          dataSource: data,
          loading: false,
        }));
      })
      .catch((error) => {
        setState((preState) => ({ ...preState, loading: false }));
      });
  };
  return [state, request] as const;
};
/**
 * @deprecated 获取设备的AQI指数
 */
export const useDeviceAqiData = () => {
  const [state, setState] = React.useState<{
    loading: boolean;
    dataSource: {
      aqi: number;
    };
  }>({
    loading: false,
    dataSource: { aqi: 0 },
  });
  const request = (params: { sn: string }) => {
    setState((preState) => ({ ...preState, loading: true }));
    Request.sendRequest({
      url: '/v1/dust/aqi',
      params,
    })
      .then((data: any) => {
        setState((preState) => ({
          ...preState,
          loading: false,
          dataSource: data,
        }));
      })
      .catch((error) => {
        setState((preState) => ({ ...preState, loading: false }));
      });
  };
  return [state, request] as const;
};
