/*
 * @Description: app actions
 * @Author: ZY
 * @Date: 2020-12-23 10:25:37
 * @LastEditors: scy😊
 * @LastEditTime: 2021-01-29 08:46:37
 */
import {ActionTree, ActionContext} from 'vuex';
import {RootState, useStore} from '@/store';
import {state, UserState} from './state';
import {Mutations} from './mutations';
import {UserMutationTypes} from './mutation-types';
import {UserActionTypes} from './action-types';
import {login} from '@/apis/user';
import {removeToken, setToken} from '@/utils/cookies';
import {PermissionActionType} from '../permission/action-types';
import router, {resetRouter} from '@/router';
import {RouteRecordRaw} from 'vue-router';
import {ElMessage} from "element-plus";
import {walletLogout, walletSign} from "@/apis/wallet";

type AugmentedActionContext = {
  commit<K extends keyof Mutations>(
    key: K,
    payload: Parameters<Mutations[K]>[1],
  ): ReturnType<Mutations[K]>
} & Omit<ActionContext<UserState, RootState>, 'commit'>

export interface Actions {
  [UserActionTypes.ACTION_LOGIN](
    {commit}: AugmentedActionContext,
    userInfo: { username: string, password: string }
  ): void;

  [UserActionTypes.ACTION_RESET_TOKEN](
    {commit}: AugmentedActionContext
  ): void;

  [UserActionTypes.ACTION_GET_USER_INFO](
    {commit}: AugmentedActionContext
  ): void;

  [UserActionTypes.ACTION_CHANGE_ROLES](
    {commit}: AugmentedActionContext, role: string
  ): void;

  [UserActionTypes.ACTION_LOGIN_OUT](
    {commit}: AugmentedActionContext,
  ): void;
}

export const actions: ActionTree<UserState, RootState> & Actions = {
  async [UserActionTypes.ACTION_LOGIN](
    {commit}: AugmentedActionContext,
  ) {
    try {
      const signInfo = await walletSign();
      const res: any = await login({
        data: signInfo.raw,
        sign: signInfo.sign,
      });
      setToken(res.data.token);
      commit(UserMutationTypes.SET_TOKEN, res.data.token);

    } catch (e) {
      console.error(e);
      ElMessage.error('登录失败：' + e);
      throw e;
    }
  },

  [UserActionTypes.ACTION_RESET_TOKEN](
    {commit}: AugmentedActionContext) {
    removeToken();
    commit(UserMutationTypes.SET_TOKEN, '');
    commit(UserMutationTypes.SET_ROLES, []);
  },

  async [UserActionTypes.ACTION_GET_USER_INFO](
    {commit}: AugmentedActionContext
  ) {
    if (state.token === '') {
      throw Error('token is undefined!');
    }
    // await userInfoRequest().then((res: any) => {
    //   if (res?.code === 0) {
    commit(UserMutationTypes.SET_ROLES, ['admin']);
    // commit(UserMutationTypes.SET_NAME, res.data.name);
    // commit(UserMutationTypes.SET_AVATAR, res.data.avatar);
    // commit(UserMutationTypes.SET_INTRODUCTION, res.data.introduction);
    // commit(UserMutationTypes.SET_EMAIL, res.data.email);
    //     return res;
    //   } else {
    //     throw Error('Verification failed, please Login again.');
    //   }
    // });
  },

  async [UserActionTypes.ACTION_CHANGE_ROLES](
    {commit}: AugmentedActionContext,
    role: string
  ) {
    const token = role + '-token';
    const store = useStore();
    commit(UserMutationTypes.SET_TOKEN, token);
    setToken(token);
    await store.dispatch(UserActionTypes.ACTION_GET_USER_INFO, undefined);
    store.dispatch(PermissionActionType.ACTION_SET_ROUTES, state.roles);
    store.state.permission.dynamicRoutes.forEach((item: RouteRecordRaw) => {
      router.addRoute(item);
    });
  },

  [UserActionTypes.ACTION_LOGIN_OUT](
    {commit}: AugmentedActionContext
  ) {
    console.log(commit);
    removeToken();
    walletLogout();
    commit(UserMutationTypes.SET_TOKEN, '');
    commit(UserMutationTypes.SET_ROLES, []);
    resetRouter();
  }
};
