import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import createAppStore from './redux/store/createAppStore';
import {getIPCRenderer} from './utils';
import packageJson from '../package.json';
import {Provider} from 'react-redux';
import {setActiveRelease} from './redux/actions/release_version_actions';
import {setProviderState} from './redux/actions/mount_actions';
import * as serviceWorker from './serviceWorker';

const Constants = require('./constants');

const ipcRenderer = getIPCRenderer();
let store;

if (ipcRenderer) {
  ipcRenderer.on(Constants.IPC_Get_Platform_Reply, (event, arg) => {
    if (arg.Platform === 'linux') {
      const root = document.documentElement;
      root.style.setProperty('--default_font_size', '15px');
    }

    store = createAppStore(arg.Platform, arg.AppPlatform, packageJson.version);
    ipcRenderer.on(Constants.IPC_Get_State_Reply, (event, arg) => {
      if (arg.data) {
        store.dispatch(setActiveRelease(arg.data.Release, arg.data.Version));

        for (const provider of Constants.PROVIDER_LIST) {
          let state = arg.data[provider] || this.props.ProviderState[provider];
          if (state.AutoMount === undefined) {
            state['AutoMount'] = false;
          }
          if (state.AutoRestart === undefined) {
            state['AutoRestart'] = false;
          }
          store.dispatch(setProviderState(provider, state));
        }
      }
      ReactDOM.render((
        <Provider store={store}>
          <App/>
        </Provider>
      ), document.getElementById('root'));
      serviceWorker.unregister();
    });
    ipcRenderer.send(Constants.IPC_Get_State);
  });
  ipcRenderer.send(Constants.IPC_Get_Platform);
}

