import { _decorator, Component, Node, Event, Animation } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { Tips } from "../../scripts/mgr/Tips";
import { WalletManager } from "../../scripts/web3/WalletManager";
import Utils from "../../scripts/utils/Utils";
import { EM } from "../../scripts/event/EM";
import { E } from "../../scripts/event/E";
import { BindGameLayer } from "../layer/BindGameLayer";
import { SettingsLayer } from "../layer/SettingsLayer";
import { BadgeDetailLayer } from "../layer/BadgeDetailLayer";
import UserM from "../../scripts/api/UserM";
import { Label } from "cc";
import WalletM, { BadgeDto } from "../../scripts/api/WalletM";
import AB from "../../scripts/base/AB";
import { SpriteFrame } from "cc";
import { Sprite } from "cc";
const { ccclass, property } = _decorator;

@ccclass("WalletPageMain")
export class WalletPageMain extends BaseUI {
  protected onLoad(): void {
    super.onLoad();
    let self = this;
    self.setTitles();
  }
  setTitles() {
    this.setText("lbl_my_assets_title", Utils.setI18nLabel("Wallet.MyAssets"));
    this.setText("lbl_pog_tips", Utils.setI18nLabel("Wallet.POGDesc"));

    this.setText("lbl_my_address", Utils.setI18nLabel("Wallet.MyAddress"));
    this.setText("lbl_my_badges", Utils.setI18nLabel("Wallet.MyBadges"));
    this.setText("lbl_my_assets", Utils.setI18nLabel("Wallet.MyAssets"));
    this.setText("lbl_my_games", Utils.setI18nLabel("Wallet.MyGames"));
  }

  async init(isConnected: boolean) {
    if (!isConnected) {
      return;
    }

    // let walletInfo = this.FindNode("wallet_info");
    // walletInfo.active = true;

    this.setText("lbl_pog_balance", "" + UserM.ins.getPogDisplay());
    let account = WalletManager.ins.address;
    this.setText("lbl_user_wallet_address", Utils.formAddress(account));

    this.setText("lbl_tog_balance", "" + UserM.ins.getTogDisplay());

    let badge_rank_add_value = 0;
    let badge_game_pass_add_value = 0;
    let badge_rank_count = 0;
    let badge_game_pass_count = 0;
    this.setText("lbl_badge_count_1", badge_rank_count + "");
    this.setText("lbl_badge_count_2", badge_game_pass_count + "");

    this.FindNode("i_up1").getChildByName("value").getComponent(Label).string =
      badge_rank_add_value + "%";
    this.FindNode("i_up2").getChildByName("value").getComponent(Label).string =
      badge_game_pass_add_value + "%";

    await this.requestHomeData();
  }
  async requestHomeData() {
    let data = await WalletM.ins.walletHome();
    this.setText(
      "lbl_badge_count_1",
      this.getBadgeCount(data.badgeList, 1) + ""
    );
    this.setText(
      "lbl_badge_count_2",
      this.getBadgeCount(data.badgeList, 2) + ""
    );
    let addValue1 = this.getBadgeAddValue(data.badgeList, 1);
    this.FindNode("i_up1").active = addValue1 > 0;
    if (addValue1 > 0) {
      this.FindNode("i_up1")
        .getChildByName("value")
        .getComponent(Label).string = Utils.formatNumber(addValue1, 1) + "%";
    }

    let addValue2 = this.getBadgeAddValue(data.badgeList, 2);
    this.FindNode("i_up2").active = addValue2 > 0;

    if (addValue2 > 0) {
      this.FindNode("i_up2")
        .getChildByName("value")
        .getComponent(Label).string = Utils.formatNumber(addValue2, 1) + "%";
    }

    let topRankBadgeId = this.getTopRankBadgeId(data.badgeList);
    console.warn("topRankBadgeId", topRankBadgeId);
    if (topRankBadgeId <= 0) {
      topRankBadgeId = 7;
    }

    let a = await AB.ins.loadSpriteFrame(
      "texture/common/grade" + topRankBadgeId + ""
    );
    let f = a as SpriteFrame;
    this.FindAs("icon_rank_grade", Sprite).spriteFrame = f;
  }

  private getBadgeCount(badgeList: BadgeDto[], type: number) {
    let count = 0;
    if (!badgeList) {
      return count;
    }
    for (let badge of badgeList) {
      if (badge.type == type) {
        count += badge.num;
      }
    }
    return count;
  }

  getTopRankBadgeId(badgeList: BadgeDto[]) {
    let topBadgeId = 0;
    if (!badgeList) {
      return topBadgeId;
    }
    let curValue = 0;
    for (let badge of badgeList) {
      console.warn("badge", badge.id, badge.addition);
      if (badge.type == 1) {
        if (Number(badge.addition) >= curValue) {
          curValue = Number(badge.addition);
          topBadgeId = badge.id;
        }
      }
    }
    return topBadgeId;
  }

  private getBadgeAddValue(badgeList: BadgeDto[], type: number) {
    let value = 0;
    if (!badgeList) {
      return value;
    }
    for (let badge of badgeList) {
      if (badge.type == type) {
        value += Number(badge.addition);
      }
    }
    return value;
  }

  private disConnect() {
    WalletManager.ins.disconnect();
    EM.ins.emit(E.Wallet.DISCONNECTED);
  }
  protected onBtnClick(name: string, event: Event, customEventData: any): void {
    switch (name) {
      case "btn_history":
        Tips.show("POG History Coming Soon");
        break;

      case "btn_copy_address":
        //

        Utils.copyText(WalletManager.ins.address);
        Tips.show("Address Copied");

        this.tryOpenConsole();

        break;

      case "btn_setting":
        SettingsLayer.show();
        break;

      case "btn_add_game":
        BindGameLayer.show();
        break;
      case "btn_badge_rank":
        BadgeDetailLayer.show(BadgeDetailLayer.TYPE_RANK);
        break;
      case "btn_badge_game_pass":
        BadgeDetailLayer.show(BadgeDetailLayer.TYPE_GAME_PASS);
        break;
    }
  }

  private clickTimes: number = 0;
  tryOpenConsole() {
    this.clickTimes++;
    if (this.clickTimes >= 10) {
      this.clickTimes = 0;
      new window["VConsole"]();
    }
    let self = this;

    setTimeout(() => {
      self.clickTimes = 0;
    }, 3000);
  }
}
