'use strict';

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

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

  module.filter('datefilter', function() {
    var MINUTE_TIME = 60 * 1000, // 1分钟 = 60 000毫秒
      HOUR_TIME   = 60 * 60 * 1000,
      DAY_TIME    = 24 * 60 * 60 * 1000,
      MONTH_TIME  = 30 * 24 * 60 * 60 * 1000,
      YEAR_TIME   = 365 * 24 * 60 * 60 * 1000;

    return function(text) {
      if (!text) { return; }

      try {
        var mydate = jsUtil.parseDate(text, 'Y-m-d H:i:s'),
          date = mydate.getTime();

        var time = Date.now() - date,
          minutes =  Math.floor(time / MINUTE_TIME),
          hours = Math.floor(time / HOUR_TIME),
          days = Math.floor(time / DAY_TIME),
          months = Math.floor(time / MONTH_TIME),
          years = Math.floor(time / YEAR_TIME);

        if (minutes < 30 && minutes > 0) {
          return minutes + '分前';
        } else if (minutes >= 30 && minutes < 60) {
          return '半小时前';
        } else if (minutes >= 60 && hours < 24) {
          return hours + '小时前';
        } else if (hours >= 24 && days < 30) {
          return days + '天前';
        } else if (days >= 30 && months < 12) {
          return months + '月前';
        } else if(years >= 1) {
          return years + '年前';
        } else if(minutes === 0) {
          return '刚刚';
        }
      } catch (e) {
        return text;
      }
    };
  });
