#= require angular/payrollhero_api/ph_restangular.coffee

describe 'angular:payrollhero.api.factory(EmployeeScheduleDayList)', ->
  subject = undefined
  httpBackend = undefined
  employeesService = undefined
  scheduleEventsService = undefined

  setupBasicEmployeeResponse = ->
    httpBackend.expectGET('http://api.payrollhero.dev/api/v3/employees?page=1&token=t0ken')
    .respond(getJSONFixture('api/v3/employees_response.json'))

  setupBasicScheduleEventsResponse = ->
    httpBackend.expectGET('http://api.payrollhero.dev/api/v4/schedule_events?page=1&token=t0ken')
    .respond(getJSONFixture('api/v4/schedules_response.json'))

  setupWorkThisHolidayResponse = ->
    httpBackend.expectGET('http://api.payrollhero.dev/api/v4/schedule_events?page=1&token=t0ken')
      .respond(getJSONFixture('api/v4/employee_46_work_this_holiday_schedule.json'))

  setupSharedEventsResponse = ->
    httpBackend.expectGET('http://api.payrollhero.dev/api/v4/schedule_events?shared_events=true&token=t0ken')
    .respond(getJSONFixture('api/v4/shared_events_response.json'))

  beforeEach ->
    module 'payrollhero.api'
    angular.module('payrollhero.api').constant('PhToken', {token: 't0ken'})

  beforeEach inject (EmployeesService, ScheduleEventsService, EmployeeScheduleDayList, $httpBackend) ->
    subject = EmployeeScheduleDayList
    employeesService = EmployeesService
    scheduleEventsService = ScheduleEventsService
    httpBackend = $httpBackend
    return

  describe 'methods', ->
    employee = undefined
    employees = []
    schedules = []
    sharedEvents = []
    day = moment('2014-12-22')

    beforeEach ->
      setupBasicEmployeeResponse()
      setupBasicScheduleEventsResponse()
      setupSharedEventsResponse()
      employeesService.getList(page: 1).then (list) ->
        employees = list
      scheduleEventsService.getList(page: 1).then (list) ->
        schedules = list
      scheduleEventsService.getList(shared_events: true).then (list) ->
        sharedEvents = list
      httpBackend.flush()
      employee = _(employees).where(id: 46)[0]

    describe 'the employee works holidays', ->
      beforeEach ->
        employee.works_on_holidays = true

      it 'does work holidays on the date with a holiday', ->
        expect(employee.worksHolidayOn(day)).toBeTruthy()

      it 'doesnt remove the recurring schedule on that day', ->
        info = employee.scheduleInfoOn(sharedEvents, day)
        expect(info.onHoliday).toBeFalsy()

    describe 'the employee doesnt work holidays', ->
      beforeEach ->
        employee.works_on_holidays = false

      it 'doesnt work holidays on the date with a holiday', ->
        expect(employee.worksHolidayOn(day)).toBeFalsy()

      it 'removes the recurring schedule on that day', ->
        info = employee.scheduleInfoOn(sharedEvents, day)
        expect(info.onHoliday).toBeTruthy()

      describe 'there is a works_this_holiday exception', ->
        beforeEach ->
          setupWorkThisHolidayResponse()
          scheduleEventsService.getList(page: 1)
          httpBackend.flush()

        it 'is no longer on holiday', ->
          info = employee.scheduleInfoOn(sharedEvents, day)
          expect(info.onHoliday).toBeFalsy()
