import {
  _decorator,
  Component,
  Director,
  director,
  Node,
  UITransform,
} from "cc";
import { CharacterType, GamerNode } from "./UINode/GamerNode";
import { BaseSprite } from "../GlobalScript/CommonComponent/BaseSprite";
import { NpkImage } from "../Tool/NPKImage";
import { AnimationNode } from "../GlobalScript/Animation/AnimationNode";
import { BaseButtonState } from "../GlobalScript/CommonComponent/BaseButton";
import { DiceButtonNode } from "./UINode/DiceButtonNode";
import { OtherWinNode, otherWinType } from "./UINode/OtherWinNode";
import { BoardRoot } from "./BoardRoot";
import { StartGameUINode } from "./StartGameNode/StartGameUINode";
import { SelectNumberNode } from "./DialogNode/SelectNumberNode";
import { GameRootSingleton } from "./GameRootController";
const { ccclass } = _decorator;

@ccclass("UIRoot")
export class UIRoot extends Component {
  /// 等待玩家
  private awaitGamerNode: Node;
  /// 玩家一
  private gamerOne: GamerNode;
  /// 玩家二
  private gamerTwo: GamerNode;
  /// 玩家三
  private gamerThree: GamerNode;
  /// 投骰子按钮
  private diceButton: DiceButtonNode;
  /// 其他胜利条件
  private otherWinNode: OtherWinNode;

  //* 初始化开始游戏界面的UI
  initStartGameUI() {
    const startGameUINode = new StartGameUINode();
    startGameUINode.addComponent(UITransform).setContentSize(1067, 600);
    this.node.addChild(startGameUINode);
  }

  //* 初始化游戏界面UI
  initGameUI() {
    /// 玩家UI
    this.initGamerUI();
    /// 右下骰子操作按钮
    this.initDiceButton();
    /// 等待玩家加载
    this.initAwaitGamerUI();
    /// 其他胜利条件
    this.initOtherWinNode();
  }

  //* 初始化玩家UI
  initGamerUI() {
    this.gamerOne = new GamerNode("玩家一");
    this.gamerOne.setPosition(0, -540);
    this.gamerOne.charchterType = CharacterType.JianHun;
    this.node.addChild(this.gamerOne);

    this.gamerTwo = new GamerNode("");
    this.gamerTwo.setPosition(877, 0);
    this.node.addChild(this.gamerTwo);

    this.gamerThree = new GamerNode("");
    this.node.addChild(this.gamerThree);

    this.initDiceButton();
  }

  //* 初始化等待玩家加载UI
  initAwaitGamerUI() {
    const node = new Node();
    node.setPosition(177.5, -244.5);
    this.node.addChild(node);

    const spr = node.addComponent(BaseSprite);
    spr.updateSpriteFrame(NpkImage.loading_loop, 0);

    const ani = new AnimationNode("ani/loading_loop01.ani");
    ani.setPosition(350, -110);
    node.addChild(ani);

    this.awaitGamerNode = node;


    
    //todo 自动跳过，接入网络后修改
    // 1秒后 结束加载
    setTimeout(() => {
      /// 这一帧结束之后销毁
      director.once(Director.EVENT_END_FRAME, () => {
        // 销毁
        this.awaitGamerNode.destroy();

        // todo 开发时跳过倒计时动画
        /// 初始化开始倒计时动画
        // this.initCountFontAni(5);
      });

      // todo 开发时跳过倒计时动画
      this.aniDone();
    }, 1000);
  }

  //* 初始化倒计时动画
  initCountFontAni(index: number) {
    const ani = new AnimationNode('ani/count_font.ani',()=>{
      /// 这一帧结束之后销毁
      director.once(Director.EVENT_END_FRAME, () => {
        // 销毁
        ani.destroy();
      });
      /// 动画结束
        this.aniDone();
    })
    ani.setPosition(500, -200);

    this.node.addChild(ani);
  }

  //* 初始化投骰子按钮
  initDiceButton() {
    this.diceButton = new DiceButtonNode();
    this.diceButton.setPosition(849, -453);
    this.node.addChild(this.diceButton);

    /// 显示其他胜利条件
    this.diceButton.winButtonBlock = () => {
      this.otherWinNode.active = true;
    };
  }

  //* 初始化其他胜利条件
  initOtherWinNode() {
    this.otherWinNode = new OtherWinNode(otherWinType.longRun);
    this.node.addChild(this.otherWinNode);

    this.otherWinNode.active = false;
  }

  //* 动画结束 显示选择顺序
  aniDone() {
    /// 恢复其他胜利按钮的状态
    this.diceButton.winButtonComponent.ButtonState = BaseButtonState.Normal;
    /// 显示其他胜利条件
    this.otherWinNode.active = true;
    const boardNode = this.node.parent.getChildByName("BoardRoot");
    /// 显示最底层的地图块
    const board = boardNode.getComponent(BoardRoot);
    board.initMapTile();

    //*  1秒后自动关闭胜利条件显示,并显示顺序选择
    setTimeout(() => {

      //* 显示顺序选择node
      this.otherWinNode.active = false;
      
      GameRootSingleton.getInstance().DialogRoot.initSelectNumberNode();
    }, 1000);
  }

  start() {
    this.node.addComponent(UITransform).setContentSize(1067, 600);
    this.initStartGameUI();
  }

  update(deltaTime: number) {}
}
