import Koa from 'koa';
import { createClientCompiler, addShims } from 'fire-webpack';
import webpackMiddleware from 'koa-webpack';
import bodyParser from 'koa-bodyparser';
import { getServiceNames, getService } from '../service/index.js';
import callServerService from '../service/server-service.js';

export {
    addShims,
};

export default async function createServer(entry) {
    const app = new Koa();
    const serviceNames = getServiceNames();
    const compiler = createClientCompiler(entry, serviceNames);

    app.use(bodyParser());
    app.use(async (context, next) => {
        if (context.path === `/_api` && context.method === `POST`) {
            try {
                const [
                    serviceName,
                    methodName,
                ] = context.request.query.method.split(`.`);

                // TODO: If context.request.body is not an array, exit.

                // TODO: There should be some sort of check to see if executing this service method is
                // allowed.
                const Service = getService(serviceName);

                await callServerService(Service, methodName, context);
            }
            catch (e) {
                context.type = `json`;
                context.body = JSON.stringify({ error: true });
                context.status = e.status || 500;
            }
        }
        else {
            await next();
        }
    });
    app.use(webpackMiddleware({
		compiler,
        dev: {
            // noInfo: true,
            stats: {
                colors: false,
            },
            // quiet: true,
        },
        hot: {
            noInfo: true,
            quiet: true,
        },
	}));
    app.use(async (context, next) => {
        await next();

        // TODO: Server-render everything? Including the css!
        // TODO: We should use jsx and renderToString?
        context.type = `html`;
        context.body = `<!doctype html>
        <html>
        <head>
          <title></title>
        </head>
        <body id="root">
        </body>
        <script src="/client.js"></script>
        </html>`;
    });
	app.listen(3000);
}

@@ -616,16 +616,17 @@
 api%60 && 
+(
 context.
@@ -642,16 +642,45 @@
 = %60POST%60
+ %7C%7C context.method === %60GET%60)
 ) %7B%0A    
@@ -1211,24 +1211,57 @@
 catch (e) %7B%0A
+                console.log(e);%0A%0A
             

allow GET api calls