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

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

  protected onLoad(): void {
    super.onLoad();
    this.initResult();
  }
  private _rankMap = {};

  async initResult() {
    let list = [];

    list.push({
      name: "player_1",
      score: 9.9,
      rank: 1,
      isSelf: false,
      kill: 87,
    });
    list.push({
      name: "player_2",
      score: 8.2,
      rank: 2,
      isSelf: true,
      kill: 66,
    });
    list.push({
      name: "player_3",
      score: 6.7,
      rank: 3,
      isSelf: false,
      kill: 55,
    });
    list.push({
      name: "player_4",
      score: 4.1,
      rank: 4,
      isSelf: false,
      kill: 34,
    });

    for (let index = 5; index <= 9; index++) {
      list.push({
        name: "player_" + index,
        score: 2 - index / 10,
        rank: index,
        isSelf: false,
        kill: 9 - index,
      });
    }

    for (let i in list) {
      this._rankMap[list[i].rank] = list[i];
    }

    for (let index = 1; index <= 9; index++) {
      this.FindNode("no" + index).active = false;
    }

    for (let index = 0; index < list.length; index++) {
      const element = list[index];

      let node = this.FindNode("no" + element.rank);
      if (node) {
        if (node.getComponent(UIGameResultPlayerItem) == null) {
          node.addComponent(UIGameResultPlayerItem);
        }
        node.getComponent(UIGameResultPlayerItem).init(element);
      }
    }

    for (let index = 1; index <= 9; index++) {
      if (this._rankMap[index] == null) {
        break;
      }

      if (index <= 3) {
        this.playTopAnim(index);
      } else if (index <= 6) {
        this.playLeftAnim(index);
      } else {
        this.playRightAnim(index);
      }
    }
  }

  playTopAnim(index: number) {
    let self = this;
    setTimeout(() => {
      let animNode = self.FindNode("no" + index);
      let moveDis = 200;
      animNode.setPosition(animNode.position.x, animNode.position.y - moveDis);
      animNode.active = true;
      UIUtils.AnimBezierMoveAndScale(
        animNode,
        new Vec3(animNode.position.x, animNode.position.y + moveDis, 0),
        1,
        0.3
      );
    }, index * 200);

    setTimeout(() => {
      let aaa = self.FindNode("circle_anim");
      UIUtils.AnimBezierMoveAndScale(aaa, new Vec3(0, 0, 0), 1.3, 1, () => {
        UIUtils.AnimBezierMoveAndScale(aaa, new Vec3(0, 0, 0), 1, 0.5);
      });
    }, 0);
  }
  playLeftAnim(index: number) {
    let self = this;
    setTimeout(() => {
      self.FindNode("no" + index).active = true;
      let animNode = self.FindNode("no" + index);
      let moveDis = 200;
      animNode.setPosition(animNode.position.x - moveDis, animNode.position.y);
      animNode.active = true;
      tween(animNode)
        .to(0.5, {
          position: new Vec3(
            animNode.position.x + moveDis,
            animNode.position.y
          ),
        })
        .start();
    }, 700 + index * 100);
  }
  playRightAnim(index: number) {
    let self = this;
    setTimeout(() => {
      let animNode = self.FindNode("no" + index);
      let moveDis = 200;
      animNode.setPosition(animNode.position.x + moveDis, animNode.position.y);
      animNode.active = true;
      tween(animNode)
        .to(0.5, {
          position: new Vec3(
            animNode.position.x - moveDis,
            animNode.position.y
          ),
        })
        .start();
    }, 800 + index * 100);
  }

  protected simpleOnBtnClick(name: string): void {
    if (name == "btn_back") {
      this.closePage();
      tgx.UIMgr.inst.closeAll();
      SceneUtil.loadScene(SceneDef.LOBBY);
    }
  }
}
