import { EntryBase } from '../ContractBase/EntryBase'
import type { PageBase } from '../ContractBase/PageBase'
import type { Message } from '../Communication/Message'
import { LoginAuthentication } from '../Library/Ext/Function/LoginAuthentication'
import { CheckManager } from '../ServiceProxy/Ext/ServiceHelper/CheckManager'
import type { IRecheckContract } from '../Contracts/IRecheckContract'
import type { IAuthorizeContract } from '../Contracts/IAuthorizeContract'
import type { ISubmitContract } from '../Contracts/ISubmitContract'
import { TradeModel } from '../Models/TradeModel'
import { PageHelper } from '../Presentation/PagesHelper'

/**
 * 复核逻辑入口
 */
export class RecheckEntry {
  /**
   * 权限编号
   */
  private powerCode: string

  /**
   * 准备复核数据事件
   */
  public previewRecheck?: (page: PageBase, args: PreviewRecheckEventArgs) => void

  /**
   * 构造函数
   */
  constructor(powerCode: string) {
    this.powerCode = powerCode
  }

  /**
   * 执行复核操作
   */
  public async raiseRecheck(): Promise<boolean> {
    if (!this.previewRecheck) return false

    const args = new PreviewRecheckEventArgs(PageData)
    this.previewRecheck(PageData, args)

    const recheckCondition = PageData.tradeAPIs.getCheckElement(args)
    if (!recheckCondition) return false

    recheckCondition.legalPerson = LoginAuthentication.instance.groupKinbrNo

    const checkManager = useCheckManager()
    checkManager.onAskSuperResult((result, dataBus) => {
      switch (result) {
        case 'Success':
          const irc = args.page as IRecheckContract
          const trademodel = PageHelper.getTradeModel(PageData)

          if (trademodel.metadata?.isAuth === 'true') {
            const contract = PageData as IAuthorizeContract
            if (!contract?.authorizeEntries?.length) {
              args.page.tradeAPIs.showInfoMessageBox('复核已通过，在下一步检查授权时出错，请检查')
              break
            }

            const entry = contract.authorizeEntries.find(x => x.powerCode === '0')
            if (!entry) {
              args.page.tradeAPIs.showInfoMessageBox('复核已通过，在下一步检查授权时出错，请检查')
              break
            }

            entry.loadData(irc.recheckEntries[0].pageData, irc.recheckEntries[0].messageData)
            args.page.tradeAPIs.showInfoMessageBox('复核已通过，将做下一步授权检查')
            if (!entry.raiseAuthorize()) break
          } else {
            args.page.tradeAPIs.showInfoMessageBox('复核已经通过，将提交交易')
          }

          const submit = args.page as ISubmitContract
          submit.submitEntry.loadData(irc.recheckEntries[0].pageData, irc.recheckEntries[0].messageData)
          submit.submitEntry.raiseSubmit()
          break

        case 'Failed':
          args.page.tradeAPIs.showInfoMessageBox('复核没有通过')
          break

        case 'Undo':
          args.page.tradeAPIs.showInfoMessageBox('复核没已取消')
          break
      }
    })

    return checkManager.doSuper(
      PageData,
      recheckCondition,
      args.customizeMessage || args.messageData,
      this.powerCode,
      args.enrollAccount,
      Number(args.enrollAmt),
      args.enrollAccountName,
      args.enrollDescription
    )
  }
}

/**
 * 准备复核数据事件参数
 */
export class PreviewRecheckEventArgs {
  public account: string = ''
  public amt: number = 0
  public deAccount: string = ''
  public crAccount: string = ''
  public sinAmt: number = 0
  public ctSigns: string = ''
  public operationType: string = ''
  public deAccountSeqn: string = ''
  public enrollAccount: string = ''
  public enrollAccountName: string = ''
  public enrollAmt: number = 0
  public enrollDescription: string = ''
  public customizePages: Uint8Array[] = []
  public customizeMessage?: Message
  public page: PageBase

  constructor(page: PageBase) {
    this.page = page
  }
}