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 } } = {
    left: {
        0: 0,
        1: 100,
        2: 200,
        3: 300,
    },
    middle: {
        0: 500,
        1: 600,
        2: 700,
        3: 800,
    },
    right: {
        0: 1000,
        1: 1100,
        2: 1200,
        3: 1300,
    },
    'left-middle': {
        0: 200,
        1: 200,
        2: 500,
        3: 500,
    },
    'right-middle': {
        0: 1100,
        1: 1100,
        2: 1300,
        3: 1300,
    },
}

export class TreeFactory {
    scene: PlayScene;
    possiblePositions: string[] = ["left", "middle", "right", "left-middle", "right-middle"];

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

    create(obstaclesGroup: Phaser.Physics.Arcade.Group) {
        const groupYPosition = Phaser.Math.Between(this.scene.scale.height, this.scene.scale.height * 1.8);
        const numberOfTrees = Math.floor(Math.random() * 4) + 1;
        const position = pickRandom(this.possiblePositions);
        for (let i = 0; i < numberOfTrees; i++) {
            const tree = new Tree(this.scene, position, i, groupYPosition);
            obstaclesGroup.add(tree);
        }
    }
}

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

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

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

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

    init() {
        this
            .setOrigin(0, 1)
            .setCollideWorldBounds(true)
            .setBodySize(150, 250)
            .setOffset(25, 15)
            .setDepth(2);

        // Handle resizing
        this.scene.scale.once('resize', this.scaleToFit, this);
        this.scene.scale.once('resize', this.positionOnMountain, this);

        this.scaleToFit();
        this.positionOnMountain();
    }

    positionOnMountain() {
        const scaleFactorX = this.scene.mountain?.scaleFactors.width || 1;
        this.setX(positionsMap[this.position][this.place] * scaleFactorX);
        this.setY(this.groupYPosition);
    }

    scaleToFit() {
        // Use the scale factor from the Mountain class to maintain consistency
        const scaleFactor = this.scene.mountain?.scaleFactors.height || 1;
        const width = 142;
        const height = 204;

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

    update() {

    }
}
