import React from 'react'
import ReactDOM from 'react-dom'
import { App } from './components/App'
import { CurrenciesLoader } from './backend/currenciesLoader'
import { ipcRenderer } from 'electron'

let start: boolean = false
let appData: VixonicData
let currencies: Currencies = {}

const currenciesLoader = new CurrenciesLoader()

currenciesLoader.onData = (update) => {
  currencies = update
  render()
}

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

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

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

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

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