import store from "../store/index";
import {
  readLogData,
  readInspectionData,
  readDarfData,
  readDeviceData
} from "@/utils/IoReadingAndWriting.js";
import lodash from "lodash";


/**
 * 
 * 获取所有巡检数据
 * @returns 
 */
export const getAllInspections = () => {
  return new Promise((resolve, reject) => {
    if(store.state.all_data.length){

      const tempRes = lodash.orderBy(store.state.all_data, "creatTime", ['desc']);
      const result = lodash.orderBy(tempRes, "synchronization", ['asc']);
      resolve(result)
      return; 
    }
    readInspectionData()
      .then((res) => {
        const tempRes = lodash.orderBy(res, "creatTime", ['desc']);
        const result = lodash.orderBy(tempRes, "synchronization", ['asc']);
        store.commit("SET_ALL_DATA", result);
        resolve(result)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

/**
 * 
 * 获取所有日志数据
 * @returns 
 */
export const getLogList = () => {
  return new Promise((resolve, reject) => {
    if(store.state.log_list.length){
      const tempRes = lodash.orderBy(store.state.log_list, "time", "desc");
      resolve(tempRes)
      return; 

    }

    readLogData()
      .then((res) => {
        const tempRes = lodash.orderBy(res, "time", "desc");
        store.commit("SET_LOG_LIST", tempRes);
        resolve(tempRes)
      })
      .catch((error) => {
        reject(error)
      })

  })
}



/**
 * 
 * 获取巡检数据详情
 * @returns 
 */
export const getInspectionDetails = (uid) => {
  return new Promise((resolve, reject) => {
    getAllInspections()
      .then((res) => {
        const detailsInfo = res.filter((item) => item.uid == uid)[0] || {};

        resolve(detailsInfo)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

/**
 * 
 * 获取草稿数据
 * @returns 
 */
export const getDarft = () => {
  return new Promise((resolve, reject) => {
    readDarfData()
    .then((res) => {
      store.commit("SET_DARF_DATA", res);
      resolve(res)
    })
    .catch((error) => {
      reject(error)
    })
  })
}

// 获取设备上架所有图片信息
export const getAllDeviceData = () => {
  return new Promise((resolve, reject) => {
    console.log(store.state.deviceData.length)
    if(store.state.deviceData.length){

      const tempRes = lodash.orderBy(store.state.deviceData, "creatTime", ['desc']);
      const result = lodash.orderBy(tempRes, "synchronization", ['asc']);
      resolve(result)
      return; 
    }
    readDeviceData()
      .then((res) => {
        const tempRes = lodash.orderBy(res, "creatTime", ['desc']);
        const result = lodash.orderBy(tempRes, "synchronization", ['asc']);
        store.commit("SET_DEVICEDATA", result);
        resolve(result)
      })
      .catch((error) => {
        reject(error)
      })
  })
}

/**
 * 
 * 获取设备上架数据详情
 * @returns 
 */
export const getDeviceDataDetails = () => {
  return new Promise((resolve, reject) => {
    getAllDeviceData()
      .then((res) => {
        const detailsInfo = res

        resolve(detailsInfo)
      })
      .catch((error) => {
        reject(error)
      })
  })
}