import { getConnectedCog } from "../../utils/CogManager";
export class Player extends Phaser.Physics.Arcade.Sprite {
    scene;
    jumpSound;
    hitSound;
    canGoRight;
    constructor(scene) {
        const x = 1000;
        const y = 1000;
        super(scene, x, y, "marty-ski-straight");
        this.scene = scene;
        this.canGoRight = true;
        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;
        // const gravity = 2000 * (this.scene.mountain?.scaleFactors.height || 1);
        this
            .setOrigin(0.5, 1) // Origin at the bottom center of the player
            .setCollideWorldBounds(true, undefined, undefined, true)
            .setBodySize(90, 50)
            .setOffset(35, 20)
            .setDepth(4)
            .setVisible(false);
        this.registerSounds();
        // Handle resizing
        this.scene.scale.once('resize', this.scaleToFit, this);
        this.scene.scale.once('resize', this.positionOnMountain, this);
        // this.scaleToFit();
        // this.positionOnMountain();
    }
    positionOnMountain() {
        this.setY(600 * (this.scene.mountain?.scaleFactors.height || 1));
        // position middle of screen
        this.setX(this.scene.scale.width / 2);
        this.setVisible(true);
    }
    scaleToFit() {
        // console.log("Player scaleToFit triggered");
        // Use the scale factor from the Mountain class to maintain consistency
        const scaleFactor = this.scene.mountain?.scaleFactors.height || 1;
        const width = 180 * .8;
        const height = 285 * .8;
        // Apply the scale factor to the player
        this.setDisplaySize(width * scaleFactor, height * scaleFactor);
    }
    // getStartTrigger(): "left" | "right" | "none" {
    //   /* 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 W or S keys to determine if the player should jump / start the game */
    //   const connectedCog = getConnectedCog();
    //   if (connectedCog) {
    //     const tiltDirection = (connectedCog as Cog).publishedDataAnalyser.cogState.tilt;
    //     if (tiltDirection === "left" || tiltDirection === "right") {
    //       return tiltDirection;
    //     } else {
    //       return "none";
    //     }
    //   } else {
    //     if (!this.scene.cursors) return "none";
    //     const { left, right } = this.scene.cursors;
    //     if (left.isDown) {
    //       return "left";
    //     } else if (right.isDown) {
    //       return "right";
    //     } else {
    //       return "none";
    //     }
    //   }
    // }
    // actionDecider(tiltDirection: "left" | "right" | "none") {
    //   if (tiltDirection === "none") return;
    //   if (tiltDirection === "left") {
    //     if (this.scene.selectedCogTiltLeftBlock?.block === "moveleft") {
    //       this.moveLeft();
    //       return "left";
    //     } else if (this.scene.selectedCogTiltLeftBlock?.block === "moveright") {
    //       this.moveRight();
    //       return "right";
    //     }
    //   } else if (tiltDirection === "right") {
    //     if (this.scene.selectedCogTiltRightBlock?.block === "moveleft") {
    //       this.moveLeft();
    //       return "left";
    //     } else if (this.scene.selectedCogTiltRightBlock?.block === "moveright") {
    //       this.moveRight();
    //       return "right";
    //     }
    //   }
    // }
    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 W or S keys to determine if the player should jump / start the game */
        const connectedCog = getConnectedCog();
        if (connectedCog) {
            const data = connectedCog.raftStateInfo.accelerometer;
            const tiltValue = data.ax;
            return tiltValue;
        }
        else {
            return "none";
            //   if (!this.scene.cursors) return "none";
            //   const { left, right } = this.scene.cursors;
            //   if (left.isDown) {
            //     return "left";
            //   } else if (right.isDown) {
            //     return "right";
            //   } else {
            //     return "none";
            //   }
        }
    }
    actionDecider(tiltDirection) {
        if (tiltDirection === "none")
            return;
        if (tiltDirection === 0)
            return "none";
        if (tiltDirection === "left" || tiltDirection < 0) {
            if (this.scene.selectedCogTiltLeftBlock?.block === "moveleft") {
                this.moveLeft(tiltDirection);
                if (tiltDirection > -0.1) {
                    return "none";
                }
                return "left";
            }
            else if (this.scene.selectedCogTiltLeftBlock?.block === "moveright") {
                if (tiltDirection > -0.1) {
                    return "none";
                }
                this.moveRight(-tiltDirection);
                return "right";
            }
        }
        else if (tiltDirection === "right" || tiltDirection > 0) {
            if (this.scene.selectedCogTiltRightBlock?.block === "moveleft") {
                this.moveLeft(-tiltDirection);
                if (tiltDirection < 0.1) {
                    return "none";
                }
                return "left";
            }
            else if (this.scene.selectedCogTiltRightBlock?.block === "moveright") {
                this.moveRight(tiltDirection);
                if (tiltDirection < 0.1) {
                    return "none";
                }
                return "right";
            }
        }
    }
    async moveLeft(tiltValue) {
        if (typeof tiltValue === "string") {
            const move = 10;
            for (let i = 0; i < move; i++) {
                this.setX(this.x - .3);
                await new Promise((resolve) => setTimeout(resolve, 10));
            }
        }
        else {
            // use tiltValue to move left
            this.setX(this.x + (tiltValue * 4));
            await new Promise((resolve) => setTimeout(resolve, 10));
        }
    }
    async moveRight(tiltValue) {
        if (!this.canGoRight)
            return;
        if (typeof tiltValue === "string") {
            const move = 10;
            for (let i = 0; i < move; i++) {
                this.setX(this.x + .3);
                await new Promise((resolve) => setTimeout(resolve, 10));
            }
        }
        else {
            // use tiltValue to move right
            this.setX(this.x + (tiltValue * 4));
            await new Promise((resolve) => setTimeout(resolve, 10));
        }
    }
    update() {
        const tiltDirection = this.getStartTrigger();
        // if (tiltDirection && !this.scene.isGameRunning && !this.scene.isGameOverVisible) {
        //   this.scene.handleGameStart();
        // }
        if (!this.scene.isGameRunning) {
            this.anims.pause();
            return;
        }
        const actualMove = this.actionDecider(tiltDirection);
        // Set the appropriate texture based on the direction.
        if (actualMove === "right") {
            this.setBodySize(90, 30);
            this.setOffset(80, 250);
            this.setTexture("marty-ski-right");
        }
        else if (actualMove === "left") {
            this.setBodySize(90, 30);
            this.setOffset(20, 250);
            this.setTexture("marty-ski-left");
        }
        else {
            this.setTexture("marty-ski-straight");
            this.setBodySize(90, 30);
            this.setOffset(35, 180);
        }
    }
    registerSounds() {
        this.hitSound = this.scene.sound.add("hit", { volume: 0.4 });
    }
    die() {
        this.anims.pause();
        this.setTexture("marty-ski-hurt");
        this.hitSound?.play();
    }
}
