import { toRefs, shallowReadonly, ShallowRef } from 'vue'
import { login, queryLoginId } from '@/services/api/account'
import { VueStore } from '../base'
import cryptojs from 'crypto-js'
import WebStorage from '@/utils/storage/base'

interface StoreState {
    loading: boolean;
    loginInfo: ShallowRef<Proto.LoginRsp>;
}

function getInitData(): Proto.LoginRsp {
    return {
        AccountIDs: [],
        LoginCode: '',
        LoginID: 0,
        LoginUserType: 0,
        AccountStatus: 0,
        UserID: 0,
        ClientID: 0,
        MemberUserID: 0,
        Token: '',
    }
}

/**
 * 登录存储类
 */
const store = new (class extends VueStore<StoreState> {
    constructor() {
        const storage = new WebStorage(sessionStorage, 'loginInfo', getInitData())
        const state: StoreState = {
            loading: false,
            loginInfo: storage.getRef(),
        }
        super(state)
    }

    actions = {
        /** 用户登录 */
        userLogin: (param: Proto.LoginReq) => {
            this.state.loading = true
            return new Promise<Proto.LoginRsp>((resolve, reject) => {
                queryLoginId({
                    data: {
                        username: param.LoginID
                    },
                    success: (res) => {
                        login({
                            data: {
                                ...param,
                                LoginID: res.data,
                                LoginPWD: cryptojs.SHA256(res.data + param.LoginPWD).toString(),
                            },
                            success: (res) => {
                                this.state.loginInfo = res
                                resolve(res)
                            },
                            fail: (err) => {
                                reject(err)
                            },
                            complete: () => {
                                this.state.loading = false
                            }
                        })
                    },
                    fail: (err) => {
                        this.state.loading = false
                        reject(err)
                    }
                })
            })
        },
        /** 获取用户登录信息 */
        getLoginInfo: <K extends keyof Proto.LoginRsp>(key: K) => {
            return this.state.loginInfo[key]
        },
        /** 获取登录令牌 */
        getToken: () => {
            return this.state.loginInfo.Token
        },
        /** 获取用户ID */
        getUserId: () => {
            return this.state.loginInfo.UserID
        },
        /** 获取登录ID */
        getLoginId: () => {
            return this.state.loginInfo.LoginID
        },
        /** 获取首个账户ID */
        getFirstAccountId: () => {
            const accounts = this.state.loginInfo.AccountIDs
            return accounts[0] ?? 0
        },
        /** 重置数据 */
        reset: () => {
            this.state.loginInfo = getInitData()
        }
    }
})

export function useLoginStore() {
    return shallowReadonly({
        ...toRefs(store.state),
        ...store.actions,
        ...store.methods,
    })
}