import PlayScene from "../scenes/PlayScene";

const pickRandom = <T>(arr: T[]): T => arr[Math.floor(Math.random() * arr.length)];

const positionsMap: { [position: string]: { [place: number]: number } } = {
    bottom: {
        0: -50 + 10,
        1: 35 + 10,
        2: 120 + 10,
        3: 205 + 10,
    },
    middle: {
        0: 120 + 10,
        1: 205 + 10,
        2: 290 + 10,
        3: 375 + 10,
    },
    top: {
        0: 330 + 10,
        1: 415 + 10,
        2: 500 + 10,
        3: 585 + 10,
    },
    'top-bottom': {
        0: -50 + 10,
        1: 35 + 10,
        2: 500 + 10,
        3: 585 + 10,
    },
}

export class CogFactory {
    scene: PlayScene;
    possiblePositions: string[] = ["bottom", "top", "middle", "top-bottom"];

    constructor(scene: PlayScene) {
        this.scene = scene;
    }

    create(obstaclesGroup: Phaser.Physics.Arcade.Group) {
        const groupXPosition = Phaser.Math.Between(this.scene.scale.width, this.scene.scale.width * 1.8);
        const numberOfCogs = Math.floor(Math.random() * 3) + 2;
        const position = pickRandom(this.possiblePositions);
        for (let i = 0; i < numberOfCogs; i++) {
            const cog = new Cog(this.scene, position, i, groupXPosition);
            obstaclesGroup.add(cog);
        }
        //     {const numberOfCogs = 4
        //     const position = "top";
        //     const groupXPosition = 100;
        //     console.log(`Creating ${numberOfCogs} cogs at ${position}`);
        //     for (let i = 0; i < numberOfCogs; i++) {
        //         const cog = new Cog(this.scene, position, i, groupXPosition);
        //         obstaclesGroup.add(cog);
        //     }}
        //    { const numberOfCogs = 4
        //     const position = "middle";
        //     const groupXPosition = 200;
        //     console.log(`Creating ${numberOfCogs} cogs at ${position}`);
        //     for (let i = 0; i < numberOfCogs; i++) {
        //         const cog = new Cog(this.scene, position, i, groupXPosition);
        //         obstaclesGroup.add(cog);
        //     }}
        //    { const numberOfCogs = 4
        //     const position = "bottom";
        //     const groupXPosition = 300;
        //     console.log(`Creating ${numberOfCogs} cogs at ${position}`);
        //     for (let i = 0; i < numberOfCogs; i++) {
        //         const cog = new Cog(this.scene, position, i, groupXPosition);
        //         obstaclesGroup.add(cog);
        //     }}
        //     { const numberOfCogs = 4
        //         const position = "top-bottom";
        //         const groupXPosition = 400;
        //         console.log(`Creating ${numberOfCogs} cogs at ${position}`);
        //         for (let i = 0; i < numberOfCogs; i++) {
        //             const cog = new Cog(this.scene, position, i, groupXPosition);
        //             obstaclesGroup.add(cog);
        //         }}
    }
}

export class Cog extends Phaser.Physics.Arcade.Sprite {
    scene: PlayScene;
    position: string;
    place: number;
    groupXPosition: number;

    constructor(scene: PlayScene, position: string, place: number, groupXPosition: number) {
        const x = 0;
        const y = 0;
        super(scene, x, y, "cog");
        this.scene = scene;
        this.position = position;
        this.place = place;
        this.groupXPosition = groupXPosition;

        scene.add.existing(this);
        scene.physics.add.existing(this);

        this.init();
        this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
        this.once(Phaser.GameObjects.Events.DESTROY, this.cleanup, this);
    }

    init() {
        this
            .setOrigin(0, 1)
            .setCollideWorldBounds(false)
            .setBodySize(180, 180)
            .setOffset(-15, 30)
            .setDepth(2)
            .setAngle(-27); // Rotate the cog by 60 degrees

        // Handle resizing
        this.scene.scale.on('resize', this.scaleToFit, this);
        this.scene.scale.on('resize', this.positionOnSky, this);

        this.scaleToFit();
        this.positionOnSky();
    }

    positionOnSky() {
        const cogHeight = positionsMap[this.position][this.place] * (this.scene.sky?.scaleFactors.height || 1);

        const groundTopY = this.scene.ground?.y ?? this.scene.scale.height;
        const y = groundTopY - cogHeight;
        this.setY(y - 10);

        this.setX(this.groupXPosition);
    }

    scaleToFit() {
        // Use the scale factor from the Sky class to maintain consistency
        const scaleFactor = this.scene.sky?.scaleFactors.height || 1;
        const width = 85;
        const height = 85;

        // Apply the scale factor to the Cog
        this.setDisplaySize(width * scaleFactor, height * scaleFactor);
    }

    update() {

    }

    cleanup() {
        this.scene?.events.off(Phaser.Scenes.Events.UPDATE, this.update, this);
        this.scene?.scale.off('resize', this.scaleToFit, this);
        this.scene?.scale.off('resize', this.positionOnSky, this);
    }
}
