import { Player } from '../characters/player.js';
import { ProEdgeEnemy } from '../characters/proEdge.js';
//import { NNBoy } from '../characters/99boy.js';
import { Interface } from '../interface.js';
 
export class ProEdge extends Phaser.Scene {

    constructor() {
        super({ key: 'ProEdge' });

        this.cameras;
        this.player;
        this.cursors = null;
        this.wide = 1600;
        this.tall = 900;
        this.topBound = 1100;
        this.leftBound = 800;
    }

    preload() {
        this.load.image('proedge-tiles', 'assets/proedge-tiles.png');
        this.load.image('heart-full', 'assets/heart-full.png');  
        this.load.image('heart-empty', 'assets/heart-empty.png');
        this.load.image('heart-upgrade', 'assets/heart-upgrade.png');
        this.load.image('explosion', 'assets/explosion.png');
        this.load.tilemapTiledJSON('proedgeMap', 'assets/proedge.json'); 
        this.load.spritesheet('player-tiles', 'assets/player-tiles.png', {
            frameWidth: 100, 
            frameHeight: 100 
        });
        this.load.spritesheet('proedge-enemies', 'assets/proedge-enemies.png', {
            frameWidth: 100, 
            frameHeight: 100 
        });
        this.load.spritesheet('boss-tiles', 'assets/boss-tiles.png', {
            frameWidth: 200, 
            frameHeight: 200 
        });

        this.load.audio('dungeonMusic', 'assets/music/99Dungeon.mp3');
        this.load.audio('gunShot', 'assets/sounds/gun-shot.mp3');
        this.load.audio('axeThrow', 'assets/sounds/axe.mp3');
        this.load.audio('health-pickup', 'assets/sounds/health-pickup.mp3');
        this.load.audio('player-damage', 'assets/sounds/player-damage.mp3');

        // Enemy Sounds
        this.load.audio('swoosh', 'assets/sounds/swoosh.mp3');
    }

    create() {                
        // Load the map and tileset
        const proedgeMap = this.make.tilemap({ key: 'proedgeMap' });
        const proedgeTiles = proedgeMap.addTilesetImage('proedge-tiles', 'proedge-tiles');
        //const NNTiles = proedgeMap.addTilesetImage('99boy-tiles', '99boy-tiles');
        const mainLayer = proedgeMap.createLayer('main', proedgeTiles, 0, 0)
            .setCollisionByProperty({ collides: true });
        //const objectsLayer = proedgeMap.createLayer('objects', dungeonTiles, 0, 0)
        //    .setCollisionByProperty({ collides: true });
        const enemiesLayer = proedgeMap.getObjectLayer('enemies');
        //const interactiveLayer = proedgeMap.getObjectLayer('interactive');
        //const zoneLayer = proedgeMap.getObjectLayer('zones');

        // Show Score
        this.interface = new Interface(this);
        this.interface.showScore();

        // Add a player
        this.player = new Player(this, this.game.globalState.startProX, this.game.globalState.startProY);
        //this.player = new Player(this, 3550, 350);
        this.player.healthBars(true, 1, 3);

        // zoneLayer.objects.forEach(object => {
        //     if (object.name === 'gulchExit') {
        //         const Gulch = this.add.rectangle(object.x, object.y, object.width, object.height);
        //         this.Gulch = this.physics.add.existing(Gulch);
        //     }
        //     if (object.name === 'bossSave') {
        //         const bossSave = this.add.rectangle(object.x, object.y, object.width, object.height);
        //         this.bossSave = this.physics.add.existing(bossSave);
        //     }
        // });

        // Add Interactive
        // this.interactive = this.physics.add.group();
        // interactiveLayer.objects.forEach(object => {
        //     if (object.gid) {
        //         let inter = this.interactive.create(object.x+50, object.y-50, '99Dungeon-tiles', object.gid-1);
        //         inter.properties = object.properties;
        //     } else {
        //         let inter = this.physics.add.existing(this.add.rectangle(object.x, object.y, object.width, object.height));
        //         this.interactive.add(inter);
        //         if (object.properties) {
        //             inter.properties = object.properties;
        //         }
        //     }
        // });

        // Add Enemies
        this.enemies = this.physics.add.group({
            classType: ProEdgeEnemy,
            runChildUpdate: true
        });
        enemiesLayer.objects.forEach(object => {
            let aProEdge;
            console.log(object.gid);
            if (object.gid > 300) {
                aProEdge = this.enemies.create(object.x-50, object.y-50, 'boss-tiles', object.gid-301);
            } else {
                aProEdge = this.enemies.create(object.x-50, object.y-50, 'proedge-enemies', object.gid-101);
            }
            if (object.properties) {
                object.properties.forEach(prop => {
                    if (prop.name === 'patrolX') {
                        let to = object.x + prop.value;
                        if (to < object.x) {
                            aProEdge.setPatrolX(to, object.x);
                        } else {
                            aProEdge.setPatrolX(object.x, to);
                        }
                    }
                    if (prop.name === 'patrolY') {
                        let to = object.y + prop.value;
                        if (to < object.y) {
                            aProEdge.setPatrolY(to-100, object.y-100);
                        } else {
                            aProEdge.setPatrolY(object.y-100, to-100);
                        }
                    }
                    if (prop.name === 'follow' && prop.value === true) {
                        aProEdge.follows = true;
                    }
                    if (prop.name === 'fire') {
                        aProEdge.shoots = true;
                        aProEdge.bottleReload = prop.value;
                    }
                    if (prop.name === 'speed') {
                        aProEdge.speed = prop.value;
                    }
                    if (prop.name === 'garbage') {
                        aProEdge.garbage = prop.value;
                    }
                    if (prop.name === 'fastShot' && prop.value === true) {
                        aProEdge.bottleReload = 2000;
                        aProEdge.bottleReloadVariance = 500;
                    }
                    if (prop.name === 'NNBoss' && prop.value === true) {
                        aProEdge.boss = true;
                    }
                    if (prop.name === 'health') {
                        aProEdge.health = prop.value;
                    }
                });
            }
        });

        // Create a group for bullets
        this.bullets = this.physics.add.group();
        this.attacks = this.physics.add.group();
        this.explosions = this.physics.add.group();
        this.axes = this.physics.add.group();
        this.hearts = this.physics.add.group();

        // Input
        this.cursors = this.input.keyboard.createCursorKeys();

        this.physics.add.collider(this.player, mainLayer);
        // this.physics.add.collider(this.player, objectsLayer);
        // this.physics.add.collider(this.enemies, mainLayer);
        // this.physics.add.collider(this.enemies, objectsLayer);
        // this.physics.add.collider(this.player, this.interactive, touchInteractive);
        this.physics.add.collider(this.player, this.enemies, (player, enemy) => {
            player.takeDamage();
        });
        // this.physics.add.collider(this.player, this.Gulch, (player, zone) => {
        //     this.game.globalState.startNNX = 2400;
        //     this.game.globalState.startNNY = 3350;
        //     this.bgMusic.stop();
        //     this.scene.start('Gulch');
        // });
        // this.physics.add.collider(this.player, this.bossSave, (player, zone) => {
        //     this.game.globalState.startNNX = 2350;
        //     this.game.globalState.startNNY = 550;
        // });

        // function touchInteractive(player, interactive) {
        //     interactive.properties.forEach(prop => {
        //         if (prop.name === 'key' && prop.value === true) {
        //             player.addItemToInventory('key');
        //             interactive.destroy();
        //         }
        //         if (prop.name === 'lock' && prop.value === true) {
        //             if (player.hasItem('key')) {
        //                 player.removeItemFromInventory('key');
        //                 let checkTiles = objectsLayer.getTilesWithin(interactive.x/100-5, interactive.y/100-5, 15, 15);
        //                 checkTiles.forEach(frameTile => {
        //                     if (frameTile.properties.lockedDoor === true) {
        //                         objectsLayer.removeTileAt(frameTile.x, frameTile.y);
        //                     }
        //                     interactive.destroy();
        //                 });
        //             }
        //         }
        //     });
        // };

        // Collision between bullets and enemies
        this.physics.add.collider(this.bullets, this.enemies, (bullet, enemy) => {
            bullet.destroy();
            enemy.takeDamage(1);
        });

        this.physics.add.overlap(this.axes, this.enemies, (axe, enemy) => {
            axe.destroy();
            enemy.takeDamage(1);
        });

        this.physics.add.collider(this.player, this.attacks, (player, attack) => {
            player.takeDamage(attack);
            attack.destroy();
        });

        this.physics.add.collider(this.player, this.explosions, (player, attack) => {
            player.takeDamage(attack);
        });

        this.physics.add.collider(this.player, this.hearts, (player, heart) => {
            player.addHealth(1);
            heart.destroy();
        });

        // Add level heart upgrade
        // if (this.game.globalState.hasInventory('99-heart') === 0) {
        //     this.upgrade = this.physics.add.image(5600, 500, 'heart-upgrade').setScale(0.2);
        //     this.physics.add.collider(this.player, this.upgrade, (player, upgrade) => {
        //         this.game.globalState.addToInventory('99-heart', 1);
        //         this.player.maxHearts ++;
        //         this.player.numHearts ++;
        //         this.game.globalState.maxHearts = this.player.maxHearts;
        //         this.game.globalState.hearts = this.player.numHearts;
        //         this.player.updateHealth();
        //         upgrade.destroy();                
        //     });
        // }

                //this.cameras.main.setScroll(800, 1100);
        this.cameras.main.setBounds(1600, 2700, 1600, 900);
        
        // Background Music
        this.bgMusic = this.sound.add('dungeonMusic', { volume: 0.5 });
        this.bgMusic.loop = true;
        this.bgMusic.play();
    }

    update(time, delta) {
        this.player.update();

        const onBounds = this.cameras.main.getBounds();
        if (this.player.body.y <= onBounds.y) {
            this.cameras.main.setBounds(onBounds.x, onBounds.y-900, 1600, 900);
        }
        if (this.player.body.y >= onBounds.y+900) {
            this.cameras.main.setBounds(onBounds.x, onBounds.y+900, 1600, 900);
        }
        if (this.player.body.x <= onBounds.x) {
            this.cameras.main.setBounds(onBounds.x-1600, onBounds.y, 1600, 900);
        }
        if (this.player.body.x >= onBounds.x+1600) {
            this.cameras.main.setBounds(onBounds.x+1600, onBounds.y, 1600, 900);
        }
    }
}