'use strict';

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _initAbeForProcesses = require('./initAbeForProcesses');

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

// IMPORT LIB
var Page = require('../../cli').Page;
var cmsOperations = require('../../cli').cmsOperations;
var abeExtend = require('../../cli').abeExtend;
var Manager = require('../../cli').Manager;
var cmsData = require('../../cli').cmsData;
var cmsTemplates = require('../../cli').cmsTemplates;
var config = require('../../cli').config;
var templatesTexts = {};

function publishNext(files, tt, cb) {
  var i = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;

  var pub = files.shift();
  if (typeof pub !== 'undefined' && pub !== null) {

    var jsonObject = _fsExtra2.default.readJsonSync(pub[_initAbeForProcesses.processConfig.ABE_STATUS].path);
    i++;
    var p = new Promise(function (resolve) {
      if (typeof templatesTexts[jsonObject.abe_meta.template] === 'undefined' || templatesTexts[jsonObject.abe_meta.template] === null) {
        templatesTexts[jsonObject.abe_meta.template] = cmsTemplates.template.getTemplate(jsonObject.abe_meta.template);
      }

      cmsData.source.getDataList(_path2.default.dirname(jsonObject.abe_meta.link), templatesTexts[jsonObject.abe_meta.template], jsonObject, true).then(function () {
        jsonObject = abeExtend.hooks.instance.trigger('afterGetDataListOnSave', jsonObject);

        var obj = {
          type: jsonObject.abe_meta.status,
          json: {
            content: jsonObject
          }
        };

        var page = new Page(obj.json.content.abe_meta.template, templatesTexts[jsonObject.abe_meta.template], obj.json.content, true);

        cmsOperations.save.saveHtml(_path2.default.join(config.root, _initAbeForProcesses.processConfig.ABE_DESTINATION, jsonObject.abe_meta.link), page.html);

        obj = abeExtend.hooks.instance.trigger('afterSave', obj);

        (0, _initAbeForProcesses.trace)('(' + (0, _initAbeForProcesses.getTime)() + ') ' + i + ' - ' + pub[_initAbeForProcesses.processConfig.ABE_STATUS].path.replace(config.root, '').replace(config.data.url, '') + ' (tpl: ' + jsonObject.abe_meta.template + ')');
        resolve();
      }, function () {
        (0, _initAbeForProcesses.error)('generate-posts ERROR on ' + pub[_initAbeForProcesses.processConfig.ABE_STATUS].path.replace(config.root, '').replace(config.data.url, ''));
        resolve();
      });
    });

    p.then(function () {
      publishNext(files, tt, cb, i++);
    }).catch(function (e) {
      publishNext(files, tt, cb, i++);
      (0, _initAbeForProcesses.error)('error', e);
    });
  } else {
    cb(i);
  }
}

function startProcess() {
  (0, _initAbeForProcesses.log)('start publish all at path ' + _initAbeForProcesses.processConfig.ABE_PATH);
  (0, _initAbeForProcesses.log)('searching for file at ' + config.root);
  (0, _initAbeForProcesses.log)('seach status: ' + _initAbeForProcesses.processConfig.ABE_STATUS);
  (0, _initAbeForProcesses.log)('save to: ' + _path2.default.join(config.root, _initAbeForProcesses.processConfig.ABE_DESTINATION));
  var files = Manager.instance.getListWithStatusOnFolder(_initAbeForProcesses.processConfig.ABE_STATUS, _initAbeForProcesses.processConfig.ABE_PATH);

  (0, _initAbeForProcesses.log)('Found ' + files.length + ' to republish');

  publishNext(files, files.length, function (i) {
    (0, _initAbeForProcesses.log)('total ' + i + ' files');
    (0, _initAbeForProcesses.log)('publish process finished ' + (0, _initAbeForProcesses.getTime)());
    process.exit(0);
  });
}

(0, _initAbeForProcesses.init)('generate-posts', {
  ABE_STATUS: 'publish',
  ABE_PATH: '',
  ABE_DESTINATION: 'site'
}).then(startProcess, function (msg) {
  (0, _initAbeForProcesses.error)(msg);
  process.exit(0);
});