import { ref, reactive, defineComponent } from 'vue'
import { TradeBusHelper } from './TradeBusHelper'
import { TradeInfoHelper } from './TradeInfoHelper'
import { TradeAPIsHelper } from './TradeAPIsHelper'
import { TradeResourcesHelper } from './TradeResourcesHelper'
import { PaperlessEntry } from './PaperlessEntry'
import { CashCodeEntry } from './CashCodeEntry'
import { PlatformLogger } from '../../Platform.Common/LogSystem/PlatformLogger'
import { ServiceSettings } from '../../TellerSystem.ServiceProxy/Ext/ServiceHelper/ServiceSettings'
import { TradeHandle } from '../../TellerSystem.Library/Ext/Function/TradeHandle'
import { PrintAttributes } from '../../Platform.Presentation/PageFunctions/PrintAttributes'

/**
 * 页面基类，填充通用的业务接口API
 * 注意:该对象需要支持序列化/反序列化
 */
export abstract class PageBase implements ITradeHelper {
    // 业务总线
    private _tradeBus: TradeBusHelper | null = null
    get TradeBus(): TradeBusHelper {
        if (!this._tradeBus) {
            this._tradeBus = new TradeBusHelper(this)
        }
        return this._tradeBus
    }

    // 交易信息
    private _tradeInfo: TradeInfoHelper | null = null
    get TradeInfo(): TradeInfoHelper {
        if (!this._tradeInfo) {
            this._tradeInfo = new TradeInfoHelper(this)
        }
        return this._tradeInfo
    }

    // 操作接口
    private _tradeAPIs: TradeAPIsHelper | null = null
    get TradeAPIs(): TradeAPIsHelper {
        if (!this._tradeAPIs) {
            this._tradeAPIs = new TradeAPIsHelper(this)
        }
        return this._tradeAPIs
    }

    // 资源集合
    private _tradeResources: TradeResourcesHelper | null = null
    get TradeResources(): TradeResourcesHelper {
        if (!this._tradeResources) {
            this._tradeResources = new TradeResourcesHelper(this)
        }
        return this._tradeResources
    }

    // 无纸化对象
    private paperless: PaperlessEntry | null = null
    get Paperless(): PaperlessEntry {
        if (!this.paperless) {
            this.paperless = new PaperlessEntry()
        }
        return this.paperless
    }

    // 冠字号对象
    private cashCode: CashCodeEntry | null = null
    get CashCode(): CashCodeEntry {
        if (!this.cashCode) {
            this.cashCode = new CashCodeEntry()
        }
        return this.cashCode
    }

    // 银行标签
    get BankFlag(): string {
        return ServiceSettings.BankFlag
    }

    // 打印事件
    private pagePrintedHandlers: ((attr: PrintAttributes) => void)[] = []

    /**
     * 初始化页面
     */
    public initPage(): boolean {
        return true
    }

    /**
     * 页面加载完成
     */
    protected onTradePageLoaded(): void {
        let flag = false
        try {
            // 加载页面初始化
            flag = this.initPage()
            if (flag) {
                // 焦点管理逻辑
                // 将交易打开的时间登记到bus
                try {
                    if (!this.TradeBus.get('__StartTime__')) {
                        this.TradeBus.set('__StartTime__', new Date().toISOString())
                    }
                } catch (ex) {
                    TradeHandle.writeImportantLog('获取交易打开时间异常', ex.message, 'StartTime')
                }
            }
        } catch (e) {
            PlatformLogger.tradeErrorInfo('调用InitPage失败！', e, this.TradeModel.Code)
        }
        if (!flag) {
            // 关闭页面逻辑
        }
    }

    /**
     * 记录打印日志
     */
    private log2PagePrinted(log: string): void {
        try {
            PlatformLogger.systemInfo(`Log2PagePrinted:start 【${log}】=>${this.pagePrintedHandlers.length}`)
            if (this.pagePrintedHandlers.length === 0) return
            PlatformLogger.systemInfo(`Log2PagePrinted:list length=>${this.pagePrintedHandlers.length}`)
        } catch {}
    }

    /**
     * 触发打印后事件
     */
    public onPagePrinted(attr: PrintAttributes): void {
        PlatformLogger.systemInfo(`OnPagePrinted:触发事件=>${this.pagePrintedHandlers.length}`)
        if (this.pagePrintedHandlers.length > 0) {
            this.log2PagePrinted('OnPagePrinted.start')
            this.pagePrintedHandlers.forEach(handler => handler(attr))
            this.log2PagePrinted('OnPagePrinted.end')
        }
    }

    /**
     * 添加打印事件监听
     */
    public addPagePrintedListener(handler: (attr: PrintAttributes) => void): void {
        this.pagePrintedHandlers.push(handler)
    }

    /**
     * 移除打印事件监听
     */
    public removePagePrintedListener(handler: (attr: PrintAttributes) => void): void {
        this.pagePrintedHandlers = this.pagePrintedHandlers.filter(h => h !== handler)
    }

    /**
     * 清理缓存数据
     */
    public clear(): void {
        this.paperless = null
        this._tradeAPIs = null
        this._tradeBus = null
        this._tradeInfo = null
        this._tradeResources = null
    }
}

// Vue组件封装
export const PageBaseComponent = defineComponent({
    extends: PageBase,
    methods: {
        initPage(): boolean {
            return super.initPage()
        },
        onPagePrinted(attr: PrintAttributes): void {
            super.onPagePrinted(attr)
        }
    }
})