import { _decorator, Component, Node, instantiate } from "cc";
import {
  BindGameConfirmInfo,
  BindGameUserInfo,
} from "../../scripts/mgr/PushMsgM";
import { Hall } from "../hall/Hall";
import BaseUI from "../../scripts/base/BaseUI";
import AB from "../../scripts/base/AB";
import {
  LinkGameAccountItem,
  LinkGameAccountItemClick,
} from "./LinkGameAccountItem";
import { TipItem } from "../../scripts/mgr/TipItem";
import { Tips } from "../../scripts/mgr/Tips";
import GameM from "../../scripts/api/GameM";
import { Sprite } from "cc";
import ConfigM from "../../scripts/api/ConfigM";
const { ccclass, property } = _decorator;

@ccclass("LinkGameConfirmLayer")
export class LinkGameConfirmLayer
  extends BaseUI
  implements LinkGameAccountItemClick
{
  OnClick(user: BindGameUserInfo) {
    let result = GameM.ins.LinkGameConfirm(
      this._data.gameId,
      user.userId,
      user.userName,
      user.wallet
    );
    if (result) {
      Tips.show("Link game successful");
      this.closePage()
    }
  }
  static async show(data: BindGameConfirmInfo) {
    let layer = await Hall.ins.showLayer("prefab/play/LinkGameConfirmLayer");
    layer.getComponent(LinkGameConfirmLayer).init(data);
  }

  private _data: BindGameConfirmInfo;
  init(data: BindGameConfirmInfo) {
    this._data = data;
    this.refresh();
  }
  async refresh() {
    let c = ConfigM.ins.getGame(this._data.gameId);
    this.setText("lbl_game_name", c.gameName);

    let f = await AB.ins.loadSpriteFrame(
      "texture/play/icon_" + this._data.gameId
    );
    this.FindAs("game_icon", Sprite).spriteFrame = f;

    let layout = this.FindNode("account_layout");
    let prefab = await AB.ins.loadPrefab("prefab/play/LinkGameAccountItem");
    let list = this._data.userList;
    if (!list || list.length == 0) {
      this.FindNode("NoData").active = true;
      return;
    }
    this.FindNode("NoData").active = false;

    for (let i = 0; i < list.length; i++) {
      let user = list[i];
      let n = instantiate(prefab);
      layout.addChild(n);
      n.getComponent(LinkGameAccountItem).init(user, this);
    }
  }
  start() {}

  update(deltaTime: number) {}
}
