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

export class ActTwo extends State {
    player:Player;
    pad:Pad;

    amulet:Phaser.Sprite;

    music:Phaser.Sound;

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

    preload() {
        this.game.load.atlasJSONHash('enemy', 'assets/animations/robe/robe.png', 'assets/animations/robe/robe.json');
        this.game.load.atlasJSONHash('player_robe', 'assets/animations/player_robe/player_robe.png', 'assets/animations/player_robe/player_robe.json');
        this.game.load.image('table_bg', 'assets/images/table_bg.png');
        this.game.load.image('amulet_small', 'assets/images/amulet_small.png');
        this.game.load.audio('amulet_music', 'assets/music/amulet.ogg');
        this.game.load.audio('amulet_use', 'assets/sounds/amulet_use.ogg');
        this.game.load.image('dialog_1', 'assets/dialogs/scene_2_0.png');
        this.game.load.image('dialog_2', 'assets/dialogs/scene_2_1.png');
    }

    create() {
        this.game.stage.backgroundColor = '#fff';
        this.game.add.sprite(0, 0, 'table_bg');

        this.amulet = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'amulet_small');
        this.amulet.position.add(-60, -100);

        this.music = this.game.add.audio('amulet_music', 1, true);
        this.music.play();

        var amuletUseSound = this.game.add.audio('amulet_use');

        // setup input
        this.game.input.gamepad.start();
        this.pad = new Pad(this.game.input.gamepad.pad1);

        // setup player
        var robe = this.game.add.sprite(0, 180, 'enemy', '05.png');
        var player_robe = this.game.add.sprite(0, 180, 'player_robe', '05.png');
        player_robe.visible = false;

        this.player = new Player(robe, this.pad);
        this.player.setupRobeAnimations(robe);
        this.player.sprite.position.setTo(0, this.game.world.height / 2);
        this.player.sprite.scale.setTo(2.5, 2.5);

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

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

        var alreadyPickedUp = false;
        this.pad.onThree = ()=> {
            if (!alreadyPickedUp) {
                var amuletPosition = new Phaser.Point(this.amulet.x, this.amulet.y);
                amuletPosition.x += this.amulet.width / 2;
                amuletPosition.y += this.amulet.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 = amuletPosition.distance(playerPositionFeet);
                if (distance < 100) {
                    alreadyPickedUp = true;
                    var sequence = new Sequence(this.game);
                    sequence.chain(0, ()=> {
                        this.dialog1.visible = true;
                    }).then(()=> {
                        return sequence.chain(4000, ()=> {
                            this.dialog1.visible = false;
                            this.dialog2.visible = true;
                        });
                    }).then(()=> {
                        return sequence.chain(4000, ()=> {
                            this.dialog2.visible = false;
                        });
                    });
                    this.amulet.visible = false;
                    robe.visible = false;
                    player_robe.visible = true;
                    var previousPosition = new Phaser.Point(this.player.sprite.position.x, this.player.sprite.y);
                    this.player.sprite = player_robe;
                    this.player.setupPlayerRobeAnimations(player_robe);
                    this.player.sprite.scale.setTo(2.5, 2.5);
                    this.player.sprite.position.setTo(previousPosition.x, previousPosition.y);
                    this.player.sprite.play('down');
                    this.player.sprite.animations.stop();
                    this.music.fadeTo(500, 0);
                    amuletUseSound.volume = 0;
                    amuletUseSound.play();
                    amuletUseSound.fadeTo(500, 1);
                    this.game.time.events.add(8000, ()=> {
                        this.game.state.start('ritual');
                    });
                }
            }
        };
    }

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

    update() {
        this.pad.update();
    }
}