import { getConnectedCog } from "../../utils/CogManager";
import { EventBus } from "../EventBus";
import { BubbleUtils } from "../utils/BubbleUtils";
export class GameScene extends Phaser.Scene {
    selectedCogTiltLeftBlock = null;
    selectedCogTiltRightBlock = null;
    selectedCogFireBlock = null;
    isShooting = false;
    // α between 0 (max smoothing cog's acc values,
    //  max lag) and 1 (no smoothing):
    //@ts-ignore
    alpha = window.alpha || 0.1;
    smoothAx = 0;
    smoothAy = 0;
    bubbleUtils = new BubbleUtils();
    constructor() {
        super({ key: 'GameScene' });
        //@ts-ignore
        window.gameScene = this; // for debugging purposes
    }
    create() {
        this.bubbleUtils.setCanvasStyle();
        this.bubbleUtils.createBackground(this);
        this.bubbleUtils.createWalls(this);
        this.bubbleUtils.createRestartButton(this);
        this.bubbleUtils.createBubbleElements(this);
        const connectedCog = getConnectedCog();
        if (connectedCog) {
            this.smoothAx = connectedCog.raftStateInfo.accelerometer.ax;
            this.smoothAy = connectedCog.raftStateInfo.accelerometer.ay;
        }
        this.bubbleUtils.setupInputHandlers(this);
        this.bubbleUtils.loadSounds(this);
        this.bubbleUtils.createScoreText(this);
        EventBus.emit("scene-ready", this);
    }
    update(time, delta) {
        const connectedCog = getConnectedCog();
        if (connectedCog) {
            const ay = connectedCog.raftStateInfo.accelerometer.ay;
            const ax = connectedCog.raftStateInfo.accelerometer.ax;
            if (this.selectedCogTiltLeftBlock?.block === "moveleft" &&
                this.selectedCogTiltRightBlock?.block === "moveright") {
                // this is the right conditions so we use cog as steering wheel
                // 1) apply low-pass:
                this.smoothAx += this.alpha * (ax - this.smoothAx);
                this.smoothAy += this.alpha * (ay - this.smoothAy);
                // compute raw angle: zero = right, positive = counter-clockwise
                // but since your arrow art apparently points “up” at zero,
                // we’ll rotate by –90° (–π/2) to compensate.
                const raw = Math.atan2(this.smoothAy, -this.smoothAx); // radians, zero=right, CCW+
                const display = raw - Math.PI / 2; // shift so that (0,1)→up
                this.bubbleUtils.arrow?.setRotation(display);
                this.bubbleUtils.initialBubble?.setRotation(display);
                this.bubbleUtils.updateProjectionLine(display);
                this.bubbleUtils.updateProtractorArc(display);
                // shoot when cog's button is pressed
                if (this.selectedCogFireBlock?.block === "fire") {
                    const isCogButtonPressed = connectedCog.publishedDataAnalyser.cogState?.buttonClick;
                    if (isCogButtonPressed === "click" && ay > 0 && !this.isShooting) {
                        this.bubbleUtils.shootBubble(this);
                        this.isShooting = true;
                    }
                    else if (isCogButtonPressed === "release") {
                        this.isShooting = false;
                    }
                }
            }
            if (this.selectedCogTiltLeftBlock?.block === "moveleft" &&
                this.selectedCogTiltRightBlock?.block === "moveleft") {
                // when cog is steered left, the arrow rotates correctly to the left, but when steered right, it rotates to the left again. so basically it only steers left
                // ————— only steer left —————
                // 1) apply low-pass to the *raw* readings
                this.smoothAx += this.alpha * (ax - this.smoothAx);
                this.smoothAy += this.alpha * (ay - this.smoothAy);
                // 2) clamp the X to always be negative (–1..0)
                const sanitizedAx = -Math.abs(this.smoothAx);
                // 3) build angle exactly as before, but using sanitized
                const raw = Math.atan2(this.smoothAy, -sanitizedAx);
                const display = raw - Math.PI / 2;
                this.bubbleUtils.arrow?.setRotation(display);
                this.bubbleUtils.initialBubble?.setRotation(display);
                this.bubbleUtils.updateProjectionLine(display);
                this.bubbleUtils.updateProtractorArc(display);
                if (this.selectedCogFireBlock?.block === "fire") {
                    const isCogButtonPressed = connectedCog.publishedDataAnalyser.cogState?.buttonClick;
                    if (isCogButtonPressed === "click" && ay > 0 && !this.isShooting) {
                        this.bubbleUtils.shootBubble(this);
                        this.isShooting = true;
                    }
                    else if (isCogButtonPressed === "release") {
                        this.isShooting = false;
                    }
                }
            }
            if (this.selectedCogTiltLeftBlock?.block === "moveright" &&
                this.selectedCogTiltRightBlock?.block === "moveright") {
                // when cog is steered right, the arrow rotates correctly to the right, but when steered left, it rotates to the right again. so basically it only steers right
                // ————— only steer right —————
                // 1) apply low-pass
                this.smoothAx += this.alpha * (ax - this.smoothAx);
                this.smoothAy += this.alpha * (ay - this.smoothAy);
                // 2) clamp the X to always be positive (0..+1)
                const sanitizedAx = Math.abs(this.smoothAx);
                // 3) same atan2 but with sanitized
                const raw = Math.atan2(this.smoothAy, -sanitizedAx);
                const display = raw - Math.PI / 2;
                this.bubbleUtils.arrow?.setRotation(display);
                this.bubbleUtils.initialBubble?.setRotation(display);
                this.bubbleUtils.updateProjectionLine(display);
                this.bubbleUtils.updateProtractorArc(display);
                if (this.selectedCogFireBlock?.block === "fire") {
                    const isCogButtonPressed = connectedCog.publishedDataAnalyser.cogState?.buttonClick;
                    if (isCogButtonPressed === "click" && ay > 0 && !this.isShooting) {
                        this.bubbleUtils.shootBubble(this);
                        this.isShooting = true;
                    }
                    else if (isCogButtonPressed === "release") {
                        this.isShooting = false;
                    }
                }
            }
            if (this.selectedCogTiltLeftBlock?.block === "moveright" &&
                this.selectedCogTiltRightBlock?.block === "moveleft") {
                // when cog is steered left,
                // the arrow rotates wrongly to the right, and when steered right, it rotates to the left. so basically its opposite
                this.smoothAx += this.alpha * (ax - this.smoothAx);
                this.smoothAy += this.alpha * (ay - this.smoothAy);
                // compute raw angle: zero = right, positive = counter-clockwise
                // but since your arrow art apparently points “up” at zero,
                // we’ll rotate by –90° (–π/2) to compensate.
                const raw = Math.atan2(this.smoothAy, this.smoothAx); // radians, zero=right, CCW+
                const display = raw - Math.PI / 2; // shift so that (0,1)→up
                this.bubbleUtils.arrow?.setRotation(display);
                this.bubbleUtils.initialBubble?.setRotation(display);
                this.bubbleUtils.updateProjectionLine(display);
                this.bubbleUtils.updateProtractorArc(display);
                if (this.selectedCogFireBlock?.block === "fire") {
                    const isCogButtonPressed = connectedCog.publishedDataAnalyser.cogState?.buttonClick;
                    if (isCogButtonPressed === "click" && ay > 0 && !this.isShooting) {
                        this.bubbleUtils.shootBubble(this);
                        this.isShooting = true;
                    }
                    else if (isCogButtonPressed === "release") {
                        this.isShooting = false;
                    }
                }
            }
        }
    }
}
