import { reactive, ref } from 'vue'
import { AuthConditionElement } from './AuthConditionElement'
import { Message } from '../../Communication/Message'
import { PageBase } from '../../ContractBase/PageBase'
import { TradeBusData } from '../../Models/TradeBusData'
import { TradeInfoData } from '../../Models/TradeInfoData'
import { AuthDataHelper } from './AuthDataHelper'
import { LoginUserInfo } from '../../Models/UserInfo'
import { TradeHandle } from '../../Library/Ext/TradeExtension/TradeHandle'
import { UserManagerHandle } from '../../Library/Ext/Function/UserManagerHandle'
import { AuthHandle } from '../../ServiceProxy/Ext/WebAPI/Auth/AuthHandle'
import { ElementInfoResult } from '../../ServiceProxy/Ext/WebAPI/Auth/Entity/ElementInfoResult'
import { QueryTellerStatusBatch } from '../../ServiceProxy/Ext/ServiceHelper/QueryTellerStatusBatch'
import { LoginAuthentication } from './LoginAuthentication'

/**
 * 授权对象，在交易中使用此对象来申请授权
 */
export class AuthManager {
    private static _instance: AuthManager
    private _systemSerialNumber: string = ''
    
    public static get Instance(): AuthManager {
        if (!AuthManager._instance) {
            AuthManager._instance = new AuthManager()
        }
        return AuthManager._instance
    }

    get SystemSerialNumber(): string {
        return this._systemSerialNumber
    }
    set SystemSerialNumber(value: string) {
        this._systemSerialNumber = value
    }

    // 授权有结果时触发的事件
    public onAsksuperResult: ((result: AsksuperResultType, busData: TradeBusData) => void) | null = null

    /**
     * 检查授权
     */
    public async DoAuth(
        target: PageBase,
        element: AuthConditionElement,
        msg: Message,
        pagedata: PageData[],
        funcnum: string,
        accountno: string,
        amount: string,
        count: string,
        custname: string,
        des: string,
        branch: string
    ): Promise<boolean> {
        const data = TradeInfoData.GetTradeInfo(target)
        const tradedata = data.TradeBus
        const pagedatas = pagedata.length > 0 ? pagedata[0].datas : new Uint8Array(0)

        if (!element) {
            this.ClearDataBus(target, pagedatas)
            return false
        }

        if (!pagedata && target) {
            pagedata = [{
                index: 1,
                datas: target.SaveTradePage(target.Content)
            }]
        }

        this.SystemSerialNumber = target.CreateSerialNumber()
        msg.SerialNumber = this.SystemSerialNumber

        const result = await this.CheckAuth(target, element, msg, data.TradeCode, funcnum, branch, pagedatas)
        if (!result) return false

        const authdata = new AuthDataHelper(target)
        const listImgTrade: ImageSource[] = []

        // ... 其他业务逻辑实现 ...

        return true
    }

    /**
     * 本地授权处理
     */
    private async LocalAuth(
        target: PageBase,
        pagedata: PageData[],
        accountno: string,
        amount: number,
        custname: string,
        des: string,
        tradedata: TradeBusData
    ): Promise<boolean> {
        // ... 本地授权业务逻辑实现 ...
        return true
    }

    /**
     * 远程授权处理
     */
    private async RemoteAuth(
        target: PageBase,
        pagedata: PageData[],
        accountno: string,
        amount: number,
        custname: string,
        des: string
    ): Promise<boolean> {
        // ... 远程授权业务逻辑实现 ...
        return true
    }

    // ... 其他方法实现 ...
}

// Vue组合式API封装
export const useAuthManager = () => {
    const authManager = reactive(AuthManager.Instance)
    return {
        authManager
    }
}

// 类型定义
interface PageData {
    index: number
    datas: Uint8Array
}

type ImageSource = any
type AsksuperResultType = 'Success' | 'Failed' | 'Cancel'