"use strict";
var Handlebars = require('handlebars/dist/handlebars');
var SharedConstants = require('../../shared/constants/SharedConstants');
var _ = require('lodash/lang');

const lodashCloneDeep = require('lodash.clonedeep');

module.exports = {
    writeSystemHeaderValue: function(doc, handlebarsSource) {
        var result;

        if (handlebarsSource && doc) {
            var hbTemplate;
            hbTemplate = Handlebars.compile(handlebarsSource);
            result = hbTemplate(doc);
        }

        return result;
    },

    writeSystemHeaderSummary: function(newRecord, template, oldRecord, updates) {
        logger.debug("Writing system header summary");

        if(template) {
            var nameSource;
            var descriptionSource;

            if (template.summaryNameRule || template.summaryDescriptionRule) {
                nameSource = template.summaryNameRule;
                descriptionSource = template.summaryDescriptionRule;

                if (newRecord) {

                    var hbDocument;
                    if (oldRecord) {
                        hbDocument = lodashCloneDeep(oldRecord);
                        //applying updates for Handlebars compiling purpose only
                        for (var key in newRecord) {
                            hbDocument[key] = newRecord[key];
                        }
                    } else {
                        hbDocument = lodashCloneDeep(newRecord);
                    }

                    var hbTemplate;
                    if (nameSource){
                        hbTemplate = Handlebars.compile(nameSource);

                        if (updates && updates.systemHeader && updates.systemHeader.summaryName) {
                            newRecord.systemHeader.summaryName = updates.systemHeader.summaryName;
                        } else {
                            newRecord.systemHeader.summaryName = hbTemplate(hbDocument);
                        }
                    }
                    if (descriptionSource){
                        hbTemplate = Handlebars.compile(descriptionSource);

                        if (updates && updates.systemHeader && updates.systemHeader.summaryDescription) {
                            newRecord.systemHeader.summaryDescription = updates.systemHeader.summaryDescription;
                        } else {
                            newRecord.systemHeader.summaryDescription = hbTemplate(hbDocument);
                        }
                    }
                }
            }
        }

    },

    writeSystemHeaderCreated: function (newRecord, operationType, user, now) {
        // Write on insert and update.
        // The created fields represent when and who created this version of the document.

        if (operationType === SharedConstants.INSERT_OPERATION_TYPE
                || operationType === SharedConstants.UPDATE_OPERATION_TYPE) {

            if (!newRecord.systemHeader.createdDate) {
                newRecord.systemHeader.createdDate = (now ? now : new Date());
            }
            if (user) {
                newRecord.systemHeader.createdBy = user.documentId;
            }
        }

        if (operationType === SharedConstants.INSERT_OPERATION_TYPE){
          if (!newRecord.systemHeader.documentCreatedDate) {
            newRecord.systemHeader.documentCreatedDate = (now ? now : new Date());
          }
          if (user && !newRecord.systemHeader.documentCreatedBy) {
            newRecord.systemHeader.documentCreatedBy = user.documentId;
          }
        }
    }

};
