/*
 * Create dummy data files for each fluid.
 * Hourly data is not generated
 */
const fs = require('fs')
const { DateTime } = require('luxon')

function getRandomInt(min, max) {
  const minValue = Math.ceil(min * 100)
  const maxValue = Math.floor(max * 100)
  const result =
    (Math.floor(Math.random() * (maxValue - minValue)) + minValue) / 100
  return result
}

const generateData = (startingdate, endingDate, min, max) => {
  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 monthlyLoad = 0
  let yearlyLoad = 0

  while (parsingDate <= endingDate) {
    const load = getRandomInt(min, max)

    monthlyLoad += load
    yearlyLoad += load

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

    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: 1,
        day: 1,
        hour: 0,
        minute: 0,
      })
      yearlyLoad = 0
      lastYear = parsingDate.year
    }
    lastDay = parsingDate.day
    parsingDate = parsingDate.plus({ days: 1 })
  }

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

  yearDumpArray.push({
    load: Math.round(yearlyLoad * 100) / 100,
    year: lastYear,
    month: 1,
    day: 1,
    hour: 0,
    minute: 0,
  })

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

const startingdate = DateTime.local()
  .plus({ days: -120 })
  .startOf('day')
const endingDate = DateTime.local()
  .plus({ days: -1 })
  .startOf('day')

const Elec = generateData(startingdate, endingDate, 3, 10)
const Gaz = generateData(startingdate, endingDate, 16, 68)
const Eau = generateData(startingdate, endingDate, 200, 300)

const dumpElec = {
  'io.enedis.day': Elec.dailyLoad,
  'io.enedis.month': Elec.monthlyLoad,
  'io.enedis.year': Elec.yearlyLoad,
}
const dumpStringElec = JSON.stringify(dumpElec)
fs.writeFile('data/dayData-elec.json', dumpStringElec, function(err, result) {
  if (err) console.log('error', err)
})

const dumpGas = {
  'io.grdf.day': Gaz.dailyLoad,
  'io.grdf.month': Gaz.monthlyLoad,
  'io.grdf.year': Gaz.yearlyLoad,
}
const dumpStringGas = JSON.stringify(dumpGas)
fs.writeFile('data/dayData-gas.json', dumpStringGas, function(err, result) {
  if (err) console.log('error', err)
})

const dumpWater = {
  'io.egl.day': Eau.dailyLoad,
  'io.egl.month': Eau.monthlyLoad,
  'io.egl.year': Eau.yearlyLoad,
}
const dumpStringWater = JSON.stringify(dumpWater)
fs.writeFile('data/dayData-water.json', dumpStringWater, function(err, result) {
  if (err) console.log('error', err)
})

const dump = {
  'io.enedis.day': Elec.dailyLoad,
  'io.enedis.month': Elec.monthlyLoad,
  'io.enedis.year': Elec.yearlyLoad,

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

  'io.egl.day': Eau.dailyLoad,
  'io.egl.month': Eau.monthlyLoad,
  'io.egl.year': Eau.yearlyLoad,
}

const dumpString = JSON.stringify(dump)

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