'use strict';

var angular = require('angular'),
  _ = require('jsUtil');
require('../modules');
require('./ModelRemoteService');
require('./ModelLocalStorageService');

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

module.factory('ModelService', function($q, ModelRemoteService, ModelLocalStorageService) {

  var modelStorage = ModelLocalStorageService;
  var modelService = {
    getLatestInfos: getLatestInfos,
    alreadyStorage: alreadyStorage,
    getModel: getModel,
    refreshStorageModel: refreshStorageModel,
    getImgPath: getImgPath,
    getModelsChange: getModelsChange,
    refreshShowCard: refreshShowCard,
    getShowCard: getShowCard,
    getModelOfId: getModelOfId,
    getNotSelectedModel: getNotSelectedModel,
    setStorageSelectedModel: setStorageSelectedModel,
    setStorageNotSelectedModel: setStorageNotSelectedModel,
    addOrUpdateModelsChange: addOrUpdateModelsChange,
    getSelectedModels: getSelectedModels
  };

  return modelService;

  /**
   * 获取首页模块的最新信息
   */
  function getLatestInfos() {
    return ModelRemoteService.queryLatestInfos().then(function(data) {
      return data;
    });
  };

  function alreadyStorage() {
    return !!modelStorage.getModels();
  }

  /**
   * 获取首页模块信息
   */
  function getModel() {
    var models = modelStorage.getModels();
    if(!_.isEmpty(models)) {
      return $q.when(models);
    } else {
      return this.refreshStorageModel();
    }
  };

  function getSelectedModels() {
    return $q.when(modelStorage.getSelectedModels());
  }

  function refreshStorageModel() {
    ModelLocalStorageService.refreshSelectedModels();
    var md5 = modelStorage.getModelsMd5() || '-1';
    return ModelRemoteService.queryModel(md5).then(function(data) {
      if(!_.isNull (data.md5)) { // 有更新
        modelStorage.addOrUpdateModels(data.models);
        modelStorage.addOrUpdateModelsMd5(data.md5);
        modelStorage.addOrUpdateModelsChange(true);
      } else {
        modelStorage.addOrUpdateModelsChange(false);
      }
    }).then(function() {
      ModelLocalStorageService.refreshSelectedModels();
      return modelStorage.getModels();
    });
  };

  function getImgPath(id) {
    return ModelRemoteService.queryImgPath(id).then(function(data) {
      var url = data.url,
        _url = modelStorage.getImgPath(id);
      if (url != _url) {//不相同则更新
        modelStorage.addOrUpdateImgPath(id, url);
      }
      return data;
    });
  };

  /**
   * 获取模块数据是否有更新
   */
  function getModelsChange() {
    return modelStorage.getModelsChange();
  };

  /**
   * 将标签放入缓存中
   */
  function refreshShowCard(showCard) {
    modelStorage.addOrUpdateShowCard(showCard);
  };

  /**
   * 获取缓存中的标签
   */
  function getShowCard() {
    return modelStorage.getShowCard();
  };

  function getModelOfId(id) {
    return this.getModel().then(function(models) {
      var result = null;
      _.forEach(models, function(model) {
        if(model.id == id) {
          result = model;
        }
      });
      return result;
    });
  }

  function getNotSelectedModel() {
    return $q.when(modelStorage.getNotSelectedModels());
  }

  function setStorageNotSelectedModel(models) {
    return modelStorage.setNotSelectedModels(models);
  }

  function setStorageSelectedModel(models) {
    return modelStorage.setSelectedModels(models);
  }

  function addOrUpdateModelsChange(modelChage) {
    modelStorage.addOrUpdateModelsChange(modelChage);
  }
});
