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

module.constant('AppNotices', AppNotices)
  .constant('Notice', Notice);


function AppNotices() {
  this.notices = [];
  this.unreadNum = 0;//通知页面消息未读数目默认为0
  this.orderNotices = orderNotices;
  this.order;
  this.unreadNumChangeListeners = {};
}

AppNotices.prototype = {
  push: function(noticeData) {
    var notices = [];
    if (_.isArray(noticeData)) {
      notices = this._pushArray(noticeData);
    } else {
      var notice = this._pushOne(noticeData);
      if (notice) {
        notices = [notice];
      }
    }

    this.fireUnreadNumChange(notices);
    return notices;
  },

  _pushArray: function(noticesData) {
    var notices = [], notice;
    for (var i = 0, len = noticesData.length; i < len; i++) {
      notice = this._pushOne(noticesData[i]);
      if (notice) {
        notices.push(notice);
      }
    }

    return notices;
  },

  _pushOne: function(noticeData) {
    if(!noticeData.moduleName) {
      return;
    }
    var notice = this.get(noticeData.moduleName);

    if (notice) {
      notice.update(noticeData);
    } else {
      notice = new Notice(noticeData);
      this.notices.push(notice);
    }

    this.unreadNum = this.calUnreadNum();
    this.orderNotices(this.order);

    return notice;
  },

  clear: function() {
    var notices = this.notices;
    for (var i = 0, len = notices.length; i < len; i++) {
      notices[i].unreadNum = 0;
    }
    this.fireUnreadNumChange(notices);
    this.notices = [];
    this.unreadNum = 0;
  },

  getMaxVersion: function() {
    var maxVersion = -1, //最大版本号默认为-1
      notice;
    for(var i = 0, len = this.notices.length; i < len; i++) {
      notice = this.notices[i];
      maxVersion = (notice.version > maxVersion) ? notice.version : maxVersion;
    }
    return maxVersion;
  },

  calUnreadNum: function() {
    var unreadNum = 0, notice;
    for (var i = 0, len = this.notices.length; i < len; i++) {
      notice = this.notices[i];
      if(notice.content) {
        unreadNum += notice.unreadNum;
      }
    }
    return unreadNum;
  },

  isMessage: function(notice) {
    return notice.moduleName === 'message';
  },

  sortNoticesByTime: function() {
    if (!this.notices) { return; }
    this.notices.sort(function(a, b) {
      if(a.sendTime < b.sendTime) {
        return 1;
      } else if(a.sendTime == b.sendTime) {
        return 0;
      } else {
        return -1;
      }
    });
  },

  sortNoticesByModule: function(moduleNames) {
    if(!moduleNames) {
      return;
    }
    var _notices = [], noticeList = [], notice;
    _.apply(_notices, this.notices);

    _.each(moduleNames, function(order) {
      for(var i = 0; i < _notices.length; i++) {
        notice = _notices[i];
        if(notice.id && notice.id.indexOf(order) === 0 ) {
          noticeList.push(notice);
          _.remove(_notices, notice);
        }
      }
    });

    if(_notices && _notices.length > 0 ){
      noticeList = noticeList.concat(_notices);
    }

    this.notices = noticeList;
  },

  get: function(moduleName) {
    var notice;
    for (var i = 0, len = this.notices.length; i < len; i++) {
      notice = this.notices[i];
      if (notice.moduleName === moduleName) {
        return notice;
      }
    }
    return null;
  },

  registerUnreadNumberChange: function(moduleName, listener) {
    var listeners = this.unreadNumChangeListeners[moduleName],
      notice = this.get(moduleName);
    if (!listeners) {
      listeners = this.unreadNumChangeListeners[moduleName] = [];
    }
    listeners.push(listener);

    if (notice) {
      listener(notice.unreadNum);
    }
  },

  fireUnreadNumChange: function(notices) {
    var listeners, moduleName;
    for (var i = 0, len = notices.length; i < len; i++) {
      moduleName = notices[i].moduleName;
      listeners = this.unreadNumChangeListeners[moduleName];
      if (listeners) {
        for (var j = 0, listenersLen = listeners.length; j < listenersLen; j++) {
          listeners[j](this.get(moduleName).unreadNum);
        }
      }
    }
  }
};

function orderNotices(order) {
  this.order = order;
  if(!order) {
    this.sortNoticesByTime();
  } else {
    this.sortNoticesByModule(order);
  }
}

/**
 * this:一个Notice
 * @param {[type]} noticeData [description]
 */
function Notice(noticeData) {
  this.unreadNum = 0;
  this.update(noticeData);
  this.id = this.moduleName;
}

Notice.prototype = {
  update: function(noticeData) {
    _.apply(this, noticeData);
    this.id = this.moduleName;
  }
};

