'use strict';
const express = require('express');
const path = require('path');
const fs = require('fs');
const shortid = require('shortid');

function mockApi(libDir, packageInfo, { upload_token } = config) {
  const skinMetaJSON = path.resolve(libDir + '/skin.json');
  const contentMetaJSON = path.resolve(libDir + '/content.json');
  const presetsJSON = path.resolve(libDir + '/presets.json');
  const demoJSON = path.resolve(libDir + '/demo.json');
  let guid = "workbench_";
  let compositions = [];

  let skins = [];
  const getSkinMeta = () => JSON.parse(fs.readFileSync(skinMetaJSON));
  const getContentMeta = () => JSON.parse(fs.readFileSync(contentMetaJSON));
  const getDemoContent = () => JSON.parse(fs.readFileSync(demoJSON));
  const getPresets = () =>
    JSON.parse(fs.readFileSync(presetsJSON)).map(
      preset => (preset.widget_id = 'widget') && preset
    );
  const getSkins = () => getPresets().concat(skins);

  const app = express();

  app.set('views', path.join(__dirname, '/views'));
  app.set('view engine', 'pug');

  app.get('/editor.html', (req, res) => {
    res.render('editor');
  });

  app.get('/widgets/:id', (req, res) => {
    res.json({
      id: 'widget',
      category: 'dev',
      name: packageInfo.title,
      module: packageInfo.name,
      skinMeta: getSkinMeta(),
      contentMeta: getContentMeta(),
      skins: getPresets(),
      content: getDemoContent(),
    });
  });

  app.get('/widgets/:id/embed.html', (req, res) => {
    res.render('widget', {
      widget: {
        id: 'widget',
        category: 'dev',
        module: packageInfo.name,
        skinMeta: getSkinMeta(),
        contentMeta: getContentMeta(),
        skins: getPresets(),
        content: getDemoContent(),
      },
      user: {
        id: 'devuser',
        upload_token,
        premium: true,
      },
    });
  });

  app.get('/compositions', function(req, res) {
    res.json(compositions);
  });

  app.post('/compositions', function(req, res) {
    let composition = req.body;
    composition.id = guid + shortid.generate();
    // console.log("mockup api - on create composition id:", composition.id);
    composition.user_id = 'mock-user';
    composition.content.map(c => (c.composition_id = composition.id));

    compositions.push(composition);

    res.json(composition);
  });

  app.post('/compositions/:id', function(req, res) {
    compositions = compositions.map(function(comp) {
      if (comp.id === req.params.id) return req.body;
      return comp;
    });

    res.json(req.body);
  });

  app.get('/compositions/:id', function(req, res) {
    res.json(
      compositions.filter(function(comp) {
        return comp.id === req.params.id;
      })[0]
    );
  });

  app.get('/compositions/:id/embed.html', function(req, res) {
    var composition = compositions.filter(function(comp) {
      return comp.id === req.params.id;
    })[0];
    var skin = getSkins().filter(function(skin) {
      return skin.id === composition.skin_id;
    })[0];
    var widget = {
      id: 'widget',
      category: 'dev',
      module: packageInfo.name,
      skinMeta: getSkinMeta(),
      contentMeta: getContentMeta(),
      skins: getPresets(),
      content: getDemoContent(),
    };
    var user = {
      id: 'devuser',
      upload_token,
      premium: true,
    };
    res.render('embed', { composition, skin, widget, user });
  });

  app.get('/skins', function(req, res) {
    res.json(getSkins());
  });

  app.post('/skins', function(req, res) {
    req.body.id = guid + shortid.generate();
    // console.log("mockup api - on create skin id:", req.body.id);
    req.body.user_id = 'mock-user';

    skins.push(req.body);

    res.json(req.body);
  });

  app.post('/skins/:id', function(req, res) {
    skins = skins.map(function(skin) {
      if (skin.id === req.params.id) return req.body;
      return skin;
    });

    res.json(req.body);
  });

  return app;
}

module.exports = mockApi;
