import { ComponentContent } from '@kit.ArkUI';
import { StrUtil } from '@pura/harmony-utils';
import { DialogCallback } from '../callback/DialogCallback';

export declare class TipDialogOption {
  title?: string;
  content: string;
  leftText?: string;
  rightText?: string;
  callback?: DialogCallback;
}

@Builder
function defaultBuilder(option: TipDialogOption) {
  Stack({alignContent: Alignment.Top}) {
    Image($r('app.media.ic_tip_dialog_top_bg')).width('100%').aspectRatio(2.72)

    Column() {
      Text(option.title)
        .fontColor($r('app.color.color_212226'))
        .fontSize(22)
        .fontFamily('ysbth')
        .fontWeight(FontWeight.Medium)
        .visibility(StrUtil.isEmpty(option.title) ? Visibility.Hidden : Visibility.Visible)
      Text(option.content)
        .textAlign(TextAlign.Center)
        .fontColor(StrUtil.isNotEmpty(option.title) ? $r('app.color.color_727686') : $r('app.color.color_212226'))
        .fontSize(StrUtil.isNotEmpty(option.title) ? 14 : 22)
        .fontWeight(StrUtil.isNotEmpty(option.title) ? FontWeight.Normal : FontWeight.Medium)
        .textOverflow({ overflow: TextOverflow.Ellipsis })
        .ellipsisMode(EllipsisMode.END)
        .margin({ left: 27, top: 12, right: 27 })
        .maxLines(5)
      Row(){
        Button(StrUtil.isNotEmpty(option.leftText) ? option.leftText : '取消', { type: ButtonType.Capsule, stateEffect: true })
          .width(110)
          .height(40)
          .fontColor($r('app.color.color_80859B'))
          .fontSize(16)
          .backgroundColor($r("app.color.color_f1f2f6"))
          .margin({left: 20})
          .onClick(() => {
            TipDialog.dismiss();
            if (option.callback?.cancel) {
              option.callback?.cancel();
            }
          })
        Blank().layoutWeight(1)
        Button(StrUtil.isNotEmpty(option.rightText) ? option.rightText : '确定', { type: ButtonType.Capsule, stateEffect: true })
          .width(110)
          .height(40)
          .fontColor(Color.White)
          .fontSize(16)
          .backgroundColor($r("app.color.color_466afd"))
          .margin({right: 20})
          .onClick(() => {
            TipDialog.dismiss();
            if (option.callback?.confirm) {
              option.callback?.confirm();
            }
          })
      }
      .margin({top: 20})
    }
    .width('100%')
    .padding({ top: 20, bottom: 20 })
  }
  .borderRadius(20)
  .backgroundColor(Color.White)
  .width('80%')
}

export class TipDialog {
  context: UIContext | null = null;
  contentNode: ComponentContent<Object> | null = null;

  private static instance: TipDialog | null = null;

  static show(context: UIContext, option: TipDialogOption, canCancel: boolean = true) {
    if (TipDialog.instance === null) {
      TipDialog.instance = new TipDialog(context, option);
    }
    TipDialog.instance?.openDialog(canCancel);
  }

  static dismiss() {
    if (TipDialog.instance !== null) {
      TipDialog.instance.closeDialog();
      TipDialog.instance = null;
    }
  }

  constructor(context: UIContext, option: TipDialogOption) {
    this.context = context;
    this.contentNode = new ComponentContent(context, wrapBuilder(defaultBuilder), option);
  }

  openDialog(canCancel: boolean) {
    if (this.context !== null && this.contentNode !== null) {
      this.context.getPromptAction().openCustomDialog(this.contentNode, {
        maskColor: '#CC000000',
        onWillDismiss: () => {
          return canCancel;
        }
      })
        .then(() => {
          console.info('OpenCustomDialog complete.');
        })
    }
  }

  closeDialog() {
    if (this.context !== null && this.contentNode !== null) {
      this.context.getPromptAction().closeCustomDialog(this.contentNode)
        .then(() => {
          console.info('CloseCustomDialog complete.');
        })
    }
  }

  updateDialog(options: TipDialogOption) {
    this.contentNode?.update(options)
  }
}