import { ActionTree, ActionContext } from 'vuex'
import { RootState, store, useStore } from '@/store'
import { UserState } from './state'
import { Mutations } from './mutations'
import { UserMutationTypes } from './mutation-types'
import { UserActionTypes } from './action-types'
import { getCookieMac, removeToken, setSessionUserInfo, setToken } from '@/utils/cookies'
import { PermissionActionType } from '../permission/action-types'
import router, { resetRouter } from '@/router'
import { useRoute, useRouter } from 'vue-router'
import { RouteRecordRaw } from 'vue-router'
import { getUsersInfo } from '@/apis/user'
import { clearAllLocalData, getLocalProjectId, setLocalProjectId } from '@/utils'
import { Toast } from 'vant'

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,
    userInfo: { username: string, password: string }
  ) {
    console.log(userInfo)
    // let { username, password } = userInfo
    // username = username.trim()
    // await loginRequest({ username, password }).then(async(res) => {
    //   if (res?.code === 0 && res.data.accessToken) {
    //     setToken(res.data.accessToken)
    commit(UserMutationTypes.SET_TOKEN, 'eyJhbGciOiJIUzI1NiJ9.eyJidWlsZGluZ19pZCI6ImZmZDc4OGIwOTQzNTQ4NTFiMDM0NTY2YzRkMTJkMTFjIiwidXNlcl9pZCI6IjYwMmVmNzA5YmViNjRhMWY5YTRiNmFmMjE5NmQwYWY3IiwidXNlcl9uYW1lIjpudWxsLCJvd25lcl9pZCI6IjkzNmE4ZWFlMDJmOTExZWNiNTQyMDJiNzIzZjVmYmQwIiwibXlfdGhpcmRfaWQiOiIyMGQ4Mzg0OTZjMGQ0ODk4YjFkN2MxNmRkZTc5ZGQwNiIsInRoaXJkX3BsYXRmb3JtX2lkIjoib3pUVGs1UFJSdUNIRUNqUXZKSXJyT3A1MkRuNCIsInNUaW1lIjoxNjQ2MDM1ODYzNTYwLCJleHAiOjE2NDYzOTU4NjM1NjAsInR5cGUiOiJ3ZWNoYXQiLCJhcHBfaWQiOiJ3eDNhZGQ2MzJmZDMxNDU3YzcifQ.C0khcRbjp_VuGF8gzr-cdybPhX1U1bSEk_48ypub6Y0')
    //   }
    // }).catch((err) => {
    //   console.log(err)
    // })
  },

  [UserActionTypes.ACTION_RESET_TOKEN](
    { commit }: AugmentedActionContext) {
    removeToken()
    commit(UserMutationTypes.SET_TOKEN, '')
    // commit(UserMutationTypes.SET_ROLES, [])
  },
  async [UserActionTypes.ACTION_GET_USER_INFO]({ commit }: AugmentedActionContext) {
    // let nowProjectId: any = 'Pj1101080259'
    const userInfo: any = {
      userName: '陈珍',
      userPhone: 15321277745,
      userId: '602ef709beb64a1f9a4b6af2196d0af7'
      // userId: '9a1ecfbacb6b4f249bf2dd3ec7793ead'
      // userId: '1302990695845462811'
    }
    setSessionUserInfo(JSON.stringify(userInfo))
    commit(UserMutationTypes.SET_USER_NAME, userInfo.userName)
    commit(UserMutationTypes.SET_USER_PHONE, userInfo.phone)
    commit(UserMutationTypes.SET_USER_ID, userInfo.userId)
    let macAdr: any = getCookieMac()
    commit(UserMutationTypes.SET_USER_MAC, macAdr)
    // store.commit(UserMutationTypes.SET_PROJECT_ID, nowProjectId)
  },
  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, [])
    store.state.permission.dynamicRoutes.forEach((item: RouteRecordRaw) => {
      router.addRoute(item)
    })
  },

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