import { reactive, ref } from 'vue'
import { PageBase } from '../ContractBase/PageBase'
import { CheckConditionElements } from './CheckConditionElements'
import { Message } from '../../Communication/Message'
import { TradeBusData } from '../../Models/TradeBusData'
import { TradeInfoData } from '../../Models/TradeInfoData'
import { CheckDataHelper } from './CheckDataHelper'
import { CheckElements } from './CheckElements'
import { LoginUserInfo } from '../../Models/UserInfo'
import { TradeHandle } from '../../Library/Ext/TradeExtension/TradeHandle'

/**
 * 复核和授权结果类型
 */
export enum AsksuperResultType {
    Success = 0,
    Failed = 1,
    Undo = 2
}

/**
 * 复核对象，交易中使用此对象来提交一个复核请求
 */
export class CheckManager {
    private static _instance: CheckManager
    private _systemSerialNumber: string = ''
    
    public static get Instance(): CheckManager {
        if (!CheckManager._instance) {
            CheckManager._instance = new CheckManager()
        }
        return CheckManager._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 DoSuper(
        target: PageBase,
        element: CheckConditionElements,
        msg: Message,
        funcnum: string,
        accountno: string,
        amount: number,
        custname: string,
        des: string
    ): Promise<boolean> {
        const t = await this.CheckSuper(target, element, msg, TradeInfoData.GetTradeInfo(target).TradeCode, funcnum)
        
        if (t) {
            const tradedata = TradeInfoData.GetTradeInfo(target).TradeBus
            tradedata["MSG"] = target.Serialize(msg)
            return this.AskSuper(target, this.GetIPageElements(target), accountno, amount, custname, des)
        }
        return true
    }

    /**
     * 得到tradepage中的需要复核回显和复核的数据项
     */
    private GetIPageElements(page: PageBase): CheckElements {
        const elements = this.CollectElements(page)
        const checkElements = new CheckElements()
        
        elements.forEach(element => {
            // 处理不同类型控件的转换逻辑
            // ...
        })
        
        return checkElements
    }

    /**
     * 收集需要复核的元素
     */
    private CollectElements(page: PageBase): any[] {
        const elements: any[] = []
        // 实现元素收集逻辑
        // ...
        return elements
    }

    /**
     * 检查复核
     */
    private async CheckSuper(
        target: PageBase,
        element: CheckConditionElements,
        msg: Message,
        tradeNo: string,
        funcnum: string
    ): Promise<boolean> {
        // 实现复核检查逻辑
        // ...
        return false
    }

    /**
     * 申请复核
     */
    private async AskSuper(
        target: PageBase,
        superdata: CheckElements,
        accountno: string,
        amount: number,
        custname: string,
        des: string
    ): Promise<boolean> {
        // 实现复核申请逻辑
        // ...
        return false
    }

    /**
     * 整合核心返回要素值与前台要素值
     */
    private MergeElement(element: CheckConditionElements, msg: Message): string {
        return JSON.stringify({
            Message: msg.SerializeForAuth(),
            Element: element.JsonValue
        })
    }
}

// Vue组合式API封装
export const useCheckManager = () => {
    const checkManager = reactive(CheckManager.Instance)
    return {
        checkManager,
        AsksuperResultType
    }
}