import { _decorator, Component, Node } from "cc";
import BaseUI from "../../scripts/base/BaseUI";
import { InputNameLayer } from "./InputNameLayer";
import Utils from "../../scripts/utils/Utils";
import { TipsLayer } from "../layer/TipsLayer";
import AB from "../../scripts/base/AB";
import { instantiate } from "cc";
import EV, { EV_TYPE } from "../../scripts/mgr/EV";
import { Tips } from "../../scripts/mgr/Tips";
import UserM from "../../scripts/api/UserM";
import ShareM from "../../scripts/mgr/ShareM";
import FamilyM from "../../scripts/api/FamilyM";
import { EditBox } from "cc";
import { FamilyItem } from "./FamilyItem";
import { HelpImage, HelpType } from "../layer/HelpImage";
const { ccclass, property } = _decorator;

@ccclass("FamilyPageNo")
export class FamilyPageNo extends BaseUI {
  async createFamily() {
    let name = await InputNameLayer.show(10);
    console.log("name", name);

    if (name == null) {
      return;
    }
    let result = await FamilyM.ins.createFamily(name);
    if (!result) {
      // Tips.show("Create Family Failed");
      return;
    }

    await UserM.ins.refreshInfo();

    let title = "";
    let content = Utils.setI18nLabel("Family.CreateFamilySuccess", name);
    let familyOwner = UserM.ins.getUserName();
    let shareLink = await ShareM.ins.getFamilyShareLink(name, familyOwner);
    let success = await TipsLayer.forceConfirm(title, content, shareLink);

    EV.ins.emit(EV_TYPE.FAMILY_STATUS_UPDATE);
  }

  protected onLoad(): void {
    super.onLoad();
    this.staticUI();
  }
  staticUI() {
    this.setText(
      "lbl_family_tips_what_is",
      Utils.setI18nLabel("Family.WhatIsFamily")
    );
    this.setText(
      "lbl_family_tips_member",
      Utils.setI18nLabel("Family.FamilyMemberBenefits")
    );
    this.setText(
      "lbl_family_tips_owner",
      Utils.setI18nLabel("Family.FamilyOwnerBenefits")
    );
  }
  async searchFamily() {
    let keyword = this.FindAs("EditBoxSearchFamily", EditBox).string;
    if (keyword == "") {
      return;
    }
    let p = await AB.ins.loadPrefab("prefab/family/FamilyItem");
    let families_layout = this.FindNode("families_layout");
    families_layout.removeAllChildren();

    let familyList = await FamilyM.ins.searchFamily(keyword);
    if (!familyList || familyList.length <= 0) {
      Tips.show("No Family Found");
      return;
    }
    for (let index = 0; index < familyList.length; index++) {
      let item = instantiate(p);
      item.active = true;
      item.parent = families_layout;
      item.getComponent(FamilyItem).init(familyList[index]);
    }
    EV.ins.emit(EV_TYPE.FAMILY_SEARCH_RESULT);
  }

  protected simpleOnBtnClick(name: string): void {
    switch (name) {
      case "btn_search":
        this.searchFamily();
        break;
      case "btn_join_family":
        Tips.show("Coming Soon");
        break;
      case "btn_create_family":
        this.createFamily();
        break;
      case "btn_box_family_box":
        HelpImage.show(HelpType.FamilyBox);
        break;
    }
  }
}
