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

export class ActOneHammerTime extends State {
    pad:Pad;
    actOne:ActOne;
    enemy:Robe;
    enemyLookingPlayer:Robe;
    player:Player;
    background:Phaser.Sprite;
    background_collision:Phaser.Sprite;
    isTrue:boolean;

    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.image('background', 'assets/images/scene_01.png');
        this.game.load.image('background_collision', 'assets/images/scene_01_collision.png');
        this.game.load.audio('klotz_sound', 'assets/sounds/klotz.ogg');
    };

    create() {
        this.game.input.gamepad.start();
        this.pad = new Pad(this.game.input.gamepad.pad1);

        this.background = this.game.add.sprite(-400, -300, 'background');
        this.background.scale.setTo(1.5, 1.5);

        this.player = new Player(this.game.add.sprite(170, 250, 'player', '01.png'), this.pad);
        this.player.pad.onStopHold = ()=> {
        };
        this.player.enabled = false;
        this.player.sprite.scale.setTo(4, 4);

        this.enemyLookingPlayer = new Robe(this.game.add.sprite(150, 250, 'enemy', '03.png'));
        this.enemyLookingPlayer.sprite.visible = false;
        this.enemyLookingPlayer.sprite.scale.setTo(4, 4);

        this.enemy = new Robe(this.game.add.sprite(150, 250, 'enemy', '03.png'));
        this.enemy.sprite.scale.setTo(4, 4);

        var sequence = new Sequence(this.game);

        var klotz = this.game.add.audio('klotz_sound');

        sequence.chain(0, ()=> {
            this.player.sprite.position.x -= 60;
            this.player.sprite.position.y -= 60;
            this.player.sprite.play('hammer');
            sequence.chain(500, ()=>{
                klotz.play();
            });
        }).then(()=> {
            return sequence.chain(1000, ()=> {
                this.player.sprite.play('down');
                this.player.sprite.position.x += 100;
                this.player.sprite.animations.stop();
                this.enemy.sprite.play('fall');
            });
        }).then(()=> {
            return sequence.chain(1000, ()=> {
                this.enemy.sprite.play('naked');
                this.player.sprite.visible = false;
                this.enemyLookingPlayer.sprite.visible = true;
            });
        }).then(()=>{
            return sequence.chain(3000, ()=>{
                this.game.state.start('actTwo');
            });
        });
    };


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