import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { Tips } from "../../scripts/mgr/Tips";
import { TipsData, TipsLayer } from "../layer/TipsLayer";
import Utils from "../../scripts/utils/Utils";
import FamilyM, { FamilyInfo } from "../../scripts/api/FamilyM";
import UserM from "../../scripts/api/UserM";
import EV, { EV_TYPE } from "../../scripts/mgr/EV";
const { ccclass, property } = _decorator;

export enum ManagePlayerItemType {
  APPROVE = 0,
  MEMBERS = 1,
}

export class ManagePlayerItemData {
  type: ManagePlayerItemType;
  userId: string;
  userName: string;
  reason: string;
  status: number;
  totalXp: number;
  dailyXp: number;
}
@ccclass("ManagePlayerItem")
export class ManagePlayerItem extends BaseUI {
  private type: ManagePlayerItemType = null!;
  private _familyInfo: FamilyInfo;
  private _data: ManagePlayerItemData;
  init(
    type: ManagePlayerItemType,
    data: ManagePlayerItemData,
    familyInfo: FamilyInfo
  ) {
    this.type = type;
    this._familyInfo = familyInfo;
    this._data = data;
    this.setText("lbl_user_name", "" + data.userName);
    this.initType(type);

    this.initUI(data);
  }
  initUI(data: ManagePlayerItemData) {
    if (this.type == ManagePlayerItemType.APPROVE) {
      let reason = data.reason;
      this.setText("lbl_approve_info", reason);
    } else if (this.type == ManagePlayerItemType.MEMBERS) {
      let totalXp = data.totalXp;
      if (totalXp < data.dailyXp) {
        totalXp = data.dailyXp;
      }
      this.setText(
        "lbl_exp_value",
        "today:" +
          Utils.formatNumber(data.dailyXp, 1) +
          " total:" +
          Utils.formatNumber(totalXp, 1)
      );
    }
  }
  initType(type: ManagePlayerItemType) {
    if (type == ManagePlayerItemType.APPROVE) {
      this.setText("lbl_manager_name", "" + "Approve");
    } else if (type == ManagePlayerItemType.MEMBERS) {
      this.setText("lbl_manager_name", "" + "Remove");
      if (this.isOwner()) {
        this.FindNode("btn_manage_one").active = false;
      }
    }

    this.FindNode("exp").active = type == ManagePlayerItemType.MEMBERS;

    this.FindNode("lbl_approve_info").active =
      type == ManagePlayerItemType.APPROVE;
  }

  protected simpleOnBtnClick(name: string): void {
    switch (name) {
      case "btn_manage_one":
        if (this.type == ManagePlayerItemType.APPROVE) {
          // FamilyM.ins.agreeJoinFamily(UserM.ins.getFamilyId(), this.data);
          this.agreeJoinFamily();
        } else if (this.type == ManagePlayerItemType.MEMBERS) {
          this.remove();
        }

        break;
    }
  }
  async agreeJoinFamily() {
    let result = await FamilyM.ins.agreeJoinFamily(
      this._familyInfo.familyId,
      this._data.userId
    );
    if (result) {
      Tips.show("Agree join success");
      EV.ins.emit(EV_TYPE.FAMILY_MEMBER_UPDATE);
      this.node.destroy();
    }
  }

  private isOwner(): boolean {
    return this._familyInfo.ownerDto?.ownerId == this._data?.userId;
  }
  async remove() {
    let self = this;

    if (this.isOwner()) {
      return;
    }

    TipsLayer.showConfirm(
      "",
      Utils.setI18nLabel("Family.RemoveConfirmContent")
    ).then((success) => {
      if (success) {
        FamilyM.ins
          .kickFamilyMember(this._familyInfo.familyId, this._data.userId)
          .then((result) => {
            if (result) {
              Tips.show(Utils.setI18nLabel("Family.RemoveMemberSuccess"));
              EV.ins.emit(EV_TYPE.FAMILY_MEMBER_UPDATE);
              self.node.destroy();
            }
          });
      }
    });
  }
}
