import ConfigM, { Config } from "../api/ConfigM";
import { Loading } from "../Loading";

import PogHttp, { PogLoginResp, PogSignMessageResp } from "../net/PogHttp";
import Utils from "../utils/Utils";
import { WalletListener, WalletManager } from "../web3/WalletManager";
import TgM from "./TgM";
import { Tips } from "./Tips";

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

  

  retryLogin() {
    WalletManager.ins.open();
  }

  async login(): Promise<PogLoginResp | null> {1    


    if (await TgM.ins.isTG()) {
      await Loading.ins.U("Telegram Login...", 0.3);
      return await TgM.ins.login();
    }

    setTimeout(async () => {
      let isConnected = await WalletManager.ins.isConnected();
      console.log("isConnected", isConnected);
      if (isConnected) {
        return;
      }
      Loading.ins.showLoginByWalletButton();
      WalletManager.ins.open();
    }, 10000);

    const walletLoginResult: PogLoginResp | null = await new Promise(
      (resolve) => {
        this.checkWalletConnect((res) => {
          resolve(res);
        });
      }
    );

    return walletLoginResult;
  }

  async beginSignMessage(address: string) {
    let signResult = await PogHttp.ins.getSignMessage(address);

    if (
      !signResult ||
      !signResult.fullMessage ||
      signResult.fullMessage.length <= 0
    ) {
      console.error("sign message error", "signResult is null");
      return;
    }

    let signature = await WalletManager.ins.signMessage(signResult.fullMessage);

    
    return signature;
  }

  async beginLogin(address: string): Promise<PogLoginResp | null> {
    if (!address) {
      return null;
    }

    


    if (ConfigM.ins.ignoreSignMessage) {
      return PogHttp.ins.loginByWalletAddress(address);
      //   .then(async (res: PogLoginResp) => {
      //     cb(res);
      //   });
    }

    let signature = await LoginM.ins.beginSignMessage(address);

    if (signature) {
      let loginResult = await PogHttp.ins.loginByWalletAddressAndSignature(
        address,
        signature
      );
      return loginResult;
    }
    return null;
  }

  async checkWalletConnect(cb: (res: PogLoginResp) => void) {
    let self = this;
    await Loading.ins.U("Connecting to wallet...", 0.3);
    let walletListener: WalletListener = {
      onLoginSuccess: async (address: string, balance: number) => {
        await Loading.ins.U("Wallet connected...", 0.4);

        await new Promise((resolve) => setTimeout(resolve, 30));
        await Loading.ins.U("Logging in...", 0.5);
        let loginResult: PogLoginResp  = await LoginM.ins.beginLogin(address);
        cb(loginResult);
        // PogHttp.ins
        //   .loginByWalletAddress(account)
        //   .then(async (res: PogLoginResp) => {
        //     cb(res);
        //   });
      },
      OnWalletDisconnected: () => {
        //
      },
    };
    WalletManager.ins.init(walletListener);
  }
  startWalletLogin() {
    WalletManager.ins.open();
  }
}
