import {
  getDataByTime
} from '@/api/api'
import { getDateStr } from '@/utils/util'

var ChcheData = []
var m_jh = ''
export async function getChcheData(jh, starTime, endTime, selectedConfigureKey) {
  if (getPlusMinutesData(starTime, 1000 * 60 * 5) > new Date()) {
    return null
  }
  console.log('准备进缓存取数据')
  console.log( starTime, endTime,selectedConfigureKey)
  if (m_jh != '' && m_jh == jh) {
    let returnData=[]
    if(ChcheData.length>0){
      returnData= ChcheData.filter(result => new Date(result.time) >= new Date(starTime) && new Date(result.time) <= new Date(endTime))
    }
    if (returnData.length > 0) {
      if (new Date(ChcheData[0].time) <= getPlusMinutesData(starTime, 1000 * 60 * 10) && getCutMinutesData(endTime, 1000 * 60 * 10) <= new Date(getPlusDate(ChcheData[0].time, 24))) {
         fistData(jh, getCutData(ChcheData[0].time, 24), ChcheData[0].time, selectedConfigureKey)
      } else if (new Date(getCutData(ChcheData[ChcheData.length - 1].time,24)) <= getPlusMinutesData(starTime, 1000 * 60 * 10) && getCutMinutesData(endTime, 1000 * 60 * 10) <= new Date(getPlusDate(ChcheData[ChcheData.length - 1].time, 24))) {
        lastData(jh, ChcheData[ChcheData.length - 1].time, getPlusDate(ChcheData[ChcheData.length - 1].time, 24), selectedConfigureKey)
      }
      return returnData
    } else {
      if (ChcheData.length != 0) {
       getInitData(jh, starTime, endTime, selectedConfigureKey)
      }
      return null
    }
  } else {
    getInitData(jh, starTime, endTime, selectedConfigureKey)
    m_jh = jh
    return null
  }
}
function getPlusDate(date, number) {//加小时
  return getDateStr(new Date(new Date(date).getTime() + 1000 * 60 * 60 * number))
}

function getCutData(date, number) {//减小时
  return getDateStr(new Date(new Date(date).getTime() - 1000 * 60 * 60 * number))
}

function getPlusMinutesData(date, number) {//加分钟
  return new Date(new Date(date).getTime() + number)
}

function getCutMinutesData(date, number) {//减分钟
  return new Date(new Date(date).getTime() - number)
}
 async function  getInitData(jh, starTime, endTime, selectedConfigureKey) {
  let f_time=getCutData(starTime,24)
  let t_time=getPlusDate(endTime,24)
   let params = { }

   params = { jh: jh, time1:f_time, time2: getPlusDate(starTime,12), templateId: selectedConfigureKey }
   getDataByTime(params).then((res) => {
     if (res.success && res.result.length > 0) {
       ChcheData=[...res.result,...ChcheData]
     }
   })
   params = { jh: jh, time1:getCutData(endTime,12), time2: t_time, templateId: selectedConfigureKey }
   getDataByTime(params).then((res) => {
     if (res.success && res.result.length > 0) {
       ChcheData=[...ChcheData,...res.result]
     }
   })
}
 /*function fistData(jh, starTime,endTime, selectedConfigureKey) {
  let params = { jh: jh, time1: starTime, time2: endTime, templateId: selectedConfigureKey }
         getDataByTime(params).then((res) => {
    if (res.success && res.result.length > 0) {
      ChcheData = [...res.result, ...ChcheData].filter(res => new Date(res.time) <= getCutMinutesData(ChcheData[ChcheData.length - 1].time, 1000 * 60 * 60 * 24))
    }
  })
}*/
function fistData(jh, starTime, endTime, selectedConfigureKey) {
  const numIntervals = 6;
  const intervalDuration = (Date.parse(endTime) - Date.parse(starTime)) / numIntervals;
  const promises = [];
  for (let i = 0; i < numIntervals; i++) {
    const startTimeStamp = Date.parse(starTime);
    const intervalStart = new Date(startTimeStamp + i * intervalDuration);
    const intervalEnd = Date.parse(intervalStart) + intervalDuration;
    const params = { jh: jh, time1: getDateStr(new Date(intervalStart.getTime())), time2:getDateStr(new Date(intervalEnd)) , templateId: selectedConfigureKey };
    const promise = getDataByTime(params);
    promises.push(promise);
  }
  Promise.all(promises).then((results) => {
    const mergedResults = results.reduce((acc, cur) => acc.concat(cur.result), []);
    const finalResult = { success: true, result: mergedResults };
    if (finalResult.success && finalResult.result.length > 0) {
      ChcheData = [...mergedResults, ...ChcheData].filter(res => new Date(res.time) <= getCutMinutesData(ChcheData[ChcheData.length - 1].time, 1000 * 60 * 60 * 24));
    }
  });
}

/*
async function lastData(jh, starTime,endTime, selectedConfigureKey) {
  let params = { jh: jh, time1: starTime, time2: endTime, templateId: selectedConfigureKey }
  getDataByTime(params).then((res) => {
    if (res.success && res.result.length > 0) {
      ChcheData = [...ChcheData,...res.result].filter(res => new Date(res.time) >= getPlusMinutesData(ChcheData[0].time, 1000 * 60 * 60 * 24))
    }
  })
}*/
async function lastData(jh, starTime,endTime, selectedConfigureKey) {
  const numIntervals = 6;
  const intervalDuration = (Date.parse(endTime) - Date.parse(starTime)) / numIntervals;
  const promises = [];
  for (let i = 0; i < numIntervals; i++) {
    const startTimeStamp = Date.parse(starTime);
    const intervalStart = new Date(startTimeStamp + i * intervalDuration);
    const intervalEnd = Date.parse(intervalStart) + intervalDuration;
    const params = { jh: jh, time1: getDateStr(new Date(intervalStart.getTime())), time2:getDateStr(new Date(intervalEnd)) , templateId: selectedConfigureKey };
    const promise = getDataByTime(params);
    promises.push(promise);
  }
  Promise.all(promises).then((results) => {
    const mergedResults = results.reduce((acc, cur) => acc.concat(cur.result), []);
    const finalResult = { success: true, result: mergedResults };
    if (finalResult.success && finalResult.result.length > 0) {
      ChcheData = [...ChcheData, ...mergedResults].filter(res => new Date(res.time) >= getPlusMinutesData(ChcheData[0].time, 1000 * 60 * 60 * 24))
    }
  })
}