import { _decorator, Component, isValid, Node, Vec3 } from "cc";
import { ModuleDef } from "../../scripts/ModuleDef";
import { GameUILayers } from "../../scripts/GameUILayers";
import BaseUI from "../base/BaseUI";
import { SceneUtil } from "../../core_tgx/base/SceneUtils";
import { SceneDef } from "../../scripts/SceneDef";
import { BallUISoloMatch } from "./BallUISoloMatch";
import UIUtils from "../base/UIUtils";
import { Lobby } from "./Lobby";
const { ccclass, property } = _decorator;

@ccclass("ChooseMode")
export class ChooseMode extends BaseUI {
  static show() {
    tgx.UIMgr.inst.show(
      ModuleDef.BALL,
      "ui_lobby/ui_choose_mode",
      GameUILayers.POPUP
    );
  }

  protected onDestroy(): void {
    Lobby.ins.enter();
  }
  private _isLoading: boolean = false;
  protected closePage(): void {
    super.closePage();
    this._isLoading = false;
  }

  protected onLoad(): void {
    super.onLoad();

    let index = 0;
    let self = this;
    this.FindNode("content").children.forEach((child) => {
      self.playAnimation(child, index);
      index++;
    });
  }
  playAnimation(child: Node, index: number) {
    child.active = false;

    let moveY = 0;
    let moveX =0;
    child.setPosition(
      child.position.x + moveX,
      child.position.y + moveY,
      child.position.z
    );
    child.setScale(0.8, 0.8, 0.8);

    setTimeout(() => {
      if (!isValid(child)) {
        return;
      }
      child.active = true;

      UIUtils.AnimBezierMoveAndScale(
        child,
        new Vec3(
          child.position.x - moveX,
          child.position.y - moveY,
          child.position.z
        ),
        1,
        0.3,
        () => {}
      );
    }, index * 80);
  }

  protected simpleOnBtnClick(name: string): void {
    if (this._isLoading) {
      return;
    }
    this._isLoading = true;
    switch (name) {
      case "btn_close":
        this.closePage();
        break;

      case "btn_1":
        tgx.UIWaiting.show();
        SceneUtil.loadScene(SceneDef.GAME_LOCAL);
        setTimeout(() => {
          this.closePage();
          tgx.UIWaiting.hide();
        }, 2000);
        break;
      case "btn_2":
        tgx.UIMgr.inst.closeAll();
        BallUISoloMatch.show();
        this._isLoading = false;

        break;
    }
  }
}
