"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Player = void 0;
require("core-js/modules/es.promise.js");
var _CogManager = require("../../utils/CogManager");
var _EventBus = require("../EventBus");
class Player extends Phaser.Physics.Arcade.Sprite {
  constructor(scene) {
    const x = 0;
    const y = 0;
    super(scene, x, y, "marty-waist");
    this.scene = scene;
    scene.add.existing(this);
    scene.physics.add.existing(this);
    this.init();
    this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
  }
  init() {
    if (!this.scene.input.keyboard) return;
    this.registerSounds();

    // Handle resizing
    this.scene.scale.once('resize', this.scaleToFit, this);
    this.scene.scale.once('resize', this.positionOnSky, this);
  }
  positionOnSky() {
    var _this$scene$sky, _this$scene$ground, _this$scene$sky2;
    const gravity = 2000 * (((_this$scene$sky = this.scene.sky) === null || _this$scene$sky === void 0 ? void 0 : _this$scene$sky.scaleFactors.height) || 1);
    this.setOrigin(0.5, 1) // Origin at the bottom center of the player
    .setGravityY(gravity).setCollideWorldBounds(false).setBodySize(90, 140).setOffset(35, 20).setVisible(false).setDepth(4);
    const gameHeight = this.scene.scale.height;

    // Position player at the bottom of the scaled background
    const skyHeight = this.scene.scale.height;
    // const y = gameHeight - (gameHeight - skyHeight); // Slight offset for alignment
    const y = (((_this$scene$ground = this.scene.ground) === null || _this$scene$ground === void 0 ? void 0 : _this$scene$ground.y) || 0) - 140;
    this.setY(y);
    this.setX(500 * (((_this$scene$sky2 = this.scene.sky) === null || _this$scene$sky2 === void 0 ? void 0 : _this$scene$sky2.scaleFactors.width) || 1));
    this.setVisible(true);
  }
  scaleToFit() {
    var _this$scene$sky3;
    // Use the scale factor from the Sky class to maintain consistency
    const scaleFactor = ((_this$scene$sky3 = this.scene.sky) === null || _this$scene$sky3 === void 0 ? void 0 : _this$scene$sky3.scaleFactors.height) || 1;
    const width = 285;
    const height = 285;

    // Apply the scale factor to the player
    this.setDisplaySize(width * scaleFactor, height * scaleFactor);
    this.setVisible(true);
  }
  getStartTrigger() {
    /* If the player is connected to a cog, use the cog's buttonClick state to determine if the player should jump / start the game */
    /* Otherwise, use the space key to determine if the player should jump / start the game */
    const connectedCog = (0, _CogManager.getConnectedCog)();
    if (connectedCog) {
      return connectedCog.publishedDataAnalyser.cogState.buttonClick === "click";
    } else {
      return false; // remove this line and comment in below if you want to use the space key to jump when not connected to a cog
      // if (!this.scene.cursors) return false;
      // const space = this.scene.cursors.space;
      // if (this.scene.input.keyboard && this.scene.input.keyboard.checkDown(this.scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A), 0)) {
      //   return true;
      // }
      // if (!space) return false;
      // const isSpaceJustDown = Phaser.Input.Keyboard.JustDown(space);
      // // const { space } = this.scene.cursors;
      // // const isSpaceJustDown = Phaser.Input.Keyboard.JustDown(space);
      // return isSpaceJustDown;
    }
  }
  actionDecider(isCogsButtonClickedOrSpaceDown, onFloor) {
    var _this$scene$currently, _this$scene$currently2, _this$scene$currently3;
    if (!isCogsButtonClickedOrSpaceDown) return;
    if (((_this$scene$currently = this.scene.currentlySectedBlock) === null || _this$scene$currently === void 0 ? void 0 : _this$scene$currently.block) === "moveforward") {
      this.moveForward();
    } else if (((_this$scene$currently2 = this.scene.currentlySectedBlock) === null || _this$scene$currently2 === void 0 ? void 0 : _this$scene$currently2.block) === "movebackward") {
      this.moveBackward();
    } else if (((_this$scene$currently3 = this.scene.currentlySectedBlock) === null || _this$scene$currently3 === void 0 ? void 0 : _this$scene$currently3.block) === "jump") {
      this.jump(onFloor);
    }
  }
  async moveBackward() {
    const move = 10;
    for (let i = 0; i < move; i++) {
      this.setX(this.x - 1);
      await new Promise(resolve => setTimeout(resolve, 10));
    }
  }
  async moveForward() {
    const move = 10;
    for (let i = 0; i < move; i++) {
      this.setX(this.x + 1);
      await new Promise(resolve => setTimeout(resolve, 10));
    }
  }
  jump(onFloor) {
    if (onFloor) {
      var _this$scene$sky4, _this$jumpSound;
      const jumpVelocity = -1200 * (((_this$scene$sky4 = this.scene.sky) === null || _this$scene$sky4 === void 0 ? void 0 : _this$scene$sky4.scaleFactors.height) || 1);
      this.setVelocityY(jumpVelocity);
      (_this$jumpSound = this.jumpSound) === null || _this$jumpSound === void 0 || _this$jumpSound.play();
      this.setTexture("marty-jump");
      _EventBus.EventBus.emit('player-jumped', this);
    }
  }
  isOffScreen() {
    const bounds = this.getBounds();
    const adjustedGameWidth = this.scene.scale.width;
    const isOffScreen = bounds.right < 0 || bounds.left > adjustedGameWidth;
    return isOffScreen;
  }
  update() {
    const isCogsButtonClickedOrSpaceDown = this.getStartTrigger();
    if (this.isOffScreen()) {
      _EventBus.EventBus.emit('player-off-screen', this);
    }
    // if (isCogsButtonClickedOrSpaceDown && !this.scene.isGameRunning && !this.scene.isGameOverVisible) {
    //   this.scene.handleGameStart();
    // }

    // if (!this.scene.isGameRunning) {
    //   return;
    // }

    const onFloor = this.body.onFloor();
    if (onFloor) {
      this.setTexture("marty-waist");
    }
    // get currently selected block and decide movement based on that
    this.actionDecider(isCogsButtonClickedOrSpaceDown, onFloor);
    if (this.body && this.body.deltaAbsY() > 1.39) {
      this.anims.stop();
    } else {
      // this.playRunAnimation();
    }
  }
  registerSounds() {
    this.jumpSound = this.scene.sound.add("jump", {
      volume: 0.3
    });
    this.hitSound = this.scene.sound.add("hit", {
      volume: 0.4
    });
  }
  die() {
    var _this$hitSound;
    this.anims.pause();
    this.setTexture("marty-hurt");
    (_this$hitSound = this.hitSound) === null || _this$hitSound === void 0 || _this$hitSound.play();
  }
}
exports.Player = Player;