import { fromJS } from 'immutable';
import Cookies from 'js-cookie';
import { Storeage as Store } from 'kit_utils';
import AppConfig from 'kit_global_config';
import defaultSetting from '../../../../config/defaultSetting';
import generRoutes, { generFlatRoutes } from '../../../utils/routes';
import Storage from '../../../utils/storage';
import {
  CLEAR_RCENT_KEYWORD,
  COMPLEX_CONFIG,
  COMPLEX_PATHNAME,
  CREATE_CONEXT,
  EXTEND_OPEN_KEY,
  GENERATE_MENU,
  GET_CONFIG,
  GET_CURRENT_INDEX,
  GET_CURRENT_ROUTES,
  GET_ERROR_CONFIG,
  GET_PATHNAME,
  GET_RCENT_KEYWORD,
  GET_RECENT_PRODUCT,
  GET_RECENT_VISITED,
  GET_THEME_CONFIG,
  LOGIN_MODE,
  SELECT_MENU_KEY,
  SLIDER_MENU_MODE,
  VALIDATE_AUTH,
  GET_MICRO_MOUNTED_STATUS,
} from './constants';
const proxy = require('../../../../config/proxy');

const keywordStorage = new Storage(
  `__global_search_keywords__micro_${window.location.hostname}`,
);
const recentVisitedStorage = new Storage(
  `__global_recent_visited__micro_${window.location.hostname}`,
);
const recentProductStorage = new Storage(
  `__global__recent_product__micro_${window.location.hostname}`,
);
const currentProduct = new Store(
  `__global__recent_productIndex__micro_${window.location.hostname}`,
);
Cookies.set('loginMode', 'pdw');
export const initialState = fromJS({
  globalConfig: {},
  globalConfigError: '',
  currentRoutes: {},
  instance: null,
  menu: [],
  flatMenu: [],
  defaultSetting,
  recentKeywords: keywordStorage.list || [],
  recentVisited: recentVisitedStorage.list || [],
  recentProducts: recentProductStorage.list || [],
  currentMenuIndex: currentProduct.get('currentMenuIndex') || 0,
  currentMenu: null,
  collapsed: true,
  loginMode: Cookies.get('loginMode') || 'pdw',
  authValidate: false,
  pathname: '',
  selectedKeys: null,
  openKeys: [],
  complexConfig: {},
  complexPathName: null,
  microMounted: true,
});

/* eslint-disable default-case, no-param-reassign */
const appReducer = (state = initialState, action) => {
  switch (action.type) {
    case GET_CONFIG:
      // eslint-disable-next-line no-debugger
      // eslint-disable-next-line no-underscore-dangle
      window.__INITIAL_STATE__ = Object.assign(
        {},
        // eslint-disable-next-line no-underscore-dangle
        action.data,
        {
          assetsRoot: proxy.assetsRoot,
          allWidgets: action.data.widgets || [],
          env: process.env.NODE_ENV === 'development' ? 'daily' : 'prod',
        },
      );
      // eslint-disable-next-line global-require,no-underscore-dangle
      window.globalConfig = AppConfig(window.__INITIAL_STATE__);
      // eslint-disable-next-line no-undef
      createStoreage.set('globalConfig', window.globalConfig);
      // eslint-disable-next-line no-undef,no-case-declarations
      const config = window.globalConfig;
      // eslint-disable-next-line no-case-declarations
      const generMenu = generRoutes(config.widgets || []);
      // eslint-disable-next-line no-case-declarations
      const flatMenu = generFlatRoutes(generMenu || []);
      return state.merge({
        globalConfig: config,
        menu: generMenu,
        flatMenu,
        currentMenu: generMenu[state.currentMenuIndex],
      });
    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,
        currentMenu: action.data[state.currentMenuIndex],
      });
    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: [],
        recentVisited: [],
      });
    case GET_CURRENT_INDEX:
      currentProduct.set('currentMenuIndex', action.data);
      return state.merge({
        currentMenuIndex: currentProduct.get('currentMenuIndex'),
      });
    case SLIDER_MENU_MODE:
      return state.merge({
        collapsed: action.data,
      });
    case LOGIN_MODE:
      return state.merge({
        loginMode: action.data,
      });
    case VALIDATE_AUTH:
      return state.merge({
        authValidate: action.data,
      });
    case GET_PATHNAME:
      return state.merge({
        pathname: action.data,
      });
    case SELECT_MENU_KEY:
      return state.merge({
        selectedKeys: action.data,
      });
    case EXTEND_OPEN_KEY:
      return state.merge({
        openKeys: action.data,
      });
    case COMPLEX_CONFIG:
      return state.merge({
        complexConfig: action.data,
      });
    case COMPLEX_PATHNAME:
      return state.merge({
        complexPathName: action.data,
      });
    case GET_MICRO_MOUNTED_STATUS:
      return state.merge({
        microMounted: action.data,
      });
    default:
      return state;
  }
};
export default appReducer;
