import { LevelBase } from "@/pages/GamePage/Components/LevelBase.ts";
import { Assets, Sprite } from "pixi.js";
import { Ease, Tween } from "@/core/tween";
import { GameEvent, globalEvent } from "@/pages/GamePage/GameEvent.ts";
import { rightWaitTime } from "@/pages/GamePage/Level/LevelConfig.ts";

export class Level19 extends LevelBase {

  level: number = 19;

  p1: Sprite;
  p2: Sprite;

  A: Sprite;

  right: Sprite;
  error: Sprite;

  // 双指缩放相关属性
  private initialDistance: number = 0;
  private initialScale: number = 1;
  private isTouching: boolean = false;
  private touchPoints: Map<number, { x: number, y: number }> = new Map();

  onLoad() {
    super.onLoad();

    this.addChild(new Sprite(Assets.get(`level${this.level}/img.png`)))
      .position.set(0, 868);

    this.A = this.addChild(new Sprite(Assets.get(`level${this.level}/船.png`)));
    this.A.anchor.set(0.4, 0.4);
    this.A.position.set(430, 1100);
    this.A.scale.set(0.5948827292110874);

    this.p1 = this.addChild(new Sprite(Assets.get(`level${this.level}/女.png`)));
    this.p1.position.set(32, 897);
    this.p2 = this.addChild(new Sprite(Assets.get(`level${this.level}/男.png`)));
    this.p2.position.set(176, 878);

    this.right = this.addChild(new Sprite(Assets.get(`common/right.png`)));
    this.right.position.set(613, 1104);
    this.right.alpha = 0;
    this.right.interactive = false;
    this.right.eventMode = "none";

    this.A.on("pointerdown", this.onPointerDown, this);
    this.A.on("globalpointermove", this.onPointerMove, this);
    this.A.on("pointerup", this.onPointerUp, this);
    this.A.on("pointerupoutside", this.onPointerUp, this);

    [this.p1, this.p2]
      .forEach((item) => {
        item.on("pointerdown", this.onPPointerDown, this);
        item.on("globalpointermove", this.onPPointerMove, this);
        item.on("pointerup", this.onPPointerUp, this);
        item.on("pointerupoutside", this.onPPointerUp, this);
      });
  }

  target = null;
  pos = null;

  onPPointerUp(e) {
    this.pos = null;
  }

  onPPointerDown(e) {
    this.target = e.target;
    const { x, y } = e.data.global;
    this.pos = {
      x: x - e.target.x,
      y: y - e.target.y
    }
  }

  onPPointerMove(e) {
    if (!this.pos) return;
    const { x, y } = e.data.global;
    const nx = x - this.pos.x;
    const ny = y - this.pos.y;
    this.target.position.set(nx, ny);
  }

  onPointerDown(e) {
    console.log("pointerdown", e.pointerId, e.data.global);

    // 记录触摸点
    this.touchPoints.set(e.pointerId, {
      x: e.data.global.x,
      y: e.data.global.y
    });

    // 如果有两个触摸点，开始缩放
    if (this.touchPoints.size === 2) {
      const points = Array.from(this.touchPoints.values());
      this.initialDistance = this.getDistance(points[0], points[1]);
      this.initialScale = this.A.scale.x;
      this.isTouching = true;
      console.log("开始缩放", this.initialDistance, this.initialScale);
    }
  }

  onPointerMove(e) {
    if (!this.touchPoints.has(e.pointerId)) return;

    // 更新触摸点位置
    this.touchPoints.set(e.pointerId, {
      x: e.data.global.x,
      y: e.data.global.y
    });

    if (this.isTouching && this.touchPoints.size === 2) {
      const points = Array.from(this.touchPoints.values());
      const currentDistance = this.getDistance(points[0], points[1]);

      // 计算缩放比例
      const scaleRatio = currentDistance / this.initialDistance;
      const newScale = this.initialScale * scaleRatio;

      // 限制最小和最大缩放
      const finalScale = Math.max(0.5, Math.min(newScale, 5));
      this.A.scale.set(finalScale);

      console.log("缩放中", currentDistance, scaleRatio, finalScale);

      // 检查是否达到目标大小
      this.checkSize();
    }
  }

  onPointerUp(e) {
    console.log("pointerup", e.pointerId);

    // 移除触摸点
    this.touchPoints.delete(e.pointerId);

    // 如果少于两个触摸点，停止缩放
    if (this.touchPoints.size < 2) {
      this.isTouching = false;
      console.log("停止缩放");
    }
  }

  getDistance(point1: { x: number, y: number }, point2: { x: number, y: number }): number {
    const dx = point1.x - point2.x;
    const dy = point1.y - point2.y;
    return Math.sqrt(dx * dx + dy * dy);
  }

  checkSize() {
    if (this.A.scale.x >= 1) {
      this.triggerSuccess();
    }
  }

  triggerSuccess() {
    // 防止重复触发
    if (this.right.alpha > 0) return;

    this.setPauseCd(true);
    this.setTouchEnable(false);

    Tween.get(this.right)
      .to({ alpha: 1 }, 444, Ease.quadInOut)
      .wait(rightWaitTime)
      .call(() => {
        globalEvent.emit(GameEvent.NextLevel);
      });
  }

  onDestroy() {
    super.onDestroy();
    Tween.removeTweens(this.A);
    Tween.removeTweens(this.right);

    // 移除触摸事件监听
    this.A.off("pointerdown", this.onPointerDown, this);
    this.A.off("globalpointermove", this.onPointerMove, this);
    this.A.off("pointerup", this.onPointerUp, this);
    this.A.off("pointerupoutside", this.onPointerUp, this);
  }
}
