import Immutable from 'immutable';
import moment from 'moment';
import { newSecurity } from './securities';
import { Calendar } from '../reducers/calendars';
import { FeedFilter } from '../reducers/filters';

import {
  ADD_WIDGET_WATCHLIST,
  ADD_WIDGET_CALENDAR,
  ADD_WIDGET_SECURITY,
  ADD_WIDGET_NEWSFEED
} from 'actions/widgets';
import {
  ADD_POPOUT_WATCHLIST,
  ADD_POPOUT_CALENDAR,
  ADD_POPOUT_SECURITY,
  ADD_POPOUT_NEWSFEED
} from 'actions/popouts';

export const Widget = new Immutable.Record({
  id: null,
  type: null,
  data: new Immutable.Record({})
});

export const FeedTheme = new Immutable.Record({
  hue: 180,
  category: null,
  target: null
});

export function widgetAddReducer(nextId, action) {
  switch (action.type) {
  case ADD_WIDGET_WATCHLIST:
  case ADD_POPOUT_WATCHLIST:
    return new Widget({
      id: nextId,
      type: 'watchlist',
      data: Immutable.fromJS({
        // Watchlist props
        watchlistId: action.id || null,
        display: {
          symbol: {visible: true, name: 'Symbol'},
          company: {visible: false, name: 'Security'},
          price: {visible: true, name: 'Price'},
          bidPrice: {visible: false, name: 'Bid'},
          askPrice: {visible: false, name: 'Ask'},
          dayHigh: {visible: true, name: 'Day Hi'},
          dayLow: {visible: true, name: 'Day Lo'},
          yearHigh: {visible: false, name: '52wk Hi'},
          yearLow: {visible: false, name: '52wk Lo'},
          nominalChange: {visible: true, name: 'Change ($)'},
          percentChange: {visible: true, name: 'Change (%)'},
          volume: {visible: true, name: 'Volume'},
        }
      })
    });
  case ADD_WIDGET_CALENDAR:
  case ADD_POPOUT_CALENDAR:
    let newWidget = new Widget({
      id: nextId,
      type: 'calendar',
      data: new Calendar(action.calendar),
    });
    if (action.calendar === null) {
      const currentDate = moment().format('YYYY-MM-DD');
      // Make sure that if the user has the page open > 1 day new calendars open to
      // the current day
      newWidget = newWidget
        .setIn(['data', 'parameters', 'dateStart'], currentDate)
        .setIn(['data', 'parameters', 'dateEnd'], currentDate);
    }
    return newWidget;
  case ADD_WIDGET_SECURITY:
  case ADD_POPOUT_SECURITY:
    return new Widget({
      id: nextId,
      type: 'security',
      data: newSecurity(action.symbol),
    });
  case ADD_WIDGET_NEWSFEED:
  case ADD_POPOUT_NEWSFEED:
  default:
    let headerSpacing = 1.7;
    if ('headerSpacing' in action) {
      if (!isNaN(parseFloat(headerSpacing)) && isFinite(headerSpacing)) {
        headerSpacing = action.headerSpacing;
      }
    }
    return new Widget({
      id: nextId,
      type: 'newsfeed',
      data: Immutable.fromJS({
        // Newsfeed props
        filters: action.filters || new FeedFilter(),
        themes: action.themes || new Immutable.List([
          new FeedTheme({
            hue: 360,
            category: 'channel',
            target: {
              id: 'exclusives-premiere',
              channel: 'Market-Moving Exclusives',
              tids: ['165347']
            }
          })
        ]),
        headerSpacing: headerSpacing,
        textSize: action.textSize || 12
      })
    });
  }
}
