var dateUtils = require('./date-utils');

/**
 * Year constructor.
 * @param {number} year
 */
function Year(year) {
  if (year > 0) {
    this.year = year;
    this.holidays = {};
    this.loadHolidays();
  } else {
    throw Error('Invalid year: ' + year);
  }
}

/**
 * Loads holidays.
 */
Year.prototype.loadHolidays = function() {
  this.addHoliday(dateUtils.createDate(this.year, 1, 1), "New Year's Day");
  this.addHoliday(dateUtils.createDate(this.year, 1, 6), 'Epiphany');
  this.addHoliday(dateUtils.goodFriday(this.year), 'Good Friday');
  this.addHoliday(dateUtils.easterSunday(this.year), 'Easter Sunday');
  this.addHoliday(dateUtils.easterMonday(this.year), 'Easter Monday');
  // TODO: May Day
  this.addHoliday(dateUtils.ascensionDay(this.year), 'Ascension Day');
  this.addHoliday(dateUtils.pentecost(this.year), 'Pentecost');
  // TODO: Midsummer Eve
  // TODO: Midsummer Day
  // TODO: All Saints' Day
  // TODO: Independence Day
  // TODO: Christmas Eve (unofficial)
  // TODO: Christmas Day
  // TODO: St. Stephen's Day
};

/**
 * Discards holidays falling on a weekend.
 */
Year.prototype.discardWeekends = function() {
  var self = this;
  var holidays = {};

  Object.keys(self.holidays).forEach(function(month) {
    if (self.holidays[month].length > 0) {
      Object.keys(self.holidays[month]).forEach(function(i) {
        var holiday = self.holidays[month][i];

        if (!dateUtils.isWeekend(holiday.year, holiday.month, holiday.day)) {
          if (typeof holidays[month] === 'undefined') {
            holidays[month] = [];
          }

          holidays[month].push(holiday);
        }
      });
    }
  });

  this.holidays = holidays;
};

/**
 * Adds a holiday.
 * @param {Date} date
 * @param {string} description
 */
Year.prototype.addHoliday = function(date, description) {
  var month = dateUtils.getMonth(date);
  var year = dateUtils.getYear(date);
  var day = dateUtils.getDay(date);
  var holiday = {
    year: year,
    month: month,
    day: day,
    description: description
  };

  if (typeof this.holidays[month] === 'undefined') {
    this.holidays[month] = [];
  }

  this.holidays[month].push(holiday);
};

module.exports = {
  /**
   * Creates a new year object.
   * @param {number} year
   * @return {Year}
   */
  get: function(year) {
    return new Year(year);
  }
};

@@ -690,24 +690,74 @@
 ;%0A  
-// TODO: 
+this.addHoliday(dateUtils.createDate(this.year, 5, 1), '
 May Day
+');
 %0A  t
@@ -971,108 +971,312 @@
 y%0A  
-// TODO: Independence Day%0A  // TODO: Christmas Eve (unofficial)%0A  // TODO: Christmas Day%0A  // TODO: 
+this.addHoliday(dateUtils.createDate(this.year, 12, 6), 'Independence Day');%0A  this.addHoliday(dateUtils.createDate(this.year, 12, 24), 'Christmas Eve (unofficial)');%0A  this.addHoliday(dateUtils.createDate(this.year, 12, 25), 'Christmas Day');%0A  this.addHoliday(dateUtils.createDate(this.year, 12, 26), %22
 St. 
@@ -1288,16 +1288,19 @@
 en's Day
+%22);
 %0A%7D;%0A%0A/**

Include May Day, Independence Day, Christmas Eve, Christmas Day, and St. Stephen's Day