var angular = require('angular');
var _ = require('jsUtil');
require('../modules');
'use strict';

var module = angular.module('info.services');

module.run(function($rootScope) {
  $rootScope.$on('serverUrl.changed', function() {
    localStorage.setItem('setSelectedModels', 'false');
    localStorage.removeItem('selectedModels');
    localStorage.removeItem('notSelectedModels');
  });
});

/**
 * 从服务器到缓存
 */
module.factory('ModelLocalStorageService', function() {
  var cacheSelectedLength = 8; // 缓存需要被选中的个数

  return {
    addOrUpdateModels: addOrUpdateModels,
    getModels: getModels,
    getSelectedModels: getSelectedModels,
    addOrUpdateModelsMd5: addOrUpdateModelsMd5,
    getModelsMd5: getModelsMd5,
    addOrUpdateModelsChange: addOrUpdateModelsChange,
    getModelsChange: getModelsChange,
    addOrUpdateShowCard: addOrUpdateShowCard,
    getShowCard: getShowCard,
    addOrUpdateImgPath: addOrUpdateImgPath,
    getImgPath: getImgPath,
    getNotSelectedModels: getNotSelectedModels,
    setNotSelectedModels: setNotSelectedModels,
    setSelectedModels: setSelectedModels,
    refreshSelectedModels: refreshSelectedModels
  };

  function addOrUpdateModels(models) {
    localStorage.setItem('models', JSON.stringify(models));
  }

  function refreshSelectedModels() {
    var models = JSON.parse(localStorage.getItem('models'));
    if (!models) {
      return;
    }
    var cacheSelectedModels = [];
    var cacheNotSelectedModels = [];
    var set = JSON.parse(localStorage.getItem('setSelectedModels'));
    var selectedModels = JSON.parse(localStorage.getItem('selectedModels'));
    if (!set && !selectedModels) {
      cacheSelectedModels = models.slice(0, cacheSelectedLength);
      cacheNotSelectedModels = models.slice(cacheSelectedLength);
    } else {
      cacheNotSelectedModels = models;
      _.each(selectedModels, function(selectedModel) {
        for (var i = 0, size = models.length; i < size; i++ ) {
          var model = models[i];
          if (selectedModel.id === model.id) {
            cacheNotSelectedModels.splice(i, 1);
            cacheSelectedModels.push(model);
            break;
          }
        }
      });
    }
    
    if (cacheSelectedModels.length <= 0) {
      cacheSelectedModels = models.slice(0, cacheSelectedLength);
      cacheNotSelectedModels = models.slice(cacheSelectedLength);
    }
    
    localStorage.setItem('selectedModels', JSON.stringify(cacheSelectedModels));
    localStorage.setItem('notSelectedModels', JSON.stringify(cacheNotSelectedModels));
  }

  function getModels() {
    var models = localStorage.getItem('models');
    return _.isEmpty(models) ? null : JSON.parse(models);
  }

  function getSelectedModels() {
    var models = localStorage.getItem('selectedModels');
    return _.isEmpty(models) ? null : JSON.parse(models);
  }

  function addOrUpdateModelsMd5(md5) {
    var jsonMd5 = JSON.stringify(md5);
    localStorage.setItem('modelsMd5', jsonMd5);
  }

  function getModelsMd5() {
    var md5 = localStorage.getItem('modelsMd5');

    return _.isEmpty(md5) ? null : JSON.parse(md5);
  }

  function addOrUpdateModelsChange(modelsChange) {
    var jsonModelsChange = JSON.stringify(modelsChange);
    localStorage.setItem('modelsChange', jsonModelsChange);
  }

  function getModelsChange() {
    var modelsChange = localStorage.getItem('modelsChange');
    return _.isEmpty(modelsChange) ? null : JSON.parse(modelsChange);
  }

  function addOrUpdateShowCard(showCard) {
    var jsonShowCard = JSON.stringify(showCard);
    localStorage.setItem('showCard', jsonShowCard);
  }

  function getShowCard() {
    var showCard = localStorage.getItem('showCard');
    return _.isEmpty(showCard) ? null : JSON.parse(showCard);
  }

  function addOrUpdateImgPath(id, url) {
    window.localStorage.setItem('info_image_url_' + id, url);
  }

  function getImgPath(id) {
    var url = window.localStorage.getItem('info_image_url_' + id);
    return url && url.indexOf("\"") == 0? JSON.parse(url) : url;
  }

  function getNotSelectedModels() {
    var models = localStorage.getItem('notSelectedModels');
    return _.isEmpty(models) ? null : JSON.parse(models);
  }

  function setNotSelectedModels(models) {
    localStorage.setItem('notSelectedModels', JSON.stringify(models));
  }

  function setSelectedModels(models) {
    localStorage.setItem('setSelectedModels', "true");
    localStorage.setItem('selectedModels', JSON.stringify(models));
  }
})

