'use strict';

var q = require("q");
var RedisKeys = require('../constants/RedisKeys');
var { getRedisClient } = require('./RedisClient');

const get = key => {
    var deferred = q.defer();

  getRedisClient().get(key, function(err, jsonString) {
        if (err) deferred.resolve();
        deferred.resolve(JSON.parse(jsonString));
    });

    return deferred.promise;
};

const set = (key, json) => {
  getRedisClient().set(key, JSON.stringify(json));
};

const del = (key) => {
    var deferred = q.defer();

  getRedisClient().del(key, function(err, response) {
        if (err) {
            deferred.reject(err);
        } else{
            deferred.resolve();
        }
    });

    return deferred.promise;
};

/* PUBLIC API */

const getAccount = userId => {
    var redisKey = RedisKeys.DOCUMENTS_ACCOUNTS_PREFIX + userId;
    logger.debug("Got account from Redis: " + redisKey);
    return get(redisKey);
};

const setAccount = user => {
    var redisKey = RedisKeys.DOCUMENTS_ACCOUNTS_PREFIX + user.documentId;
    logger.info("Set account to Redis: " + redisKey);
    set(redisKey, user);
};

const getAccountSecurity = (userId) => {
    var redisKey = RedisKeys.DOCUMENTS_ACCOUNT_SECURITY_PREFIX + userId;
    logger.debug("Got accountSecurity from Redis: " + redisKey);
    return get(redisKey);
};

const setAccountSecurity = (userId, accountSecurity) => {
    var redisKey = RedisKeys.DOCUMENTS_ACCOUNT_SECURITY_PREFIX + userId;
    set(redisKey, accountSecurity);
};

const removeAccountSecurity = (userId) => {
    var redisKey = RedisKeys.DOCUMENTS_ACCOUNT_SECURITY_PREFIX + userId;
    return del(redisKey);
};

const getAccountControl = userId => {
    var redisKey = RedisKeys.DOCUMENTS_ACCOUNT_CONTROL_PREFIX + userId;
    return get(redisKey);
};

const setAccountControl = (userId, accountControl) => {
    var redisKey = RedisKeys.DOCUMENTS_ACCOUNT_CONTROL_PREFIX + userId;
    set(redisKey, accountControl);
};

const removeAccountControl = (userId) => {
    var redisKey = RedisKeys.DOCUMENTS_ACCOUNT_CONTROL_PREFIX + userId;
    return del(redisKey);
};

const removeCachedAccount = user => {
    var redisKey = RedisKeys.DOCUMENTS_ACCOUNTS_PREFIX + user.documentId;
    return del(redisKey);
};

const getAccountGroupConfig = userId => {
  var redisKey = RedisKeys.DOCUMENTS_ACCOUNT_GROUP_CONFIG_PREFIX + userId;
  return get(redisKey);
};

const setAccountGroupConfig = (userId, accountGroupConfig) => {
  const redisKey = RedisKeys.DOCUMENTS_ACCOUNT_GROUP_CONFIG_PREFIX + userId;
  set(redisKey, accountGroupConfig);
};

const removeAccountGroupConfig = (userId) => {
  const redisKey = RedisKeys.DOCUMENTS_ACCOUNT_GROUP_CONFIG_PREFIX + userId;
  return del(redisKey);
};

const setAccountSessionIds = (userId, sessionId) => {
  const redisKey = RedisKeys.ACCOUNTS_SESSION_IDS_PREFIX + userId;
  const redisClient = getRedisClient();
  redisClient.sadd(redisKey,  sessionId);
};


const removeAccountSessions = (userId) => {
  const redisKey =  RedisKeys.ACCOUNTS_SESSION_IDS_PREFIX + userId;
  const redisClient = getRedisClient();
  redisClient.smembers(redisKey, function (err, sessionIds) {
    sessionIds.forEach( sessionId => {
      const redisSessionKey = "sess:" + sessionId;
      del(redisSessionKey);
      redisClient.srem(redisKey, sessionId );
    });
  });
};


module.exports = {

    getAccount,

    setAccount,

    removeCachedAccount,

    getAccountSecurity,

    setAccountSecurity,

    removeAccountSecurity,

    getAccountControl,

    setAccountControl,

    removeAccountControl,

    getAccountGroupConfig,

    setAccountGroupConfig,

    removeAccountGroupConfig,

    setAccountSessionIds,

    removeAccountSessions


};
