import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { Tips } from "../../scripts/mgr/Tips";
import FamilyM, { FamilyInfo } from "../../scripts/api/FamilyM";
import { InputNameLayer } from "./InputNameLayer";
import UserM from "../../scripts/api/UserM";
const { ccclass, property } = _decorator;

@ccclass("FamilyItem")
export class FamilyItem extends BaseUI {
  private _data: FamilyInfo;
  init(data: FamilyInfo) {
    this._data = data;
    this.setText("lbl_family_name", data.familyName);
    this.setText(
      "lbl_family_member_count",
      data.memberSize + "/" + data.totalSize
    );
    let ownerText = "Owner: " + data.ownerDto?.ownerName;
    this.setText("lbl_family_owner", ownerText);
  }

  protected simpleOnBtnClick(name: string): void {
    switch (name) {
      case "btn_join":
        this.requestJoinFamily();
        break;
    }
  }
  async requestJoinFamily() {
    let reason: string = await InputNameLayer.show(10);
    if (reason == null || reason.length <= 0) {
      return;
    }
    let familyId = this._data.familyId;
    if (familyId == null || familyId.length == 0) {
      return;
    }
    let result = await FamilyM.ins.requestJoinFamily(familyId, reason);

    if (result) {
      Tips.show("Join Request Success");
    }
  }
}
