import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { EM } from "../../scripts/event/EM";
import { E } from "../../scripts/event/E";
import { WalletManager } from "../../scripts/web3/WalletManager";
import { CommonTabsTitle, OnTabClickListener } from "../common/CommonTabsTitle";
import { WalletPageMain } from "../wallet/WalletPageMain";
import { PlatformManager } from "../../scripts/mgr/PlatformManager";
import TgM from "../../scripts/mgr/TgM";
import UserM from "../../scripts/api/UserM";
import { Event } from "cc";
import { ScrollView } from "cc";
import { Widget } from "cc";
import { Layout } from "cc";

const { ccclass, property } = _decorator;

@ccclass("PageWallet")
export class PageWallet extends BaseUI implements OnTabClickListener {
  private static readonly TAB_MAIN = 0;
  private static readonly TAB_POG = 1;
  private static readonly TAB_TOG = 2;
  private static readonly TAB_ESTOG = 3;

  private tabs: CommonTabsTitle;

  private pageMain: Node;
  private pagePog: Node;
  private pageTog: Node;
  private pageEstog: Node;

  onTabClick(index: number) {
    this.toPage(index);
  }

  private scrollView: ScrollView;

  private pages: Node;
  protected onLoad(): void {
    super.onLoad();
    this.pageMain = this.FindNode("page_main");
    this.pagePog = this.FindNode("page_pog");
    this.pageTog = this.FindNode("page_tog");
    this.pageEstog = this.FindNode("page_estog");

    this.pageMain.active = false;
    this.pagePog.active = false;
    this.pageTog.active = false;
    this.pageEstog.active = false;

    this.tabs = this.getComponentInChildren(CommonTabsTitle);
    this.tabs.init(["WALLET", "POG", "TOG"], this);
    // this.tabs.init(["WALLET", "POG", "TOG", "esTOG"], this);

    this.scrollView = this.FindAs("ScrollView", ScrollView);

    this.pages = this.FindNode("pages");
    this.pages.parent = this.scrollView.content;
    this.pages.setPosition(0, 0, 0);

    this.FindNode("btn_to_wallet_connect").active = false;
    this.FindNode("connected_content").active = false;
    let self = this;

    self.initWallet();
  }

  async initWallet() {
    let self = this;
    // let isTg = await TgM.ins.isTG();
    // if (!isTg) {
    //   return;
    // }
    WalletManager.ins.init({
      onLoginSuccess: (address: string, balance: number) => {
        self.bindWallet(address).then((result) => {
          self.init(result);
        });
      },
      OnWalletDisconnected: () => {
        self.init(false);
      },
    });

    EM.ins.on(E.Wallet.DISCONNECTED, this.onWalletDisconnected, this);
  }

  async bindWallet(address: string): Promise<boolean> {
    let isTg = TgM.ins.isTG();
    if (!isTg) {
      // 非tg平台，直接返回true
      return true;
    }

    if (UserM.ins.isBindWallet()) {
      return true;
    }

    console.log("bindWallet", address, "isTg", isTg);

    let result = await UserM.ins.bindWallet(address);
    if (result) {
      // 绑定成功，返回true
      return true;
    }
    return false;
  }

  protected onDestroy(): void {
    EM.ins.off(E.Wallet.DISCONNECTED, this.onWalletDisconnected, this);
  }

  private onWalletDisconnected() {
    console.error("onWalletDisconnected");
    this.init(false);
  }

  async init(isConnected: boolean) {
    this.FindNode("connected_content").active = isConnected;
    this.FindNode("btn_to_wallet_connect").active = !isConnected;

    if (isConnected) {
      this.toPage(PageWallet.TAB_MAIN);
      this.pageMain.getComponent(WalletPageMain).init(isConnected);
    }
  }
  toPage(index: number) {
    let page = null;
    switch (index) {
      case PageWallet.TAB_MAIN:
        page = this.pageMain;
        break;
      case PageWallet.TAB_POG:
        page = this.pagePog;
        break;
      case PageWallet.TAB_TOG:
        page = this.pageTog;
        break;
      case PageWallet.TAB_ESTOG:
        page = this.pageEstog;
        break;
    }

    this.FindNode("pages").children.forEach((child) => {
      if (child.active) {
        child.getComponent(BaseUI).onHide();
        child.active = false;
      }
    });

    if (page) {
      page.active = true;

      page.getComponent(BaseUI).onShow();
    }

    this.tabs.setSelect(index);
  }

  public onShow(): void {
    super.onShow();

    WalletManager.ins.isConnected().then((isConnected) => {
      this.init(isConnected);
    });
  }

  public onHide(): void {
    super.onHide();
  }

  protected simpleOnBtnClick(name: string): void {
    switch (name) {
      case "btn_to_wallet_connect":
        WalletManager.ins.open();
        break;
    }
  }
}
