export class Game extends Phaser.Scene {
    constructor() {
        super('Game');
        this.score;
        this.mult;
    }

    create() {

        //Json object for items to buy and every game-related parameters
        let objects = {
            "baguette" : {
                addMultiplier: .2,
                amount: 0,
                price: 10,
                factor: 1.5
            },
            "croissant" : {
                addMultiplier: .6,
                amount: 0,
                price: 100,
                factor: 2
            },
            "camembert" : {
                addMultiplier: 1,
                amount: 0,
                price: 1000,
                factor: 2.5
            },
            "cafe" : {
                addMultiplier: 1.5,
                amount: 0,
                price: 10000,
                factor: 3.5
            },
            "wine" : {
                addMultiplier: 2,
                amount: 0,
                price: 100000,
                factor: 5
            }
        }

        //settings
        let croissant;
        let baguette;
        this.score = 0;
        this.mult = 1;

        //f_guy coords
        //also baguette and croissnat physic drops coords
        let x = 745;
        let y = 396;

        //Setting up the background
        //background color
        //and background picture
        this.cameras.main.setBackgroundColor(0x00ff00);

        this.add.image(512, 384, 'background').setAlpha(0.5);
        this.add.image(512, 384, 'baker');

        // Create tutorial text
        const tutorialText = this.add.text(512, 650, 'Click on the french guy to generate Francs!', {
            fontFamily: 'Arial Black', fontSize: 28, color: '#ffffff',
            stroke: '#000000', strokeThickness: 8,
            align: 'center'
        }).setOrigin(0.5);

        // Create score text
        this.scoreText = this.add.text(x, 75, 'Francs : 0', {
            fontFamily: 'Arial Black', fontSize: 28, color: '#ffffff',
            stroke: '#000000', strokeThickness: 8,
            align: 'center'
        }).setOrigin(0.5);

        // Let's create our french guy #1
        const fguy = this.add.image(x, y, 'fguy1');
        fguy.setInteractive();
        // #2 (used later)
        const fguy2 = this.add.image(x, y, 'fguy2');
        fguy2.setVisible(false);

        //----------------------------------------------------------------------------------
        //
        // SHOPS SECTION

        //Shop #1 baguette

        //Baguette SHOP TEXT
        this.baguettePrice = this.add.text(100, 50, " (" + objects['baguette'].amount + "x) : " + objects['baguette'].price.toFixed(2), {
            fontFamily: 'Arial Black', fontSize: 28, color: '#ffffff',
            stroke: '#000000', strokeThickness: 8,
            align: 'center'
        }).setOrigin(0, 0.5);

        //Baguette SHOP IMAGE
        const baguetteShop = this.add.image(50, 50, 'baguette');
        baguetteShop.setInteractive();
        
        //INTERACTIVE
        baguetteShop.on('pointerdown', () => {
            [this.score, this.mult] = BuyObject(this.score, this.mult, 'baguette', objects);
            this.scoreText.setText('Francs : ' + this.score.toFixed(2));
            this.baguettePrice.setText( " (" + objects['baguette'].amount + "x) : " + objects['baguette'].price.toFixed(2) );
        });

        //--------------------------------

        //Shop #2 croissant

        //Croissant SHOP TEXT
        this.croissantPrice = this.add.text(100, 150, " (" + objects['croissant'].amount + "x) : " + objects['croissant'].price.toFixed(2), {
            fontFamily: 'Arial Black', fontSize: 28, color: '#ffffff',
            stroke: '#000000', strokeThickness: 8,
            align: 'center'
        }).setOrigin(0, 0.5);

        //Croissant SHOP IMAGE
        const croissantShop = this.add.image(50, 150, 'croissant');
        croissantShop.setInteractive();
        
        //INTERACTIVE
        croissantShop.on('pointerdown', () => {
            [this.score, this.mult] = BuyObject(this.score, this.mult, 'croissant', objects);
            this.scoreText.setText('Francs : ' + this.score.toFixed(2));
            this.croissantPrice.setText(" (" + objects['croissant'].amount + "x) : " + objects['croissant'].price.toFixed(2));
        });

        //--------------------------------

        //Shop #3 camembert

        //Camembert SHOP TEXT
        this.camembertPrice = this.add.text(100, 250, " (" + objects['camembert'].amount + "x) : " + objects['camembert'].price.toFixed(2), {
            fontFamily: 'Arial Black', fontSize: 28, color: '#ffffff',
            stroke: '#000000', strokeThickness: 8,
            align: 'center'
        }).setOrigin(0, 0.5);

        //Camembert SHOP IMAGE
        const camembertShop = this.add.image(50, 250, 'camembert');
        camembertShop.setInteractive();
        
        //INTERACTIVE
        camembertShop.on('pointerdown', () => {
            [this.score, this.mult] = BuyObject(this.score, this.mult, 'camembert', objects);
            this.scoreText.setText('Francs : ' + this.score.toFixed(2));
            this.camembertPrice.setText(" (" + objects['camembert'].amount + "x) : " + objects['camembert'].price.toFixed(2));
        });

        //--------------------------------

        //Shop #4 croissant

        //Cafe SHOP TEXT
        this.cafePrice = this.add.text(100, 350, " (" + objects['cafe'].amount + "x) : " + objects['cafe'].price.toFixed(2), {
            fontFamily: 'Arial Black', fontSize: 28, color: '#ffffff',
            stroke: '#000000', strokeThickness: 8,
            align: 'center'
        }).setOrigin(0, 0.5);

        //Cafe SHOP IMAGE
        const cafeShop = this.add.image(50, 350, 'cafe');
        cafeShop.setInteractive();

        //INTERACTIVE
        cafeShop.on('pointerdown', () => {
            [this.score, this.mult] = BuyObject(this.score, this.mult, 'cafe', objects);
            this.scoreText.setText('Francs : ' + this.score.toFixed(2));
            this.cafePrice.setText(" (" + objects['cafe'].amount + "x) : " + objects['cafe'].price.toFixed(2));
        });

        //--------------------------------

        //Shop #5 wine

        //Wine SHOP TEXT
        this.winePrice = this.add.text(100, 450, " (" + objects['wine'].amount + "x) : " + objects['wine'].price.toFixed(2), {
            fontFamily: 'Arial Black', fontSize: 28, color: '#ffffff',
            stroke: '#000000', strokeThickness: 8,
            align: 'center'
        }).setOrigin(0, 0.5);

        //Wine SHOP IMAGE
        const wineShop = this.add.image(50, 450, 'wine');
        wineShop.setInteractive();
        
        //INTERACTIVE
        wineShop.on('pointerdown', () => {
            [this.score, this.mult] = BuyObject(this.score, this.mult, 'wine', objects);
            this.scoreText.setText('Francs : ' + this.score.toFixed(2));
            this.winePrice.setText(" (" + objects['wine'].amount + "x) : " + objects['wine'].price.toFixed(2));
        });

        // END OF SHOPS SECTION
        //
        //------------------------------------------------


        //the player clicks on the french guy
        fguy.on('pointerdown', () => {

            //first click?
            //then deactivate the tutorial
            if (tutorialText.active) {
                tutorialText.setVisible(false);
            }

            //create an "animation" buy setting another sprite on
            fguy2.setVisible(true);
            
            //drop a croissant or a baguette? 
            // 0.9/1 -> baguette
            // 0.1/1 -> croissant
            let dropIsBaguette = true;
            let n = Math.random();

            //if the player obtain at least 1 croissant then he can drop it
            //only cosmetic
            if (n > 0.9 && objects['croissant'].amount > 0){
                dropIsBaguette = false;
            }

            //then drop either a baguette or a croissant
            //same behaviour, but we need to create a different image each case
            if(dropIsBaguette){
                baguette = this.physics.add.image(x, y, 'baguette');
                
                //apply a random up velocity
                const randomXVelocity = Phaser.Math.Between(-300, 300);
                baguette.setVelocityX(randomXVelocity);
                baguette.setVelocityY(-500);

            } else {
                croissant = this.physics.add.image(x, y, 'croissant');

                //apply a random up velocity
                const randomXVelocity = Phaser.Math.Between(-300, 300);
                croissant.setVelocityX(randomXVelocity);
                croissant.setVelocityY(-500);
            }

            //then we can update the score (0.01 rounded)
            this.score += this.mult;
            this.scoreText.setText('Francs : ' + this.score.toFixed(2));
        });

        //the player releases his click, we deactivate the other sprite
        fguy.on('pointerup', () => {

            //make the sprite invisible
            fguy2.setVisible(false);
        });
    }

    //never used but in case of a future application..
    GameOver() {
        this.score = 0;
        this.multiplier = 1;
        this.scene.restart();
    }
}

//Function to buy an 'object' with the 'objects' JSON object
function BuyObject(_score, _mult, object, objects) {
    let item = objects[object];

    //buy the item if enough Francs
    //and update factor, multiplier, ...
    if( _score >= item.price ) {
        _score -= item.price;
        item.amount++;
        _mult += item.addMultiplier;
        item.price *= item.factor;
        console.log("Item bought!");
    }
    else{
        console.log("Not enough Money!");
    }
    return [_score, _mult];   
}
