import ItemsM from "../mgr/ItemsM";
import { RewardGoods } from "../mgr/PushMsgM";
import WsM from "./WsM";

export class OwnerInfo {
  owner: boolean; // 当前用户是否是owener
  ownerId: string; // owner的id
  ownerName: string; // owner的名字
  rewardStatus: number; // 是否有奖励 0有1没有
  rewardPog: number; // 奖励的pog数量
}
export class FamilyInfo {
  familyId: string; // 家族id
  familyName: string; // 家族名族
  notice: string; // 家族公告
  share: string;
  dailyPog: string; // 家族今日pog
  seasonPassGame: number; // 家族赛季通行证
  xp: string; // 家族当前xp
  boxCount: number; // 宝箱数量
  totalSize: number; // 家族最大人数
  memberSize: number; // 家族当前人数
  ownerDto: OwnerInfo;
}

export class FamilyMember {
  familyId: string;
  userId: string;
  userName: string;
  totalXp: string;
  dailyXp: string;
}

export class FamilyJoinPlayer {
  id: string;
  createTime: string;
  updateTime: string;
  isDeleted: number;
  familyId: string;
  userId: string;
  reason: string;
  status: number;
}

export default class FamilyM {
  private static _ins: FamilyM;
  public static get ins(): FamilyM {
    return (FamilyM._ins ??= new FamilyM());
  }

  async getFamilyInfo(familyId: string): Promise<FamilyInfo> {
    let result = await WsM.ins.syncRequest(26009, { familyId: familyId });
    return result as FamilyInfo;
  }

  async familyMemberList(familyId: string): Promise<FamilyMember[]> {
    let result = await WsM.ins.syncRequest(26010, { familyId });
    return result.memberList as FamilyMember[];
  }

  async searchFamily(keyWords: string): Promise<FamilyInfo[]> {
    let result = await WsM.ins.syncRequest(26002, { keyWords });
    return result as FamilyInfo[];
  }

  async createFamily(familyName: string): Promise<FamilyInfo> {
    let result = await WsM.ins.syncRequest(26001, {
      familyName: familyName,
      notice: "",
    });
    return result as FamilyInfo;
  }

  async editFamilyNotice(familyId: string, notice: string): Promise<any> {
    let result = await WsM.ins.syncRequest(26003, {
      familyId,
      notice,
    });
    return result;
  }

  async requestJoinFamily(familyId: string, reason: string): Promise<any> {
    let result = await WsM.ins.syncRequest(26004, {
      familyId,
      reason,
    });
    return result;
  }

  async familyRequestList(familyId: string): Promise<FamilyJoinPlayer[]> {
    let result = await WsM.ins.syncRequest(26013, { familyId });
    if (!result || !result.joinList) {
      return [];
    }
    return result.joinList as FamilyJoinPlayer[];
  }

  async agreeJoinFamily(familyId: string, userId: string): Promise<any> {
    let result = await WsM.ins.syncRequest(26005, {
      familyId,
      userId,
    });
    return result;
  }

  async rejectJoinFamily(familyId: string, userId: string): Promise<any> {
    let result = await WsM.ins.syncRequest(26006, {
      familyId,
      userId,
    });
    return result;
  }

  async upgradeFamily(familyId: string): Promise<any> {
    let result = await WsM.ins.syncRequest(26007, { familyId });
    return result;
  }

  async kickFamilyMember(familyId: string, userId: string): Promise<any> {
    let result = await WsM.ins.syncRequest(26008, { familyId, userId });
    return result;
  }

  async claimFamilyRewardMemberBox(
    familyId: string,
    num: number
  ): Promise<RewardGoods> {
    let result: RewardGoods = await WsM.ins.syncRequest(26011, {
      familyId,
      num,
    });
    if (result) {
      ItemsM.ins.itemChange(result.changeList, result.goodList);
    }
    return result;
  }

  async claimFamilyRewardOwnerPog(familyId: string): Promise<RewardGoods> {
    let result = await WsM.ins.syncRequest(26012, { familyId });
    if (result) {
      ItemsM.ins.itemChange(result.changeList, result.goodList);
    }
    return result;
  }

  async leaveFamily(): Promise<any> {
    let result = await WsM.ins.syncRequest(26014, {});
    return result;
  }
}
