import moment from 'moment';
import commonModule from '../..';

describe('wsDatePickerService', () => {
  let wsDatePickerService;
  let baseTime;

  beforeEach(() => {
    angular.mock.module(commonModule);
    angular.mock.inject(['wsDatePickerService', (_service_) => {
      wsDatePickerService = _service_;
      baseTime = new Date(2013, 9, 23);
      jasmine.clock().mockDate(baseTime);
    }]);
  });

  afterEach(() => {
    jasmine.clock().uninstall();
  });

  describe('getYesterdayDateAsString', () => {
    it('returns the current date formatted for the specific date picker widget', () => {
      expect(wsDatePickerService.getYesterdayDateAsString().indexOf('Tue Oct 22 2013') >= 0).toBe(true);
    });
  });

  describe('getCurrentDateAsString', () => {
    it('returns the current date formatted for the specific date picker widget', () => {
      expect(wsDatePickerService.getCurrentDateAsString().indexOf('Wed Oct 23 2013') >= 0).toBe(true);
    });
  });

  describe('getNextYearDateAsString', () => {
    it('returns the current + 1 year date formatted for the specific date picker widget', () => {
      expect(wsDatePickerService.getNextYearDateAsString().indexOf('Thu Oct 23 2014') >= 0).toBe(true);
    });
  });

  describe('getCalendarEndDateAsString', () => {
    it('returns Dec 31st for the current year', () => {
      expect(moment(wsDatePickerService.getCalendarEndDateAsString()).date()).toBe(31);
      expect(moment(wsDatePickerService.getCalendarEndDateAsString()).month()).toBe(11);
      expect(moment(wsDatePickerService.getCalendarEndDateAsString())
        .year()).toBe(moment().year());
    });
  });

  describe('getFutureDateAsString', () => {
    it('returns the current date + the provided number of days', () => {
      expect(moment(wsDatePickerService.getFutureDateAsString(6)).isSame(moment().add(6, 'day'), 'day')).toBe(true);
    });
  });

  describe('getCurrentDateAsServerString', () => {
    beforeEach(() => {
      const today = moment('2015-01-19').toDate();
      jasmine.clock().mockDate(today);
    });

    afterEach(() => {
      jasmine.clock().uninstall();
    });

    it('returns the current date formatted for server', () => {
      expect(wsDatePickerService.getCurrentDateAsServerString()).toEqual('2015-01-19');
    });
  });

  describe('convertDisplayInputDateToServerFormat', () => {
    it('should convert display input date to server format', () => {
      expect(wsDatePickerService.convertDisplayInputDateToServerFormat('Jun 9, 2016')).toBe('2016-06-09');
      expect(wsDatePickerService.convertDisplayInputDateToServerFormat('June 9, 2016')).toBe('2016-06-09');
    });

    it('should fail if the date format is invalid', () => {
      expect(wsDatePickerService.convertDisplayInputDateToServerFormat('Ju 9, 2016')).toBe('Invalid date');
    });
  });

  describe('getDatesFromNowToN', () => {
    it('returns an array of dates from today\'s date until a specified number of days from now', () => {
      const dates = wsDatePickerService.getDatesFromNowToN(5);
      expect(dates.length).toBe(6); // Because it includes today's date
    });

    it('can optionally take a boolean to include only business days in that list', () => {
      const dates = wsDatePickerService.getDatesFromNowToN(5, true);
      expect(dates.length).toBe(8);
    });

    it('returns dates in the format YYYY/MM/DD', () => {
      const dates = wsDatePickerService.getDatesFromNowToN(5);
      expect(dates[0]).toBe(moment(baseTime).format('YYYY/MM/DD'));
    });
  });

  describe('getWeekendDates', () => {
    it('gets weekends between a start and end date', () => {
      const start = new Date(2013, 9, 23);
      const end = new Date(2013, 9, 30);
      const dates = wsDatePickerService.getWeekendDates(start, end);
      expect(dates.length).toBe(2); // In any 7 consecutive days there are 2 weekend days
    });
  });

  describe('getNextAvailableDate', () => {
    it('gets today\'s date if today hasn\'t been disabled', () => {
      const disabledDates = ['2013/10/26'];
      const nextAvailableDate = wsDatePickerService.getNextAvailableDate(disabledDates);

      expect((new Date(nextAvailableDate)).getTime()).toBe(baseTime.getTime());
    });

    it('gets the next date after our disabled dates', () => {
      const disabledDates = ['2013/10/23'];
      const nextAvailableDate = wsDatePickerService.getNextAvailableDate(disabledDates);
      expect((new Date(nextAvailableDate)).getTime()).toBe((new Date(2013, 9, 24)).getTime());
    });

    it('doesn\'t mess with our preferred date, if we specified one', () => {
      const disabledDates = ['2013/10/26'];
      const previouslySelectedDate = new Date(2013, 9, 30);
      const nextAvailableDate
        = wsDatePickerService.getNextAvailableDate(disabledDates,
          moment(previouslySelectedDate));
      expect((new Date(nextAvailableDate)).getTime()).toBe(previouslySelectedDate.getTime());
    });
  });
});
