import { createAction } from 'redux-actions';
import { replace } from 'react-router-redux';
import * as types from '../constant/dictActions';
import { PFetch } from '../system/fetch';
import links from '../constant/links';
// import { LoginServer } from './login';
// import SessionStorage from '../utils/session-storage';
import { getMenus, saveMenus, saveMenuGlobal, getMenuClobal } from '../service/LocalStorage';
import { getAllLeafs } from '../utils/MenuHelper';


const STORAGE_KEY_PROFILE = '@AS:profile';

export const initDataFromServerDispatch = createAction(types.INIT_DATA_FROM_SERVER);

export const initDataFromServer = () => dispatch => PFetch(links.getMenus, {})
  .then(response => {
    if ((!getMenus() || getMenus().length === 0) && response.menus && response.menus.length > 0) {
      // getMenus();
      const quickMenu = getAllLeafs(response.menus).slice(0, 8).map(i => i.menuCode);
      saveMenus(quickMenu);
      response.quickMenu = quickMenu;
    }
    return response;
  })
  .then(response => {
    dispatch(initDataFromServerDispatch({ menu: response.menus, quickMenu: response.quickMenu, layoutType: response.layoutType || '' }));
    return response.menus;
  }).then(menu => {
    saveMenuGlobal(menu);
  });

export const initDataFromServerNoMenu = () => dispatch => new Promise((resolve, reject) => resolve())
  .then(response => {
    dispatch(initDataFromServerDispatch({ menu: [] }));
  });

export const initApp = () => {
  // const payload = SessionStorage.get(STORAGE_KEY_PROFILE) || {};
  const quickMenu = getMenus();
  return createAction(types.INIT_APP)({ quickMenu });
};

export const fetchStart = createAction(types.FETCH_START);
export const fetchEnd = createAction(types.FETCH_END);

export const updateQuery = createAction(types.UPDATE_QUERY);

const authLogin = createAction(types.AUTH_LOGIN);
const authLogout = createAction(types.AUTH_LOGOUT);
const forceLogoutAction = createAction(types.AUTH_FORCE_LOGOUT);

export const updateEnterMenuId = enterMenuId => createAction(types.UPDATE_ENTER_MENU_ID)(enterMenuId);

export const updateQuickMenu = quickMenu => {
  saveMenus(quickMenu);
  return createAction(types.UPDATE_QUICK_MENU)(quickMenu);
};

export const resetMenu = createAction(types.MENU_RESET);

// eslint-disable-next-line no-unused-vars
// export const loginServer = (isRemember, params) => dispatch =>
//  PFetch(links.login, params).then(d => {
//    // if (isRemember) {
//    //
//    // }
//    SessionStorage.set(STORAGE_KEY_PROFILE, { token: d.userToken });
//    dispatch(authLogin(d.userToken));
//  }).then(() => {
//    PFetch(links.getMenus, {}).then(menusData => {
//      dispatch(initDataFromServerDispatch({ menu: menusData.menus }));
//    });
//  });

export const logout = () => dispatch =>
  PFetch(links.logout, {}).then(() => {
    // SessionStorage.set(STORAGE_KEY_PROFILE, { token: '' });
    dispatch(replace('/login'));
    dispatch(authLogout());
  });

export const forceLogout = () => dispatch => {
  // SessionStorage.set(STORAGE_KEY_PROFILE, { token: '' });
  dispatch(replace('/login'));
  dispatch(forceLogoutAction());
};
