import { fromJS } from 'immutable';

import defaultSetting from '../../../../config/defaultSetting';
import generRoutes, { generFlatRoutes } from '../../../utils/routes';
import Storage from '../../../utils/storage';
import {
  CLEAR_RCENT_KEYWORD,
  CREATE_CONEXT,
  GENERATE_MENU,
  GET_CONFIG,
  GET_CURRENT_INDEX,
  GET_CURRENT_ROUTES,
  GET_ERROR_CONFIG,
  GET_RCENT_KEYWORD,
  GET_RECENT_PRODUCT,
  GET_RECENT_VISITED,
  GET_THEME_CONFIG,
  SLIDER_MENU_MODE,
} from './constants';

const keywordStorage = new Storage("__global_search_keywords__");
const recentVisitedStorage = new Storage("__global_recent_visited__");
const recentProductStorage = new Storage("__global__recent_product__")
export const initialState = fromJS({
  globalConfig: {},
  globalConfigError: '',
  currentRoutes: {},
  instance: null,
  menu: [],
  flatMenu: [],
  defaultSetting: defaultSetting,
  recentKeywords: keywordStorage.list || [],
  recentVisited:  recentVisitedStorage.list || [],
  recentProducts: recentProductStorage.list || [],
  currentMenuIndex: 0,
  collapsed: true
});

/* eslint-disable default-case, no-param-reassign */
const appReducer = (state = initialState, action) => {
  switch (action.type) {
    case GET_CONFIG:
      window.__INITIAL_STATE__ = Object.assign({}, action.data, {
        allWidgets: action.data.widgets || [],
      });
      window.globalConfig = require('kit_global_config').default;
      createStoreage.set('globalConfig', window.globalConfig);
      const config = createStoreage.get('globalConfig');
      const generMenu = generRoutes(config.widgets || []);
      const flatMenu = generFlatRoutes(generMenu || []);
      return state.merge({
        globalConfig: config,
        menu: generMenu,
        flatMenu: flatMenu
      });
    case GET_ERROR_CONFIG:
      return state.merge({
        globalConfigError: action.data,
      });
    case GET_CURRENT_ROUTES:
      return state.merge({
        currentRoutes: action.data,
      });
    case CREATE_CONEXT:
      return state.merge({
        instance: action.data,
      });
    case GENERATE_MENU:
      return state.merge({
        menu: action.data,
      });
    case GET_THEME_CONFIG:
      return state.merge({
        defaultSetting: action.data,
      });
    case GET_RCENT_KEYWORD:
      keywordStorage.set('keywords', action.data);
      return state.merge({
        recentKeywords: keywordStorage.list
      })
    case GET_RECENT_VISITED:
      recentVisitedStorage.set('history', action.data);
      return state.merge({
        recentVisited:  recentVisitedStorage.list
      })
    case GET_RECENT_PRODUCT:
      recentProductStorage.set('history', action.data);
      return state.merge({
        recentProducts: recentProductStorage.list
      })
    case CLEAR_RCENT_KEYWORD:
      keywordStorage.clear();
      return state.merge({
        recentKeywords: []
      })    
    case GET_CURRENT_INDEX:
      return state.merge({
        currentMenuIndex: action.data
      }) 
    case SLIDER_MENU_MODE:
      return state.merge({
        collapsed: action.data
      })  
    default:
      return state;
  }
};
export default appReducer;
