import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from "redux";
import createIpc from "redux-electron-ipc";

import { BrowserRouter, Route, Switch } from "react-router-dom";

import reducers from "./reducers";

import TagsIndex from "./components/TagsIndex";
import Settings from "./components/Settings";
import Header from "./components/Header";
import Permissives from "./components/Permissives";
import Main from "./components/Main";
import Controls from "./components/Controls";
import EventLog from "./components/EventLog";

import { ipcTagUpdate } from "./actions/actions_tags";
import { ipcPlcDetailsReceived, ipcPlcErrorReceived } from "./actions/actions_plc";
import { ipcTagHistoryUpdate } from "./actions/actions_taghistory";

export const { history_tags: historyTags, event_tags: eventTags } = require("../../tagList.json");
export const historyPoints = 5000;


const ipc = createIpc({
	"tag:valueupdate": ipcTagUpdate,
	"plc:connected": ipcPlcDetailsReceived,
	"plc:error": ipcPlcErrorReceived,
	"tag:history": ipcTagHistoryUpdate
});

const createStoreWithMiddleware = applyMiddleware(ipc)(createStore);

ReactDOM.render(
	<Provider store={createStoreWithMiddleware(reducers)}>
		<BrowserRouter>
			<div>
				<Header />
				<Switch>
					<Route path="/settings" component={Settings} />
					<Route path="/permissives" component={Permissives} />
					<Route path="/alltags" component={TagsIndex} />
					<Route path="/controls" component={Controls} />
					<Route path="/events" component={EventLog} />
					<Route path="/" component={Main} />
				</Switch>
			</div>
		</BrowserRouter>
	</Provider>
	, document.querySelector("#app"));
