'use strict'

const config = require('./config');
const Repository = require('./lib/repository');

const _ = require('lodash');
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const tus = require('tus-node-server');
const jwt = require('jsonwebtoken');
const path = require('path');
const resolvePath = require('resolve-path');

const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackConfig = require('./webpack.config.js');

(async () =>
{
  try
  {
    const isDevelopment = process.env.NODE_ENV !== 'production';

    const repository = new Repository(config.database);
    await repository.load();

    const tusServer = new tus.Server();
    tusServer.datastore = new tus.FileStore({
        path: config.fileUpload.url,
        directory: config.fileUpload.path
    });

    const app = express();

    app.disable('x-powered-by');

    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(bodyParser.json());



    app.use('/', require('./lib/api/upload')(repository, tusServer));
    app.use('/token', require('./lib/api/token')(repository));
    app.use('/admin', require('./lib/api/admin')(repository));


    // Frontend
    if (isDevelopment)
    {
      const compiler = webpack(webpackConfig);

      app.use(webpackDevMiddleware(compiler, {
        publicPath: webpackConfig.output.publicPath
      }));

      app.use(webpackHotMiddleware(compiler));
    }


    // Automatic fallback support for file icons
    app.get('/images/fileicons/:format/:filename', (req, res) =>
    {
      var basePath;
      var filePath;

      try
      {
        basePath = resolvePath('./public/dist/images/fileicons/', req.params.format);
        filePath = resolvePath(basePath, req.params.filename);
      }
      catch (err)
      {
        res.sendStatus(404);
      }

      fs.stat(filePath, (err, stat) =>
      {
        if (err)
          res.sendFile(resolvePath(basePath, '_blank.png'));
        else
          res.sendFile(filePath);
      });
    });


    app.use(express.static(path.join(__dirname, 'custom')));
    app.use(express.static(path.join(__dirname, 'public', 'dist')));


    // Redirects to make Vue-router URLs less quirky
    app.get('/c/:code', (req, res) => { res.redirect(301, '/#/c/' + req.params.code) });
    app.get('/admin', (req, res) => { res.redirect(301, '/#/admin/') });



    var server = app.listen(config.port, () => console.log('Recv running on port ' + server.address().port));
  }
  catch (e)
  {
    console.log(e);
    process.exit(1);
  }
})();