"use strict";

var RedisKeys = require('../constants/RedisKeys');
var redisDataService = require('../redis/RedisDataService');
var q = require("q");

/**
 * set a component in the cache
 * @param {*} key
 * @param {*} doc
 */
var setComponent = function(key, component, userId) {
    var redisKey;
    if (key && component && component.name) {
        redisKey = RedisKeys.DOCUMENTS_COMPONENTS_PREFIX + key;
        redisDataService.setData(redisKey, JSON.stringify(component));
    } else if (component && component.serviceName){
        redisKey = RedisKeys.DOCUMENTS_COMPONENT_LIBRARY_PREFIX + component.serviceName;
        redisDataService.setData(redisKey, JSON.stringify(component));
    }
};

/**
 * get the component from the cache
 * @param {*} key
 */
var getComponent = function(key, isComponentLibrary, userId, options) {
    var deferred = q.defer();

    if (key) {
        var redisKey;

        if (isComponentLibrary){
            redisKey = RedisKeys.DOCUMENTS_COMPONENT_LIBRARY_PREFIX + key;
        }
        else{
            redisKey = RedisKeys.DOCUMENTS_COMPONENTS_PREFIX + key;
        }

        redisDataService.getData(redisKey, userId, options).then(result => {
            if (result) {
                deferred.resolve(result);
            } else {
                deferred.resolve();
            }
        }).catch(err => deferred.reject(err));
    } else {
        deferred.reject('No key specified!');
    }

    return deferred.promise;
};

/**
 * remove the component from the cache. This will be called when the component is updated so it will be reloaded on next access
 * @param {*} componentDocument - a component document. The complete document is passed in so that it can delete by different fields
 * like documentId or name
 */
var removeComponent = function(componentDocument) {
    var deferred = q.defer();

     if (componentDocument && componentDocument.name ) {

         var redisKey = RedisKeys.DOCUMENTS_COMPONENTS_PREFIX + componentDocument.name;
         redisDataService.removeData(redisKey, function (err, response) {
             if (err) {
                 deferred.reject(err);
             }

             var redisKey = RedisKeys.DOCUMENTS_SCRIPTS_PREFIX + componentDocument.name;
             redisDataService.removeData(redisKey, function (err, response) {
                 if (err) {
                     deferred.reject(err);
                 } else {
                     deferred.resolve();
                 }
             });
         });
     }else if (componentDocument && componentDocument.serviceName) {
         //removing component library document
         var redisKey = RedisKeys.DOCUMENTS_COMPONENT_LIBRARY_PREFIX + componentDocument.serviceName;
         redisDataService.removeData(redisKey, function(err, response) {
             if (err) {
                 deferred.reject(err);
             } else{
                 deferred.resolve();
             }
         });
    } else {
        deferred.resolve();
    }

    return deferred.promise;
};

module.exports = {
    setComponent: setComponent,

    getComponent: getComponent,

    removeComponent: removeComponent
};
