# encoding: UTF-8
namespace :calendars do
  desc 'Load calendars table'
  task :load => [:environment] do
    # TODO callback for new Institution
    InstitutionCalendar.where(institution_id: nil).first_or_create(name: "Calendario #{Date.current.year}")
    Institution.all.each do |institution|
      calendar = institution.institution_calendars.where(year: Date.current.year).first_or_create(name: "Calendario del #{institution.acronym} #{Date.current.year}")
    end
  end
  desc 'Load default calendar dates'
  task :default => [:environment] do
    InstitutionCalendar.where('institution_calendars.institution_id IS NOT NULL').each do |calendar|
      # semana santa
      calendar.calendar_events.where(name: 'Semana santa', start_at: Time.zone.parse('2015-04-14')).first_or_create
      calendar.calendar_events.where(name: 'Semana santa', start_at: Time.zone.parse('2015-04-15')).first_or_create
      calendar.calendar_events.where(name: 'Semana santa', start_at: Time.zone.parse('2015-04-16')).first_or_create
      calendar.calendar_events.where(name: 'Semana santa', start_at: Time.zone.parse('2015-04-17')).first_or_create
      calendar.calendar_events.where(name: 'Semana santa', start_at: Time.zone.parse('2015-04-18')).first_or_create
      calendar.calendar_events.where(name: 'Semana santa', start_at: Time.zone.parse('2015-04-21')).first_or_create
      # día del trabajo
      calendar.calendar_events.where(name: 'Día del trabajo', start_at: Time.zone.parse('2015-05-01')).first_or_create
      # día de la madre
      calendar.calendar_events.where(name: 'Día de la madre', start_at: Time.zone.parse('2015-05-10')).first_or_create
      # día del padre
      calendar.calendar_events.where(name: 'Día del padre', start_at: Time.zone.parse('2015-06-17')).first_or_create
      # fiestas patronales
      calendar.calendar_events.where(name: 'Fiestas patronales', start_at: Time.zone.parse('2015-08-01')).first_or_create
      calendar.calendar_events.where(name: 'Fiestas patronales', start_at: Time.zone.parse('2015-08-04')).first_or_create
      calendar.calendar_events.where(name: 'Fiestas patronales', start_at: Time.zone.parse('2015-08-05')).first_or_create
      calendar.calendar_events.where(name: 'Fiestas patronales', start_at: Time.zone.parse('2015-08-06')).first_or_create
      # día de la independencia
      calendar.calendar_events.where(name: 'Día de la independencia', start_at: Time.zone.parse('2015-09-15')).first_or_create
      # día de los difuntos
      calendar.calendar_events.where(name: 'Día de los difuntos', start_at: Time.zone.parse('2015-11-02')).first_or_create
      # vacaciones navideñas
      calendar.calendar_events.where(name: 'Vacaciones navideñas', start_at: Time.zone.parse('2015-12-24')).first_or_create
      calendar.calendar_events.where(name: 'Vacaciones navideñas', start_at: Time.zone.parse('2015-12-25')).first_or_create
      calendar.calendar_events.where(name: 'Vacaciones navideñas', start_at: Time.zone.parse('2015-12-26')).first_or_create
      calendar.calendar_events.where(name: 'Vacaciones navideñas', start_at: Time.zone.parse('2015-12-29')).first_or_create
      calendar.calendar_events.where(name: 'Vacaciones navideñas', start_at: Time.zone.parse('2015-12-30')).first_or_create
      calendar.calendar_events.where(name: 'Vacaciones navideñas', start_at: Time.zone.parse('2015-12-31')).first_or_create
      calendar.calendar_events.where(name: 'Vacaciones navideñas', start_at: Time.zone.parse('2016-01-01')).first_or_create
      calendar.calendar_events.where(name: 'Vacaciones navideñas', start_at: Time.zone.parse('2016-01-02')).first_or_create
    end
  end
end
