'use strict';

const unhandledRejection = require("unhandled-rejection");

unhandledRejection().on("unhandledRejection", (error) => {
	throw error;
});

const reactHotLoader = require("react-hot-loader");

reactHotLoader.setConfig({
	pureSFC: true
});

const Promise = require("bluebird");
const documentReadyPromise = require("document-ready-promise");
const React = require("react");
const ReactDOM = require("react-dom");
const createReactClass = require("create-react-class");
const immutable = require("immutable");
const defaultValue = require("default-value");

const WindowManager = require("./components/window-manager");
const ViewManager = require("./components/view-manager");
const NotificationArea = require("./components/notification-area");

const createWindowStore = require("./stores/windows");
const createNotificationStore = require("./stores/notifications");
const router = require("./router");



// const Form = require("./components/view-manager/form");


/*
{
	id: 1,
	title: "Foo",
	width: 400,
	height: 300,
	initialX: 20,
	initialY: 20,
	contents: <ViewManager.ViewManager router={router} initialPath="/" onAction={this.onAction} />
}, {
	id: 2,
	title: "Bar",
	width: 500,
	height: 350,
	initialX: 200,
	initialY: 80,
	contents: <ViewManager.ViewManager router={router} initialPath="/node/create" onAction={this.onAction} />
}
*/

/* NOTE: This wrapper exists due to pureSFC use: https://github.com/gaearon/react-hot-loader/issues/1088#issuecomment-434912466 */
let AppWrapper = reactHotLoader.hot(module)(createReactClass({
	displayName: "AppWrapper",
	render: function () {
		return <App />;
	}
}));

let App = createReactClass({
	displayName: "App",
	getInitialState: function () {
		return {
			// windows: immutable.List([{
			// 	id: 1,
			// 	title: "Foo",
			// 	width: 400,
			// 	height: 300,
			// 	initialX: 20,
			// 	initialY: 20,
			// 	contents: <ViewManager.ViewManager router={router} initialPath="/" onAction={this.onAction} />
			// }]),
			windows: createWindowStore({
				onUpdated: () => {
					this.forceUpdate();
				}
			}),
			notifications: createNotificationStore({
				onUpdated: () => {
					this.forceUpdate();
				}
			})
		};
	},
	onAction: function (action) {
		if (action.type === "open") {
			this.state.windows.add({
				title: "New",
				initialWidth: defaultValue(action.options.width, 300),
				initialHeight: defaultValue(action.options.height, 220),
				initialX: defaultValue(action.options.x, 20),
				initialY: defaultValue(action.options.y, 100),
				resizable: defaultValue(action.options.resizable, true),
				contents: <ViewManager.ViewManager router={router} initialPath={action.path} onAction={this.onAction} />
			});
		} else if (action.type === "notify") {
			this.state.notifications.add({
				title: action.options.title,
				message: action.message,
				type: action.options.type,
				timeout: defaultValue(action.options.timeout, 5000),
				controls: action.options.controls
			});
		} else {
			console.warn(`Unhandled action of type: ${action.type}`);
		}
	},
	render: function () {
		return (
			<div className="app">
				<WindowManager store={this.state.windows}>
					<div id="mainToolbar">
						<ViewManager.ViewManager router={router} initialPath="/static/header-menu" onAction={this.onAction} />
					</div>
					<NotificationArea store={this.state.notifications} />
				</WindowManager>
			</div>
		);
	}
});

Promise.try(() => {
	return documentReadyPromise();
}).then(() => {
	ReactDOM.render(<AppWrapper />, document.getElementById("app"));
});
