'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _cli = require('../../cli');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var page = function page(req, res) {
  var html = req.query.html ? true : false;
  var json = null;
  var editor = false;
  if (typeof req.body.json !== 'undefined' && req.body.json !== null) {
    editor = true;
    if (typeof req.body.json === 'string') {
      json = JSON.parse(req.body.json);
    } else {
      json = req.body.json;
    }
  }

  var filepath = req.originalUrl.replace('/abe/page', '');

  if (typeof filepath !== 'undefined' && filepath !== null) {
    var jsonPath = null;
    var linkPath = null;

    var filePathTest = _cli.cmsData.revision.getDocumentRevision(filepath);
    if (typeof filePathTest !== 'undefined' && filePathTest !== null) {
      jsonPath = filePathTest.path;
      linkPath = filePathTest.abe_meta.link;
    }

    if (jsonPath === null || !_cli.coreUtils.file.exist(jsonPath)) {
      res.status(404).send('Not found');
      return;
    }

    if (typeof json === 'undefined' || json === null) {
      json = _cli.cmsData.file.get(jsonPath);
    }

    var meta = _cli.config.meta.name;

    var templateId = '';
    if (json[meta] && json[meta].template) {
      templateId = json[meta].template;
    } else {
      templateId = req.params[0];
    }
    var text = _cli.cmsTemplates.template.getTemplate(templateId, json);

    if (!editor) {

      _cli.cmsData.source.getDataList(_path2.default.dirname(linkPath), text, json).then(function () {
        var page = new _cli.Page(templateId, text, json, html);
        res.set('Content-Type', 'text/html');
        res.send(page.html);
      }).catch(function (e) {
        console.error(e);
      });
    } else {
      text = _cli.cmsData.source.removeDataList(text);
      var page = new _cli.Page(templateId, text, json, html);
      res.set('Content-Type', 'text/html');
      res.send(page.html);
    }
  } else {
    // not 404 page if tag abe image upload into each block
    if (/upload%20image/g.test(req.url)) {
      var b64str = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
      var img = new Buffer(b64str, 'base64');

      res.writeHead(200, {
        'Content-Type': 'image/jpeg',
        'Content-Length': img.length
      });
      res.end(img);
    } else {
      res.status(404).send('Not found');
    }
  }
};

exports.default = page;