import { _decorator, Node, Event } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { Hall } from "../hall/Hall";
import { RichText } from "cc";
import { Label } from "cc";
import PayM from "../../scripts/mgr/PayM";
import ShareM from "../../scripts/mgr/ShareM";
import ConfigM from "../../scripts/api/ConfigM";
const { ccclass, property } = _decorator;

export class TipsData {
  forceConfirm: boolean = false;
  title: string;
  content: string;
  onConfirm: () => void = null;
  onCancel: () => void = null;
  singleName: string = "";
  link: string = null;
  shareLink: string = null;

  constructor(content: string) {
    this.content = content;
  }
  async show(): Promise<TipsLayer> {
    return await TipsLayer.show(this);
  }
}

export interface OnConfirm {
  (): void;
}

@ccclass("TipsLayer")
export class TipsLayer extends BaseUI {
  static build(content: string): TipsData {
    let tipsData: TipsData = new TipsData(content);
tipsData.title=""
    return tipsData;
  }

  static forceConfirm(
    title: string,
    content: string,
    shareLink: string = null
  ): Promise<boolean> {
    if (Hall.ins==null) {
      return Promise.resolve(false);
    }
    return new Promise((resolve, reject) => {
      let tipsData: TipsData = TipsLayer.build(content);
      tipsData.title = title;
      tipsData.shareLink = shareLink;
      tipsData.forceConfirm = true;
      tipsData.onConfirm = () => {
        resolve(true);
      };
      tipsData.onCancel = () => {
        resolve(false);
      };
      TipsLayer.show(tipsData);
    });
  }
  private static single: Map<string, Node> = new Map();
  static async show(data: TipsData): Promise<TipsLayer> {
    if (Hall.ins == null) {
      return null;
    }
    if (data.singleName && data.singleName != "") {
      if (TipsLayer.single.has(data.singleName)) {
        return TipsLayer.single.get(data.singleName).getComponent(TipsLayer);
      }
    }

    let layer = await Hall.ins.showLayer("prefab/layer/TipsLayer");
    let comp = layer.getComponent(TipsLayer);
    comp.init(data);
    if (data.singleName && data.singleName != "") {
      TipsLayer.single.set(data.singleName, layer);
    }
    return comp;
  }

  static async showConfirm(title: string, content: string): Promise<boolean> {
    if (Hall.ins == null) {
      return Promise.resolve(false);
    }
    return new Promise((resolve, reject) => {
      let tipsData: TipsData = TipsLayer.build(content);
      tipsData.title = title;
      tipsData.onConfirm = () => {
        resolve(true);
      };
      tipsData.onCancel = () => {
        resolve(false);
      };
      TipsLayer.show(tipsData);
    });
  }

  private data: TipsData;
  init(data: TipsData) {
    this.data = data;
    let self = this;
    if (data.title == null || data.title == "") {
      this.FindNode("lbl_title").active = false;
    } else {
      this.setText("lbl_title", data.title);
    }

    this.FindAs("lbl_content", RichText).string = data.content;

    if (data.forceConfirm) {
      setTimeout(() => {
        self.FindNode("tap_close").active = false;
        self.FindNode("btn_close").active = false;
        self.FindNode("btn_close_button").active = false;
      }, 500);
    }

    this.FindNode("btn_share").active = data.shareLink && data.shareLink != "";

    this.FindNode("btn_link").active = data.link != null;
    this.FindNode("btn_link").getComponent(Label).string = data.link;
    // this.setText("btn_link", data.link);
  }
  protected onLoad(): void {
    super.onLoad();
  }

  public onBtnClick(name: string, event: Event, customEventData: any): void {
    console.log(
      `111 onBtnClick name == "btn_close_button" || name == "btn_close"`
    );
    console.error(name);
    if (name == "btn_close_button" || name == "btn_close") {
      this.data?.onCancel?.();
      this.closePage();
    }
    if (name == "btn_confirm") {
      this.data?.onConfirm?.();
      this.closePage();
    }

    switch (name) {
      case "btn_share":
        ShareM.ins.share(this.data.shareLink);
        break;
    }
  }
  protected onDestroy(): void {
    if (this.data?.singleName) {
      TipsLayer.single.delete(this.data.singleName);
    }
  }

  protected simpleOnBtnClick(name: string): void {
    if (name == "btn_link") {
      let url = this.FindNode("btn_link").getComponent(Label).string;

      ConfigM.ins.openLink(url);
    }
  }
}
