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

angular
  .module('linker.services')
  .constant('FavoriteLinker', FavoriteLinker)
  .constant('User', User);

function FavoriteLinker() {
  this.items = [];
  this.favoriteNum = 0;
}

FavoriteLinker.prototype = {
  /**
   * 插入一个新的常用联系人
   */
  add: function(userData) {
    if(_.isArray(userData)) {
      this._pushArray(userData);
    } else {
      this._pushOne(userData);
    }
    this.calcFavoriteNum();
    this.sort();
  },

  _pushArray: function(userData) {
    var user;
    for(var i = 0, len = userData.length; i < len; i++) {
      user = userData[i];
      this._pushOne(user);
    }
  },

  _pushOne: function(userData) {
    if(!userData || !userData.userId) {
      return;
    }
    var user = this.getByUserId(userData.userId);

    if (user) {
      user.update(userData);
    } else {
      user = new User(userData);
      this.items.push(user);
    }
  },

  /**
   * 判断这个人是否在常用联系人中
   * @return {Boolean}
   */
  isFavorite: function(userId) {
    var user;
    for(var i = 0, len = this.items.length; i < len; i++) {
      user = this.items[i];
      if(user.userId === userId) {
        return true;
      }
    }
    return false;
  },
  /**
   * 从常用联系人中删去该人
   */
  remove: function(userId) {
    var _User = this.getByUserId(userId);
    if(_.isEmpty(_User)) {return;}
    _.remove(this.items, _User);
    this.calcFavoriteNum();
  },
  /**
   * 清空
   */
  clear: function() {
    this.items = [];
    this.calcFavoriteNum();
  },
  /**
   * 按拼音首字母排序
   */
  sort: function() {
    this.count = this.items.length;

    function compareTo(fistletter, secondletter) {
      if(fistletter === secondletter) {
        return 0;
      } else if(fistletter < secondletter) {
        return -1;
      } else if(fistletter > secondletter) {
        return 1;
      }
    }

    this.items = this.items.sort(function(a, b) {
      var letterComp = compareTo(a.userNamePinyin[0], b.userNamePinyin[0]);
      if(letterComp === 0) {
        return a.userName.localeCompare(b.userName);
      }
      return letterComp;
    });

    var letterMap = this.letterMap = {};
    this.items.forEach(function(item, index) {
      var letter = item.userNamePinyin[0].substring(0, 1).toUpperCase();
      if (!letterMap.hasOwnProperty(letter)) {
        letterMap[letter] = index;
      }
    });
  },
  /**
   * 根据userId来查找常用联系人中的user
   * @param  {[String]} userId 用户ID
   * @return {[User]} 查询到的User
   */
  getByUserId: function(userId) {
    var user, result;
    for(var i = 0, len = this.items.length; i < len; i++) {
      user = this.items[i];
      if(user.userId === userId) {
        result = user;
      }
    }
    return result;
  },

  calcFavoriteNum: function() {
    this.favoriteNum = this.items.length;
  },

  /**
   * 获取所有user的id
   * @return {[String]}
   */
  getUserIds: function() {
    var userIds = [], user;
    for(var i = 0, len = this.items.length; i < len; i++) {
      user = this.items[i];
      userIds.push(user.userId);
    }
    return userIds;
  }

};

function User(userData) {
  this.update(userData);
}
User.prototype = {
  update: function(userData) {
    _.apply(this, userData);
  }
};
