export default class ADManager {
  private static _ins: ADManager = null;
  public static get ins(): ADManager {
    return ADManager._ins ??= new ADManager();
  }

  /* ... */
  // view AD steps is [1,2,3,5,6,7], click AD steps is [1,2,3,6,7,5,4]
  callbackFunc = {
    // Indicates load ad resource from OpenAD platform, false will be returned if there is no resource to be loaded for the publisher slot/zone
    onAdResourceLoad: (e) => {
      // 'step1', e = true / false
    },
    // Indicates the interactive ad is opening
    onAdOpening: (e) => {
      // 'step2', e = true / false
      console.log("onAdOpening:", e);
    },
    // Indicates the interactive ad is opened,
    onAdOpened: (e) => {
      // 'step3',  e = true / false
      console.log("onAdOpened:", e);
    },
    // indicates the interactive ad task is finished, the task is defined by publisher
    onAdTaskFinished: (e) => {
      // 'step5',  e = true / false
      console.log("onAdTaskFinished:", e);
    },
    // Indicates the interactive ad is closing
    onAdClosing: (e) => {
      // 'step6',e = true / false
      console.log("onAdClosing:", e);
    },
    // Indicates the interactive ad is closed
    onAdClosed: (e) => {
      // 'step7', e = view / click / close
      // close: user manually closed ads. client side can not get any rewards.
      if (e === "close") {
        // do sth ...
      }
      // view: viewed Ad completed, not clicked, not manually closed ads; client side needs to get rewards level 1.
      if (e === "view") {
        // do sth ...
      }
      // click: clicked Ad completed, include viewed Ad, not manually closed ads; client side needs to get rewards level 2.
      // If your app is 'WEB3' or 'WEB', can add codes below; 'LMA' or 'LWA' can not write codes here.
      if (e === "click") {
        // do sth ...
      }

      console.log("onAdClosed:", e);
    },
    // Indicates clicked and jumps
    onAdClick: (e) => {
      // 'step4', e = true / false
      console.log("onAdClick:", e);
    },
  };

  clickReward = async () => {
    // maybe this is a ajax, maybe a ws, or somthing. async mode.
    // let res = await getRewardsLevel2Method;
    console.log("点击广告的clickReward");
    if (this.getSuccess) {
      this.getSuccess.call(null);
    }
  };

  getSuccess: Function;

  // 展示Google广告
  public showGoogleAD(callBack: Function) {
    this.getSuccess = callBack;
    console.log("showGoogleAD:", callBack);

    //@ts-ignore
    if (!window.googletag) {
      console.log("googletag not init");
      return;
    }

    //@ts-ignore
    const googletag = window.googletag || { cmd: [] };

    var that = this;
    googletag.cmd.push(() => {
      console.log(
        "googletag.enums.OutOfPageFormat.REWARDED:",
        googletag.enums.OutOfPageFormat.REWARDED
      );
      const rewardedSlot = googletag
        .defineOutOfPageSlot(
          "/23296944437/rewarded",
          googletag.enums.OutOfPageFormat.REWARDED
        )
        .addService(googletag.pubads());
      console.log("rewardedSlot:", rewardedSlot);
      googletag.enableServices();
      googletag.pubads().addEventListener("rewardedSlotReady", function (evt) {
        // if (confirm("want to see a rewarded ad?")) {
        var result = evt.makeRewardedVisible();
        console.log("makeRewardedVisible return:", result);
      });

      googletag
        .pubads()
        .addEventListener("rewardedSlotGranted", function (evt) {
          console.error("granted123! " + JSON.stringify(evt.payload));
          // {type:"",amount:0}
          if (that.getSuccess) {
            that.getSuccess.call(evt.payload);
          }
        });

      googletag.pubads().addEventListener("rewardedSlotClosed", function (evt) {
        console.error("Closed by the user!");
        googletag.destroySlots([rewardedSlot]);
      });
      googletag.display(rewardedSlot);
      // googletag.display("div-gpt-ad-1747208248493-0");
    });
  }
  // 展示Line广告
  //@ts-ignore
  public showInterLineAD(callBack: Function) {
    this.getSuccess = callBack;
    console.log("showInterLineAD:", callBack);

    /* ... */
    const LineAD = {
      adInfo: {
        zoneId: 430, // int, This is an example zoneId, please get your own code parameters
        publisherId: 15, // int, This is an example publisherId, please get your own code parameters
      },
      adParams: {
        line: {
          type: "LMA", // LMA, LWA
          liffId: "2007001592-ozOgMXpb", // when type = LMA / LWA, liffId is required
          //@ts-ignore
          prototype: window.liff, // when type = LMA / LWA, prototype is required. line.prototype must contain two methods: getProfile and openWindow, whether you are using liff SDK js or npm installation package;
        },
        wallet: {
          // If you have a web3 wallet components, Optional
          type: "eth", // eth: eth wallet, kaia: line wallet, ton: ton wallet;
          provider: null, // here is a provider object after wallet initialization.
          components: "", // web3 wallet components name
        },
      },
    };

    /* ... */
    //@ts-ignore
    window.OpenADLineJsSDK.interactive.init({ ...LineAD }).then((res) => {
      console.log("window.OpenADLineJsSDK.interactive:", res);
      if (res.code === 0) {
        // you can callback 'getRender' function, user can load an interactive ad;
        //@ts-ignore
        window.OpenADLineJsSDK.interactive.getRender({
          adInfo: LineAD.adInfo,
          cb: this.callbackFunc,
          clickReward: this.clickReward,
        });
      } else {
        console.error("no ad!");
        // you can't callback 'getRender' function, user can't load an interactive ad;
      }
    });
  }

  /* ... */
}
