#= require angular/payrollhero_api/time_helper

describe 'angular:payrollhero.api.factory(TimeHelper)', ->
  subject = undefined

  beforeEach ->
    module 'payrollhero.api'

  mtz = (time) ->
    moment.tz(time, 'America/Vancouver')

  beforeEach inject (TimeHelper) ->
    subject = TimeHelper
    return

  toDateString = (dateMoment) ->
    dateMoment.format('YYYY-MM-DD')

  describe "#daysOfTheWeek", ->
    it "generates the days of the week", ->
      day = moment('2014-01-01')
      expect(_(subject.daysOfTheWeek(day).map(toDateString)).value()).toEqual(
        ['2013-12-29', '2013-12-30', '2013-12-31', '2014-01-01', '2014-01-02',
         '2014-01-03', '2014-01-04'])

  describe "#isToday", ->
    it "answers correctly", ->
      expect(subject.isToday(moment())).toBeTruthy()
      expect(subject.isToday(moment().add(days: 1))).toBeFalsy()

  describe '#adjustToDay', ->
    it "puts the time of moments on the day", ->
      ex1 = subject.adjustToDay(mtz('2015-02-04'), mtz('14:00'), 'America/Vancouver')
      ex2 = subject.adjustToDay(mtz('2015-02-04'), mtz('2014-09-21 14:00'), 'America/Vancouver')
      ex3 = subject.adjustToDay(mtz('2015-02-04'), mtz('14:00'), 'Asia/Hong_Kong')
      ex4 = subject.adjustToDay(mtz('2015-02-04'), mtz('2014-09-21 14:00'), 'Asia/Hong_Kong')
      expect(ex1.toISOString()).toEqual("2015-02-04T08:00:00.000Z")
      expect(ex2.toISOString()).toEqual("2015-02-04T22:00:00.000Z")
      expect(ex3.toISOString()).toEqual("2015-02-03T16:00:00.000Z")
      expect(ex4.toISOString()).toEqual("2015-02-04T06:00:00.000Z")

  describe '#differenceInWholeDays', ->
    it 'gives the difference in whole days between randomtimes', ->
      ex1 = subject.differenceInWholeDays(mtz('2015-02-04 09:00'), mtz('2015-02-03 21:00'))
      ex2 = subject.differenceInWholeDays(mtz('2015-02-04 09:00'), mtz('2015-02-04 21:00'))
      ex3 = subject.differenceInWholeDays(mtz('2015-02-03 09:00'), mtz('2015-02-04 21:00'))

      expect(ex1).toEqual(1)
      expect(ex2).toEqual(0)
      expect(ex3).toEqual(-1)

  describe '#moveAndPutInTz', ->
    it 'moves times by a number of days and puts them in the appropriate tz', ->
      ex1 = subject.moveAndPutInTz(1, mtz('2014-09-21 14:00'), 'America/Vancouver')
      ex2 = subject.moveAndPutInTz(0, mtz('2014-09-21 14:00'), 'America/Vancouver')
      ex3 = subject.moveAndPutInTz(1, mtz('2014-09-21 14:00'), 'Asia/Hong_Kong')
      ex4 = subject.moveAndPutInTz(-1, mtz('2014-09-21 14:00'), 'Asia/Hong_Kong')

      console.log(ex1.toISOString())
      console.log(ex2.toISOString())
      console.log(ex3.toISOString())
      console.log(ex4.toISOString())

      expect(ex1.toISOString()).toEqual("2014-09-22T21:00:00.000Z")
      expect(ex2.toISOString()).toEqual("2014-09-21T21:00:00.000Z")
      expect(ex3.toISOString()).toEqual("2014-09-22T06:00:00.000Z")
      expect(ex4.toISOString()).toEqual("2014-09-20T06:00:00.000Z")

  describe '#now', ->
    it 'is now?', ->
      expect(subject.now().isSame(moment(), 'second')).toBeTruthy()
