import { init, miniApp, openTelegramLink } from "@telegram-apps/sdk";
import PogHttp, { PogLoginResp } from "../net/PogHttp";
import Utils from "../utils/Utils";
import { Tips } from "./Tips";
import { isTMA } from "@telegram-apps/bridge";

import { retrieveLaunchParams, retrieveRawInitData } from "@telegram-apps/sdk";
import WsM from "../api/WsM";

export default class TgM {
  private _shareText: string = `POG ,the powerful platform,has surprise airdrops! 🎁`;

  async login(): Promise<PogLoginResp | null> {
    // if (Utils.getQueryString("tg") == "1") {
    //   return await PogHttp.ins.loginByTgParams(
    //     Utils.getQueryString("initDataRaw"),
    //     Utils.getQueryString("tgWebAppStartParam")
    //   );
    // }

    let launchParams = retrieveLaunchParams();
    console.warn("tg launchParams from sdk is :", launchParams);
    if (!launchParams) {
      return null;
    }

    let initDataRaw = retrieveRawInitData();
    console.warn("tg initDataRaw from sdk is :", initDataRaw);

    if (!initDataRaw) {
      return null;
    }

    let tgWebAppStartParam: string = Utils.getQueryString("tgWebAppStartParam");
    console.log("tg tgWebAppStartParam is :", tgWebAppStartParam);
    let result: PogLoginResp = await PogHttp.ins.loginByTgParams(
      initDataRaw,
      tgWebAppStartParam
    );

    return result;
  }
  private static _ins: TgM = null!;

  public static get ins(): TgM {
    return (this._ins ??= new TgM());
  }

  public async init(): Promise<boolean> {
    let tgInitResult = init();

    return true;
  }

  public async isTG(): Promise<boolean> {
    // if (Utils.getQueryString("tg") == "1") {
    //   return true;
    // }

    let isTG = isTMA();
    if (isTG) {
      await this.init();
      console.warn("It's Telegram Mini Apps");
      return true;
    } else {
      console.warn("It's not Telegram Mini Apps");
    }

    return false;
  }

  public async shareToTg(shareText: string): Promise<boolean> {
    shareText = shareText ? shareText : this._shareText;
    let shareLink = "";
    // shareLink=Data.user._shareLink
    try {
      let tgUrl = `https://t.me/share/url?url=${encodeURIComponent(
        shareLink
      )}&text=${encodeURIComponent(shareText)}`;
      this.openTelegramLink(tgUrl);
      return true;
    } catch (error) {
      Tips.show("share failed");
      return false;
    }
  }

  public openTelegramLink(url: string, isClose: boolean = false): void {
    try {
      if (Utils.isNameLink(url, "t.me")) {
        // window["Telegram"].WebApp.openTelegramLink(url);
        if (openTelegramLink.isAvailable()) {
          console.warn("openTelegramLink is available"+url);
          openTelegramLink(url);
        } else {
          window.open(url, "_blank");
        }

        // if (isClose) {
        //   setTimeout(() => {
        //     window["Telegram"].WebApp.close();
        //   }, 1500);
        // }
      } else {
        window.open(url, "_blank");
      }
    } catch (error) {
      window.open(url, "_blank");
    }
  }
}
