import { fromJS } from 'immutable';
import Cookies from 'js-cookie';
import AppConfig from 'kit_global_config';
import { Storeage as Store } from 'kit_utils';

import { Storage } from '@wisdom-utils/utils';

import defaultSetting from '../../../../config/defaultSetting';
import generRoutes, { generFlatRoutes } from '../../../utils/routes';
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_MICRO_MOUNTED_STATUS,
  GET_PATHNAME,
  GET_RCENT_KEYWORD,
  GET_RECENT_PRODUCT,
  GET_RECENT_VISITED,
  GET_THEME_CONFIG,
  LOGIN_MODE,
  LOGINOUT,
  SELECT_MENU_KEY,
  SLIDER_MENU_MODE,
  VALIDATE_AUTH,
} 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', Cookies.get('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.__INITIAL_STATE__.mdi = 'SDI';
      window.__INITIAL_STATE__.menu = 'banner-left';
      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;
      // config.access_token = "";
      // eslint-disable-next-line no-case-declarations
      const generMenu = generRoutes(config.widgets || []);
      // eslint-disable-next-line no-case-declarations
      const { homepage } = window.globalConfig;
      if (homepage !== '') {
        // const index = generMenu.findIndex(item => item.level === -1);
        // if (index > -1) {
        //   generMenu = generMenu.reverse();
        // }
      }
      // eslint-disable-next-line no-case-declarations
      const flatMenu = generFlatRoutes(generMenu || []);
      Cookies.set('city', Cookies.get('city') || window.globalConfig.client);
      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,
      });
    case LOGINOUT:
      return state.merge({
        complexConfig: action.data.complexConfig,
        currentMenuIndex: action.data.currentMenuIndex,
      });
    default:
      return state;
  }
};
export default appReducer;
