import store from '../store';
import Immutable from 'immutable';
import { isPopout } from 'utils/popouts';

export const ADD_POPOUT_CALENDAR = 'ADD_POPOUT_CALENDAR';
export function addPopoutCalendar(calendar = null) {
  return {
    type: ADD_POPOUT_CALENDAR,
    calendar
  };
}

export const ADD_POPOUT_NEWSFEED = 'ADD_POPOUT_NEWSFEED';
export function addPopoutNewsfeed(filters = null, themes = null, headerSpacing = null, textSize = null) {
  return {
    type: ADD_POPOUT_NEWSFEED,
    filters,
    themes,
    headerSpacing,
    textSize
  };
}

export const ADD_POPOUT_SECURITY = 'ADD_POPOUT_SECURITY';
export function addPopoutSecurity(symbol = null) {
  return {
    type: ADD_POPOUT_SECURITY,
    symbol
  };
}

export const ADD_POPOUT_WATCHLIST = 'ADD_POPOUT_WATCHLIST';
export function addPopoutWatchlist(id) {
  return {
    type: ADD_POPOUT_WATCHLIST,
    id
  };
}

export const REMOVE_POPOUT = 'REMOVE_POPOUT';
export function removePopout(id) {
  return {
    type: REMOVE_POPOUT,
    id
  };
}

export function addPopout(type, data = null) {
  if (!isPopout()) {
    bzTrack.track(type + ': Popped Out', {
      data: data.toJS()
    });
  }
  switch (type) {
  case 'calendar':
    // Clear the list of events to simplify the query-string parameter
    const calendar = data.remove('events');
    return addPopoutCalendar(calendar);
  case 'newsfeed':
    const filters = data.get('filters');
    const themes = data.get('themes');
    const headerSpacing = data.get('headerSpacing');
    const textSize = data.get('textSize');
    return addPopoutNewsfeed(filters, themes, headerSpacing, textSize);
  case 'security':
    const symbol = data.get('symbol');
    return addPopoutSecurity(symbol);
  case 'watchlist':
    const watchlistId = data.get('watchlistId');
    return addPopoutWatchlist(watchlistId);
  default:
    return null;
  }
}
