app.factory('$date', function(moment) {
  var factory = {};

  factory.collisionDetection = function(start, end, events) {
    start = factory.extract(start);
    end = end ? factory.extract(end) : start;

    if (end < start) {
      return null;
    }

    for (var i = 0; i < events.length; i++) {
      /*
      if (events[i].approval) {
        if (events[i].approval.cancelled) {
          continue;
        }

        if (events[i].approval.level < events[i].approval.maxLevel) {
          continue;
        }
      }
      */

      var eventStart = factory.extract(events[i].dateStart);
      var eventEnd = eventStart;
      if (events[i].dateEnd) {
        eventEnd = factory.extract(events[i].dateEnd);
      }

      // $    |__$__|
      if (eventEnd > start && eventEnd <= end) {
        return true;
      }
      // |__$__|    $
      if (eventStart >= start && eventStart < end) {
        return true;
      }
      // $   |____|    $
      if (eventStart <= start && eventEnd >= end) {
        return true;
      }
      // |___$___$____|
      if (eventStart >= start && eventEnd <= end) {
        return true;
      }
    }

    return false;
  };

  factory.extract = function(datetime) {
    try {
      return moment(datetime).format('YYYY-MM-DD');
    }
    catch (err) {
      return factory.extract(new Date(datetime));
    }
  };

  factory.extractMinutes = function(time) {
    try {
      return factory.pad(time.getHours()) + ':' + factory.pad(time.getMinutes());
    }
    catch (err) {
      time = new Date(time);
      return factory.pad(time.getHours()) + ':' + factory.pad(time.getMinutes());
    }
  };

  factory.formatDatetime = function(datetime) {
    if (!datetime) {
      return '-';
    }
    var d = new Date(datetime);

    return d.getFullYear() + '-' + factory.pad(d.getMonth() + 1) + '-' + factory.pad(d.getDate()) + ' às ' +
      factory.pad(d.getHours()) + ':' + factory.pad(d.getMinutes()) + ':' + factory.pad(d.getSeconds());
  };

  factory.calculateTimespan = function(startTimestamp, endTimestamp) {
    var end;
    if (endTimestamp) {
      end = moment(endTimestamp);
    }
    else {
      end = moment();
    }

    var start = new Date(startTimestamp);

    var duration = moment.duration(end.diff(start));
    var mins = duration.asMinutes().toFixed(2);
    return mins;
  };

  factory.totalMinutes = function(timeString) {
    var HourMinutes = timeString.split(':');
    if (HourMinutes.length == 1) {
      HourMinutes.push(0);
    }

    var hours = parseInt(HourMinutes[0]);
    var minutes = parseInt(HourMinutes[1]);

    return hours * 60 + minutes;
  };

  factory.pad = function(number, count) {
    var res = number + "";
    count = count || 2;
    while (res.length < count) {
      res = "0" + res;
    }
    return res;
  };

  factory.getTimestamp = function() {
    var d = new Date();
    var timestamp = factory.pad(d.getHours()) + ":" + factory.pad(d.getMinutes());
    return timestamp;
  };

  factory.getWeekDay = function(weekday) {
    weekday -= 1;
    switch (weekday) {
      case 0:
        return 'Domingo';
      case 1:
        return 'Segunda-Feira';
      case 2:
        return 'Terça-Feira';
      case 3:
        return 'Quarta-Feira';
      case 4:
        return 'Quinta-Feira';
      case 5:
        return 'Sexta-Feira';
      case 6:
        return 'Sabado';
      default:
        return '?';
    }
  };

  factory.getTotalMinutes = function(time) {
    var HourMinutes = time.split(':');
    if (HourMinutes.length === 0 || HourMinutes.length > 2) {
      return null;
    }
    if (HourMinutes.length == 1) {
      HourMinutes.push(0);
    }

    var hours = parseInt(HourMinutes[0]);
    if (hours < 0 || hours > 23) {
      return null;
    }
    var minutes = parseInt(HourMinutes[1]);
    if (minutes < 0 || minutes > 59) {
      return null;
    }

    return hours * 60 + minutes;
  };

  factory.formatDate = function(date) {
    var d = new Date(date);
    var month = '' + (d.getMonth() + 1);
    var day = '' + d.getDate();
    var year = d.getFullYear();

    if (month.length < 2) {
      month = '0' + month;
    }

    if (day.length < 2) {
      day = '0' + day;
    }

    return [year, month, day].join('-');
  };

  return factory;
});
