import { UID, IS_AUTH, ERROR_STATE, MENUS, LOCALE, PARAMS, AVAILABLE_MY_MENUS, MY_MENUS } from './mutation_types';

import api from '@/service';
import { find } from 'lodash';

const setUID = ({ commit }, data) => {
  commit(UID, data);
};

const setErrorState = ({ commit }, data) => {
  commit(ERROR_STATE, data);
};

const setIsAuth = ({ commit }, data) => {
  commit(IS_AUTH, data);
};

const setMenus = ({ commit }, data) => {
  commit(MENUS, data);
};

const setAvailableMyMenus = ({ commit }, data) => {
  commit(AVAILABLE_MY_MENUS, data);
};

const setMyMenus = ({ commit }, data) => {
  commit(MY_MENUS, data);
};

const setLocale = ({ commit }, data) => {
  commit(LOCALE, data);
};

const setParams = ({ commit }, data) => {
  commit(PARAMS, data);
};

const processResponse = (store, loginResponse) => {
  // http://devops.sdsdev.co.kr/confluence/pages/viewpage.action?pageId=76315514 code표 참고
  // console.log('processResponse:', loginResponse);
  switch (loginResponse.code) {
    case 'authOk':
      setUID(store, loginResponse.uid);
      setErrorState(store, { code: loginResponse.code });
      setIsAuth(store, true);
      break;
    case 'exception':
      setErrorState(store, { code: loginResponse.code, message: 'System error' });
      setIsAuth(store, false);
      break;
    default:
      // console.log(typeof loginResponse.code);
      if (typeof loginResponse.code === 'number') {
        loginResponse.code = String(loginResponse.code);
      }
      setErrorState(store, loginResponse);
      setIsAuth(store, false);
      break;
  }
};

const myMenus = (store, menu) => {
  let myMenus = [];
  menu.map(item => {
    myMenus.push(find(store.getters.getAvailableMyMenus, ['menuId', item]));
  });
  return myMenus;
}

export default {
  async login(store, { userId, password }) {
    const loginResponse = await api.login(userId, password);
    processResponse(store, loginResponse);
    return store.getters.getIsAuth;
  },
  async loginNewEpTray(store, { userInfo, key }) {
    const loginResponse = await api.loginNewEpTray(userInfo, key);
    processResponse(store, loginResponse);
    return store.getters.getIsAuth;
  },
  logout() {
    api.logout();
  },
  async getMenus(store, forceRemote = false) {
    const menuResponse = await api.getMenus(forceRemote);
    setMenus(store, menuResponse);
    const availableMyMenu = menuResponse.filter(menu => {
      // 외부 링크 사용
      if (menu.externalUrlUsed) {
        return true;
      }
      // default Page 가 있는 메뉴
      return menu.pages.length > 0 && find(menu.pages, ['defaulted', true]);
    }).map(menu => {
      const defaultPage = find(menu.pages, ['defaulted', true]);
      return {
        ...menu,
        ...defaultPage,
        selected: false,
      };
    });
    setAvailableMyMenus(store, availableMyMenu);
  },
  async getMyMenus(store) {
    const menuResponse = await api.menuService.getMyMenus();
    setMyMenus(store, myMenus(store, menuResponse));
  },
  addMyMenu(store, menuIds) {
    api.menuService.getMyMenus().then(menuResponse => {
      menuResponse.push(menuIds);
      api.menuService.addMyMenu(menuResponse).then(() => {
        setMyMenus(store, myMenus(store, menuResponse));
      });
    });
  },
  deleteMyMenu(store, menuId) {
    api.menuService.deleteMyMenu(menuId).then(() => {
      api.menuService.getMyMenus().then((menuResponse) => {
        setMyMenus(store, myMenus(store, menuResponse));
      });
    });
  },
  moveMyMenu(store, menuIds) {
    api.menuService.addMyMenu(menuIds).then(() => {
      setMyMenus(store, myMenus(store, menuIds));
    });
  },
  clearCache() {
    api.clearCache();
  },
  setLocale,
  setParams,
};
