import State = Phaser.State;
import Game = Phaser.Game;
import {Pad} from "../input/Pad";
import {ActTwo} from "./ActTwo";
import {Player} from "../entities/Player";
import {Robe} from "../entities/Robe";
import {ActOneHammerTime} from "./ActOneHammerTime";
import {Sequence} from "../manager/Sequence";

export class ActOne extends State {
    pad:Pad;
    actTwo:ActTwo;
    actHammer:ActOneHammerTime;
    enemy:Robe;
    cave:Phaser.Sprite;
    player:Player;
    background:Phaser.Sprite;
    background_collision:Phaser.Sprite;
    torch_1:Phaser.Sprite;
    torch_2:Phaser.Sprite;

    dialog1:Phaser.Sprite;
    dialog2:Phaser.Sprite;

    music:Phaser.Sound;

    preload() {
        this.game.load.atlasJSONHash('player', 'assets/animations/player/player.png', 'assets/animations/player/player.json');
        this.game.load.atlasJSONHash('enemy', 'assets/animations/robe/robe.png', 'assets/animations/robe/robe.json');
        this.game.load.atlasJSONHash('torch', 'assets/animations/torch/torch.png', 'assets/animations/torch/torch.json');
        this.game.load.image('cave', 'assets/images/cave_entrance.png');
        this.game.load.image('background', 'assets/images/scene_01.png');
        this.game.load.image('background_collision', 'assets/images/scene_01_collision.png');
        this.game.load.audio('sneak_music', 'assets/music/sneak.ogg');
        this.game.load.image('dialog_1', 'assets/dialogs/scene_1_0.png');
        this.game.load.image('dialog_2', 'assets/dialogs/scene_1_1.png');
    };

    create() {
        this.game.input.gamepad.start();
        this.pad = new Pad(this.game.input.gamepad.pad1);
        this.music = this.game.add.audio('sneak_music');
        this.music.play();

        this.background = this.game.add.sprite(0, 0, 'background');
        this.background_collision = this.game.add.sprite(0, 25, 'background_collision');
        this.background_collision.visible = false;
        this.torch_1 = this.game.add.sprite(205, 50, 'torch');
        this.torch_2 = this.game.add.sprite(505, 50, 'torch');
        this.torch_1.animations.add('idle_flame_1', ['1.png', '2.png'], 2, true, false);
        this.torch_2.animations.add('idle_flame_2', ['2.png', '1.png'], 2, true, false);
        this.enemy = new Robe(this.game.add.sprite(480, 200, 'enemy', '03.png'));

        this.cave = this.game.add.sprite(310, 108, 'cave');
        this.cave.scale.setTo(0.4, 0.4);
        this.player = new Player(this.game.add.sprite(100, 500, 'player', '01.png'), this.pad);
        this.player.sprite.play('down');
        this.player.sprite.animations.stop();
        //140, 132
        this.torch_1.scale.setTo(2.5, 2.5);
        this.torch_2.scale.setTo(2.5, 2.5);

        this.torch_1.play('idle_flame_1');
        this.torch_2.play('idle_flame_2');

        this.pad.onRightHold = ()=> {
            this.player.sprite.body.velocity.x = 200;
        };
        this.pad.onLeftHold = ()=> {
            this.player.sprite.body.velocity.x = -200;
        };
        this.pad.onUpHold = ()=> {
            this.player.sprite.body.velocity.y = -200;
        };
        this.pad.onDownHold = ()=> {
            this.player.sprite.body.velocity.y = 200;
        };
        this.pad.onThree = ()=> {
            var enemyPositionFeet = new Phaser.Point(this.enemy.sprite.x, this.enemy.sprite.y);
            enemyPositionFeet.x += this.enemy.sprite.width / 2;
            enemyPositionFeet.y += this.enemy.sprite.height;

            var playerPositionFeet = new Phaser.Point(this.player.sprite.position.x, this.player.sprite.position.y);
            playerPositionFeet.x += this.player.sprite.width / 2;
            playerPositionFeet.y += this.player.sprite.height;

            var distance = enemyPositionFeet.distance(playerPositionFeet);
            if (distance < 50) {
                this.game.state.add('actHammer', new ActOneHammerTime(), false);
                this.game.state.start('actHammer');
            }
        };

        this.game.physics.enable(this.player.sprite, Phaser.Physics.ARCADE);
        this.game.physics.enable(this.enemy.sprite, Phaser.Physics.ARCADE);
        this.game.physics.enable(this.cave, Phaser.Physics.ARCADE);
        this.game.physics.enable(this.background_collision, Phaser.Physics.ARCADE);
        this.game.physics.startSystem(Phaser.Physics.ARCADE);

        (<Phaser.Physics.Arcade.Body>this.background_collision.body).immovable = true;
        (<Phaser.Physics.Arcade.Body>this.enemy.sprite.body).immovable = true;
        (<Phaser.Physics.Arcade.Body>this.cave.body).immovable = true;
        (<Phaser.Physics.Arcade.Body>this.cave.body).setSize(200, 50, 25, 65);
        (<Phaser.Physics.Arcade.Body>this.enemy.sprite.body).setSize(20, 20, 5, 5);
        (<Phaser.Physics.Arcade.Body>this.player.sprite.body).collideWorldBounds = true;

        this.dialog1 = this.game.add.sprite(0, this.game.world.height, 'dialog_1');
        this.dialog1.anchor.setTo(0, 1);

        this.dialog2 = this.game.add.sprite(0, this.game.world.height, 'dialog_2');
        this.dialog2.anchor.setTo(0, 1);
        this.dialog2.visible = false;

        var sequence = new Sequence(this.game);
        sequence.chain(5000, ()=>{
            this.dialog1.visible = false;
        }).then(()=>{
            return sequence.chain(0, ()=>{
                this.dialog2.visible = true;
            });
        }).then(()=>{
            return sequence.chain(4000, ()=>{
                this.dialog2.visible = false;
            });
        });

    };

    shutdown() {
        this.music.stop();
    }

    update() {

        this.player.sprite.body.velocity.x = 0;
        this.player.sprite.body.velocity.y = 0;

        this.pad.update();

        this.game.physics.arcade.collide(this.player.sprite, this.background_collision);
    };
}
