import { BusinessError } from '@kit.BasicServicesKit';
import { ComponentContent, promptAction } from '@kit.ArkUI';
import { UIContext } from '@ohos.arkui.UIContext';
import { BasicType, YTLog } from '../../../../Index';
import { yTDoubleConfirm } from '../components/generalComp/YtDoubleConfirm';
import { agreePrivacy } from '../components/generalComp/AgreePrivacy';
import { LoginCollect } from '../models/LoginCollect';

interface InitOption {
  context: UIContext;
  options: promptAction.BaseDialogOptions;
}


class Params<T> implements BasicType<T> {
  src?: ResourceStr
  acSrc?: ResourceStr
  text?: string
  index?: number
  number?: number | undefined
  message?: string
  loginType?: LoginCollect
  click?: () => void

  constructor(item: BasicType<undefined> = {}) {
    this.src = item.src;
    this.text = item.text;
    this.index = item.index;
    this.number = item.number;
    this.click = item.click;
    this.message = item.message;
    this.acSrc = item.acSrc;
    this.loginType = item.loginType;
  }
}

export class YTToast {
  private static declare instance: YTToast
  private declare ctx: UIContext;
  private declare contentNode: ComponentContent<Object> | null;
  private declare options: promptAction.BaseDialogOptions;
  private maskColor: string = '#80000000'

  static getInstance() {
    if (!YTToast.instance) {
      YTToast.instance = new YTToast()
    }
    return YTToast.instance
  }

  init(initOption: InitOption) {
    this.ctx = initOption.context;
    initOption.options.maskColor = this.maskColor
    this.options = initOption.options;
  }

  setContext(context: UIContext) {
    this.ctx = context;
  }

  setContentNode(node: ComponentContent<Object>) {
    this.contentNode = node;
  }

  setOptions(options: promptAction.BaseDialogOptions) {
    this.options = options;
  }

  openToast(builder: WrappedBuilder<[
    BasicType<undefined>
  ]>, item: BasicType<undefined>) {
    this.contentNode =
      new ComponentContent(this.ctx, builder, new Params<undefined>(item));
    this.ctx.getPromptAction()
      .openCustomDialog(this.contentNode, this.options)
      .then(() => {
        console.info('OpenCustomDialog complete.')
      })
      .catch((error: BusinessError) => {
        let message = (error as BusinessError).message;
        let code = (error as BusinessError).code;
        console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
      })
  }

  doubleConfirm(item: BasicType<undefined>) {
    this.openToast(wrapBuilder(yTDoubleConfirm), item)
  }

  agreePrivacy(item: BasicType<undefined>) {
    this.openToast(wrapBuilder(agreePrivacy), item)
  }


  hide() {
    try {
      if (this.contentNode !== null) {
        this.ctx.getPromptAction()
          .closeCustomDialog(this.contentNode)
          .then(() => {
            this.contentNode = null
            console.info('CloseCustomDialog complete.')
          })
          .catch((error: BusinessError) => {
            let message = (error as BusinessError).message;
            let code = (error as BusinessError).code;
            console.error(`CloseCustomDialog args error code is ${code}, message is ${message}`);
          })
      }
    } catch (e) {
      YTLog.warn(e)
    }
  }

  updateDialog(options: promptAction.BaseDialogOptions) {
    if (this.contentNode !== null) {
      this.ctx.getPromptAction()
        .updateCustomDialog(this.contentNode, options)
        .then(() => {
          console.info('UpdateCustomDialog complete.')
        })
        .catch((error: BusinessError) => {
          let message = (error as BusinessError).message;
          let code = (error as BusinessError).code;
          console.error(`UpdateCustomDialog args error code is ${code}, message is ${message}`);
        })
    }
  }
}

export const yTToast = YTToast.getInstance()

