const fs = require('fs')
const { DateTime } = require('luxon')

const generateData = (startingdate, endingDate) => {
  let parsingDate = DateTime.local(
    startingdate.year,
    startingdate.month,
    startingdate.day
  )

  let lastDay = parsingDate.day
  const dayDumpArray = []

  let lastMonth = parsingDate.month
  const monthDumpArray = []

  let lastYear = parsingDate.year
  const yearDumpArray = []

  let dailyLoad = 0
  let monthlyLoad = 0
  let yearlyLoad = 0

  const dumpArray = []
  while (parsingDate <= endingDate) {
    const load = parseInt(Math.random() * 1000) / 100
    dailyLoad += load
    monthlyLoad += load
    yearlyLoad += load

    dumpArray.push({
      load: load,
      year: parsingDate.year,
      month: parsingDate.month,
      day: parsingDate.day,
      hour: parsingDate.hour,
      minute: parsingDate.minute,
    })

    if (parsingDate.day !== lastDay) {
      dayDumpArray.push({
        load: Math.round(dailyLoad * 100) / 100,
        year: parsingDate.year,
        month: lastMonth,
        day: lastDay,
        hour: 0,
        minute: 0,
      })
      dailyLoad = 0
      lastDay = parsingDate.day
    }

    if (parsingDate.month !== lastMonth) {
      monthDumpArray.push({
        load: Math.round(monthlyLoad * 100) / 100,
        year: lastYear,
        month: lastMonth,
        day: 0,
        hour: 0,
        minute: 0,
      })
      monthlyLoad = 0
      lastMonth = parsingDate.month
    }

    if (parsingDate.year !== lastYear) {
      yearDumpArray.push({
        load: Math.round(yearlyLoad * 100) / 100,
        year: lastYear,
        month: 0,
        day: 0,
        hour: 0,
        minute: 0,
      })
      yearlyLoadLoad = 0
      lastYear = parsingDate.year
    }

    parsingDate = parsingDate.plus({ minutes: 30 })
  }

  return {
    hourlyLoad: dumpArray,
    dailyLoad: dayDumpArray,
    monthlyLoad: monthDumpArray,
    yearlyLoad: yearDumpArray,
  }
}

const startingdate = DateTime.local(2020, 1, 1)
const endingDate = DateTime.local(2020, 4, 15)

const Eelec = generateData(startingdate, endingDate)
const Gaz = generateData(startingdate, endingDate)
const Eau = generateData(startingdate, endingDate)

//const dt2 = dt.plus({days:1})
/*

fetchData = (y1,y2,withHourlyDetail) =>
{
  const dumpArray =[];

  for (year = y1; year <= y2; year++) {
    for (month = 1; month <= 12; month++) {
      for (day = 1; day <= 30; day++) {
        if (withHourlyDetail)
        {
          for(h = 0; h<=23;h++)
          {
            for(m=0;m<=1;m++)
            {
              const date =
              {
                year: year,
                month: month,
                day: day,
                hour: h,
                minute: m*30,
                load: (parseInt(Math.random() * 10000) / 100)
              }
              dumpArray.push(date);
            }
          }
        }
      else
      {
          const date =
          {
            year: year,
            month: month,
            day: day,
            hour: 0,
            minute: 0,
            load: (parseInt(Math.random() * 10000) / 100)
          }

          dumpArray.push(date);
      }

      }
    }
  }
  return dumpArray;
}

*/
const dump = {
  'io.enedis.minute': Eelec.hourlyLoad,
  'io.enedis.day': Eelec.dailyLoad,
  'io.enedis.month': Eelec.monthlyLoad,
  'io.enedis.year': Eelec.yearlyLoad,

  'io.grdf.hour': Gaz.hourlyLoad,
  'io.grdf.day': Gaz.dailyLoad,
  'io.grdf.month': Gaz.monthlyLoad,
  'io.grdf.year': Gaz.yearlyLoad,

  //"io.egl.hour": Eau.hourlyLoad,
  'io.egl.day': Eau.dailyLoad,
  'io.egl.month': Eau.monthlyLoad,
  'io.egl.year': Eau.yearlyLoad,
}

const dumpString = JSON.stringify(dump)

fs.writeFile('dayData.json', dumpString, function(err, result) {
  if (err) console.log('error', err)
})