import React from 'react'
import ReactDOM from 'react-dom'
import { App } from './App'
import { AnniversaryLoader } from './anniversary-loader'
const { ipcRenderer } = require('electron')

let start: boolean = false
let appData: VixonicData
let anniversaries: Anniversary[] = []

const anniversariesLoader = new AnniversaryLoader()

anniversariesLoader.onData = (update) => {
  anniversaries = update
  render()
}

ipcRenderer.on('preload', (_event: any, data: VixonicData) => {
  appData = data
  anniversariesLoader.update(data)
  render()
})

ipcRenderer.on('start', (_event: any, data: VixonicData) => {
  appData = data
  anniversariesLoader.update(data)
  start = true
  render()
})

ipcRenderer.on('update', (_event: any, data: VixonicData) => {
  appData = data
  anniversariesLoader.update(data)
  render()
})

ipcRenderer.on('finish', (_event: any) => {
  anniversariesLoader.stop()
  console.log('finish')
})

function render() {
  ReactDOM.render(
    React.createElement(App, { start, appData, anniversaries }), document.getElementById('root')
  )
}
