/* eslint-disable no-unused-vars */
import {
  login,
} from '@/api/user';
import router from '@/router';

export default {
  namespaced: true,
  state: {
    isLogin: false,
    // userInfo: sessionStorage.getItem('userInfo') ?? {},
    userInfo: sessionStorage.getItem('userInfo') ? JSON.parse(sessionStorage.getItem('userInfo')) : {},
    walletInfo: {},
    checkLoginDialog: false,
    loginDialog: false,
    loginLoading: false,
    registerDialog: false,
    registerLoading: false,
    updateLoading: false,
    walletLoading: false,
    uploadWalletLoading: false,
    bindMobile: false,
  },
  mutations: {
    SET_REGISTER_DIALOG(state, val) {
      state.registerDialog = val;
    },
    SET_CHECK_LOGIN_DIALOG(state, val) {
      state.checkLoginDialog = val;
    },
    SET_LOGIN_DIALOG(state, val) {
      state.loginDialog = val;
    },
    SET_USER_INFO(state, val) {
      state.isLogin = !!Object.keys(val).length;
      state.userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
    },
    SET_WALLET_INFO(state, val) {
      state.walletInfo = val;
    },
    SET_LOADING(state, { key, val }) {
      state[key] = val;
    },
    SET_BINDMOBILE_DIALOG(state, val) {
      state.bindMobile = val;
    },
  },
  actions: {
    async register({ commit, dispatch }, data) {
      const {
        name,
        data: { clause, ...registerInfo },
      } = data;
      commit('SET_LOADING', { key: 'registerLoading', val: true });

      const fn = { account: register, quick: quickregister };

      const res = await fn[name]({ ...registerInfo, domain: document.domain });
      const { repassword, ...loginInfo } = registerInfo;
      const { username, password } = res;
      const loginParams = password ? { username, password } : loginInfo;
      commit('SET_LOADING', { key: 'registerLoading', val: false });
      dispatch('login', { ...loginParams, action: name }).then(() => {
        sessionStorage.setItem('pwd', loginParams.password);
        return Promise.resolve('success');
      });
      router.push({
        name: 'registerSuccess',
        query: {
          ...res,
          name,
        },
      });
      return res;
    },
    async login({ commit, dispatch }, { name, ...data }) {
      commit('SET_LOADING', { key: 'loginLoading', val: true });
      try {
        const res = await login(data);
        console.log('121312', res);
        const { token } = res;
        sessionStorage.setItem('token', token);
        sessionStorage.setItem('userInfo', JSON.stringify(res));
        commit('SET_USER_INFO', res);
        return true;
      } catch (err) {
        commit('SET_LOADING', { key: 'loginLoading', val: false });
        return false;
      }
    },
    async mobileLogin({ commit, dispatch }, data) {
      commit('SET_LOADING', { key: 'loginLoading', val: true });
      try {
        const res = await mobileLogin(data);
        const { token } = res;
        localStorage.setItem('token', token);
        dispatch('getWalletInfo');
        dispatch('getUserInfo');
        return true;
      } catch (err) {
        commit('SET_LOADING', { key: 'loginLoading', val: false });
        return false;
      }
    },
    async getUserInfo({ commit }, fn) {
      if (!fn) commit('SET_LOADING', { key: 'loginLoading', val: true });
      commit('SET_USER_INFO');
      commit('SET_LOGIN_DIALOG', false);
      fn?.();
      if (!fn) commit('SET_LOADING', { key: 'loginLoading', val: false });
      return Promise.resolve('success');
    },
    updateUserInfo({ commit }, data) {
      console.log(data)
      sessionStorage.setItem('token', data.token);
      sessionStorage.setItem('userInfo', JSON.stringify(data));
      commit('SET_USER_INFO', data);
    },
    getWalletInfo({ state, commit }) {
      if (state.walletLoading) {
        return;
      }

      const isRefresh = !!Object.keys(state.walletInfo).length;
      commit('SET_LOADING', {
        key: 'walletLoading',
        val: true,
      });
      wallet()
        .then((res) => {
          commit('SET_WALLET_INFO', res);
        })
        .finally(() => {
          commit('SET_LOADING', {
            key: 'walletLoading',
            val: false,
          });
        });
    },
    logout({ commit }) {
      localStorage.clear();
      sessionStorage.clear();
      commit('SET_USER_INFO', {});
      commit('SET_LOGIN_DIALOG', false);
      commit('SET_LOADING', { key: 'loginLoading', val: false });
      return Promise.resolve();
    },
  },
};
