const config = {
    type: Phaser.AUTO,
    width: 1280,
    height: 800,
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 300 },
            debug: false
        }

    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

const game = new Phaser.Game(config);

function preload() {
    this.load.image('background', 'assets/background.png');
    this.load.image('ground', 'assets/platforms.png');
    this.load.image('player', 'assets/player.png');
    this.load.image('player2', 'assets/player2.png');
    this.load.image('player_jump', 'assets/player_jump.png');
    this.load.image('bricks', 'assets/bricks.png');
    this.load.image('pee', 'assets/pee_test.png');
    this.load.image('clay', 'assets/platform_clay.png');
    this.load.image('drink', 'assets/drink.png');

    // //Create an animation for walking left
    // this.anims.create({
    //     key: 'walkLeft',
    //     frames: [
    //         { key: 'player'},
    //         { key: 'player2' }
    //     ]
    // });
}

let player, cursors, ground, platforms, drinks=0, score = 0, scoreText;

function create() {
    // Add background image
    const background = this.add.image(0, 0, 'background').setOrigin(0, 0);
    background.scaleX = this.scale.width / background.width;
    background.scaleY = this.scale.height / background.height;
    background.setScrollFactor(0);

    // Create ground
    platforms = this.physics.add.staticGroup();
    platforms.create(650, 750, 'ground')
        .setOffset(0, 50);

    // Add Bricks
    platforms.create(600, 600, 'bricks').setScale(.5).refreshBody();
    platforms.create(1100, 400, 'bricks').setScale(.5).refreshBody();
    platforms.create(900, 250, 'bricks').setScale(.5).refreshBody();

    // Add Elevators
    //let elevators = this.physics.add.staticGroup();
    let elevators = this.physics.add.image(400, 200, 'clay')
        .setScale(.3);

    // Add Platforms
    platforms.create(1200, 500, 'ground')
        .setScale(.5)
        .refreshBody()
        .setSize(1296/2, 50)
        .setOffset(0, 25);
    platforms.create(350, 300, 'ground')
        .setScale(.5)
        .refreshBody()
        .setSize(1296/2, 50)
        .setOffset(0, 25);

    // pee
    peeTest = this.physics.add.image(100, 200, 'pee')
        .setScale(.10)
        .refreshBody();

     // Create player
    player = this.physics.add.sprite(645, 450, 'player');
    player.setBounce(0.2);
    player.setSize(100, 140);
    player.setOffset(30, 4);

    // Add controls
    cursors = this.input.keyboard.createCursorKeys();

    // Add collision and sound
    this.physics.add.collider(player, platforms);
    this.physics.add.collider(player, elevators);
    //this.sound.add('jump', { volume: 0.5 });

    stars = this.physics.add.group({
        key: 'drink',
        repeat: 13,
        setXY: { x: 200, y: 0, stepX: 70 }
    });

    stars.children.iterate(function (child) {

        child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));

    });

    this.physics.add.collider(stars, platforms);
    this.physics.add.collider(peeTest, platforms);
    this.physics.add.overlap(player, stars, collectDrink, null, this);
    this.physics.add.overlap(player, peeTest, collectPee, null, this);

    function collectDrink (player, star) {
        star.disableBody(true, true);
        drinks += 1;
        scoreText.setText('Drinks: ' + drinks + ' Score: ' + score);
    }

    function collectPee (player, peeTest) {
        score += drinks * 10;
        drinks = 0;
        scoreText.setText('Drinks: ' + drinks + ' Score: ' + score);
    }

    scoreText = this.add.text(16, 16, 'Drinks: 0 Score: 0', { fontSize: '32px', fill: '#000' });

    // Enable camera follow
    this.cameras.main.startFollow(player, true, 0, 0.1);

    this.tweens.add({
        targets: elevators,
        y: -150,
        duration: 4000,
        repeat: -1, // Repeat infinitely
        yoyo: true, // Go back to the original position after reaching the target
        ease: 'Linear' // You can change this to other easing functions like 'Sine', 'Cubic', etc.
    });
}

function update() {
    if (cursors.left.isDown && player.body.x > 0) {
        player.setVelocityX(-160);
        player.setFlipX(false);
        //player.anims.play('walkLeft', true); // Play walking animation
    } else if (cursors.right.isDown && player.body.x < 1280 - 100) {
        player.setVelocityX(160);
        player.setFlipX(true);
    } else {
        player.setVelocityX(0);
    }

    if (cursors.up.isDown && player.body.touching.down) {
        player.setVelocityY(-330);
    }

    if (player.body.velocity.y > -1) {
        player.setTexture('player'); // Change texture back to normal when touching the ground
    } else {
        player.setTexture('player_jump'); // Change texture to jump
    }

}