import {
  LOADING_CALENDAR,
  CALENDAR_CHANGE_PARAMETERS,
  CALENDAR_CHANGE_DISPLAY_OPTIONS,
} from 'actions/calendars';
import {
  ACTION_SUCCEEDED,
  ACTION_FAILED,
  ACTION_STARTED
} from 'utils/actions';
import Immutable from 'immutable';
import moment from 'moment';
import {
  calendarDefaultDisplaySettings,
  calendarDefaultSortSettings,
  calendarDefaultFrozenSettings,
  calendarEmptySettings,
} from 'utils/calendar';

const initialCalendarType = 'earnings';

// TODO: whoops! we need to access colSortDirs etc with a key
// since shared!

export const CalendarParameters = new Immutable.Record({
  calendarType: initialCalendarType,
  dateStart: moment().format('YYYY-MM-DD'),
  dateEnd: moment().format('YYYY-MM-DD'),
  symbol: null,
  page: 0,
});

export const CalendarDisplayOptions = new Immutable.Record({
  colSortDirs: calendarDefaultSortSettings(),
  colHidden: calendarDefaultDisplaySettings(),
  colFrozen: calendarDefaultFrozenSettings(),
  colWidths: calendarEmptySettings(),
});

export const Calendar = new Immutable.Record({
  isLoading: false,
  canLoadMore: false,
  parameters: new CalendarParameters(),
  displayOptions: new CalendarDisplayOptions(),
  events: new Immutable.List(),
});

export function newCalendar(parameters) {
  let newParams = new CalendarParameters(parameters);

  // Make sure that if the user has the page open > 1 day new calendars open to
  // the current day
  if (!('dateStart' in parameters) && !('dateEnd' in parameters)) {
    const currentDate = moment().format('YYYY-MM-DD');
    newParams = newParams
      .set('dateStart', currentDate)
      .set('dateEnd', currentDate);
  }

  return new Calendar({
    parameters: newParams,
  });
}

export function calendarsReducer(oldCalendar = newCalendar(), action) {
  let calendar = oldCalendar;
  switch (action.type) {
  case LOADING_CALENDAR:
    if (action.status === ACTION_STARTED) {
      calendar = calendar
        .set('isLoading', true)
        .set('canLoadMore', false);
    } else {
      calendar = calendar
        .set('isLoading', false);
      if (action.status === ACTION_SUCCEEDED) {
        const events = calendar.get('events').concat(action.payload.events);
        calendar = calendar
          .set('canLoadMore', action.payload.events.length === 50)
          .set('events', events);
        // Update the page (of search results) we are on.
        calendar = calendar.setIn(['parameters', 'page'], action.payload.page);
      }
    }
    break;
  case CALENDAR_CHANGE_PARAMETERS:
    if (action.status === ACTION_SUCCEEDED) {
      calendar = calendar
        .set('parameters', new CalendarParameters(action.payload.parameters))
        .set('events', calendar.get('events').clear());
    }
    break;
  case CALENDAR_CHANGE_DISPLAY_OPTIONS:
    if (action.status === ACTION_SUCCEEDED) {
      calendar = calendar.set('displayOptions', new CalendarDisplayOptions(action.payload.displayOptions));
    }
    break;
  default:
    break;
  }
  // we have to do this since not every user will have these fields in their saved
  // state and will break for them --
  return calendar;
}
