"use strict";

const Promise = require("bluebird");
const React = require("react");

const createRouter = require("./hybrid-router");
const ViewManager = require("./components/view-manager");

function PageOne({foo}) {
	return (
		<div>
			Page one ({foo})
			<ViewManager.Link url="/page2">Next page</ViewManager.Link>
		</div>
	);
}

function PageTwo({foo}) {
	return (<div>Page two ({foo})</div>);
}

function SomeForm() {
	return (
		<ViewManager.Form method="post" url="/some-form">
			<input type="text" name="foo" placeholder="foo" />
			<input type="text" name="bar" placeholder="bar" />
			<input type="text" name="baz" placeholder="baz" />
			<button type="submit">Submit!</button>
		</ViewManager.Form>
	);
}

function SubmittedForm({data}) {
	return (
		<pre>{require("util").inspect(data)}</pre>
	);
}

let router = createRouter();

router.get("/actions/create-new-node", (req, res) => {
	res.open("/");
});

router.get("/node/create", (req, res) => {
	res.render(require("./views/node/create"));
});

router.get("/static/header-menu", (req, res) => {
	res.render(require("./views/header-menu"));
});

router.post("/add-source", (req, res) => {
	return Promise.try(() => {
		return req.pass();
	}).then((response) => {
		res.notify(req.body.url);
	});
});

router.get("/", (req, res) => {
	res.render(PageOne, {
		foo: "bar"
	});
});

router.get("/page2", (req, res) => {
	res.render(PageTwo, {
		foo: "baz"
	});

	res.open("/some-form", {
		x: 40,
		y: 50
	});

	res.notify("A thing happened!", {
		title: "Notification",
		type: "info"
	});
});

router.get("/some-form", (req, res) => {
	res.render(SomeForm);
});

router.post("/some-form", (req, res) => {
	res.render(SubmittedForm, {
		data: req.body
	});
});

module.exports = router;
