export class Pad {
    constructor(private pad:Phaser.SinglePad) {
        this.setupKeyboardHandling();
        this.setupGamePadHandling();
    }

    private setupGamePadHandling() {
        this.pad.onAxisCallback = (pad:Phaser.SinglePad, vertical:number, first:number)=> {
            if (!vertical && first < 0) {
                this.onLeft();
            }
            if (!vertical && first > 0) {
                this.onRight();
            }
            if (vertical && first < 0) {
                this.onUp();
            }
            if (vertical && first > 0) {
                this.onDown();
            }

            if (vertical === 0 && first === 0) {
                this.onStop();
            }

            this.onAnyArrow();
        };

        this.pad.onDownCallback = (button:number) => {
            switch (button) {
                case 0: //button 1
                    this.onOne();
                    break;
                case 1:
                    this.onTwo();
                    break;
                case 2:
                    this.onThree();
                    break;
                case 3:
                    this.onFour();
                    break;
            }
        };
    };

    private setupKeyboardHandling() {
        this.pad.game.input.keyboard.onUpCallback = ()=> {
            this.onStop();
        };

        this.pad.game.input.keyboard.onDownCallback = (keyboardEvent:KeyboardEvent)=> {
            switch (keyboardEvent.keyCode) {
                case Phaser.Keyboard.UP:
                    this.onUp();
                    break;
                case Phaser.Keyboard.DOWN:
                    this.onDown();
                    break;
                case Phaser.Keyboard.LEFT:
                    this.onLeft();
                    break;
                case Phaser.Keyboard.RIGHT:
                    this.onRight();
                    break;
                case Phaser.Keyboard.Q:
                    this.onOne();
                    break;
                case Phaser.Keyboard.W:
                    this.onTwo();
                    break;
                case Phaser.Keyboard.E:
                    this.onThree();
                    break;
                case Phaser.Keyboard.R:
                    this.onFour();
                    break;
            }

            switch (keyboardEvent.keyCode) {
                case Phaser.Keyboard.UP:
                case Phaser.Keyboard.DOWN:
                case Phaser.Keyboard.LEFT:
                case Phaser.Keyboard.RIGHT:
                    this.onAnyArrow();
                    break;
            }
        };
    };

    update = ()=> {
        var isLeft = this.pad.game.input.keyboard.isDown(Phaser.Keyboard.LEFT) || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -0.1;
        var isRight = this.pad.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > 0.1;
        var isUp = this.pad.game.input.keyboard.isDown(Phaser.Keyboard.UP) || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -0.1;
        var isDown = this.pad.game.input.keyboard.isDown(Phaser.Keyboard.DOWN) || this.pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > 0.1;

        if (isLeft) {
            this.onLeftHold();
        }

        if (isRight) {
            this.onRightHold();
        }

        if (isUp) {
            this.onUpHold();
        }

        if (isDown) {
            this.onDownHold();
        }

        if (!isLeft && !isRight && !isUp && !isDown) {
            this.onStopHold();
        }
    };

    onAnyArrow = ()=> {
    };
    onStop = ()=> {
    };

    onUp = ()=> {
    };
    onDown = ()=> {
    };
    onLeft = ()=> {
    };
    onRight = ()=> {
    };
    onOne = ()=> {
    };
    onTwo = ()=> {
    };
    onThree = ()=> {
    };
    onFour = ()=> {
    };

    onStopHold = ()=> {
    };
    onUpHold = ()=> {
    };
    onDownHold = ()=> {
    };
    onLeftHold = ()=> {
    };
    onRightHold = ()=> {
    };
}