/*
 * Selector
 */


export default class Selector extends Phaser.Sprite {

    constructor(whole, x = -1, y = -1) {

        // gamestate.selector.sprite = this.game.add.sprite(0, 0, 'selector');
        // gamestate.selector.x = (this.level.width>>1);
        // gamestate.selector.sprite.x = this.level.x + gamestate.selector.x * this.level.tileWidth;
        // gamestate.selector.y = (this.level.height>>1);
        // gamestate.selector.sprite.y =  this.level.y + gamestate.selector.y * this.level.tileHeight;

        super(whole.game, 0, 0, 'selector');

        this.whole = whole;
        this.level = whole.level;
        this.game = whole.game;

        this.whole._controlsgroup.add( this );

        this.grid_x = x;
        this.grid_y = y;
 
        // center it if necessary
        if (this.grid_x == -1) {
            this.grid_x = this.level.width >> 1;
        }
        if (this.grid_y == -1) {
            this.grid_y = this.level.height >> 1;
        }
        this.x = this.level.x + this.level.tileWidth * this.grid_x - this.level._blockgroup.pivot.x;
        this.y = this.level.y + this.level.tileHeight * this.grid_y - this.level._blockgroup.pivot.y  ;

        console.log(this);

        // make sure it is shown
        if (!(this.x > 0) ){
            this.x = 0;
        }
        if (!(this.y > 0) ){
            this.y = 0;
        }

    }

    moveUp()    {this._move(0);}
    moveRight() {this._move(1);}
    moveDown()  {this._move(2);}
    moveLeft()  {this._move(3)}

    _move(direction = 'r') {

        var animdir = {x: this.x, y: this.y};

        if (direction === 0) {
            if (this.grid_y==0) {
                this.whole.isAnimating = false;
                return;
            }
            animdir.y += -this.level.tileHeight;
            this._diffy = 1;
        }
        if (direction === 1) {
            if (this.grid_x == 8) {
                this.whole.isAnimating = false;
                return;
            }
            animdir.x += this.level.tileWidth;
            this._diffx = 1;
        }
        if (direction === 2) {
            if (this.grid_y == 8) {
                this.whole.isAnimating = false;
                return;
            }
            animdir.y += this.level.tileHeight;
            this._diffy = -1;
        }
        if (direction === 3) {
            if (this.grid_y == 0) {
                this.whole.isAnimating = false;
                return;
            }
            animdir.x += -this.level.tileWidth;
            this._diffx = -1;
        }

        // this._diffx = animdir.x;
        // this._diffy = animdir.y;

        console.log(animdir);

        var animate = this.game.add.tween( this );

        animate.to( animdir,
            this.whole.gamesettings.animation.speed,
            this.whole.gamesettings.animation.easing );
 
        animate.onComplete.add(function() {
            // update the block's grid posititon
            if (this._diffx>0) {
                this.grid_x++;
            } else if (this._diffx<0) {
                this.grid_x--;
            }
            if (this._diffy>0) {
                this.grid_y--;
            } else if (this._diffy<0) {
                this.grid_y++;
            }
            this.whole.isAnimating = false;

            // console.log( this.grid_x, this.grid_y );

        }, this);

        animate.start();

        this.whole.sounds.shift.play();
    }

    rotateCW() {
        var xcenter = Math.round(this.level.width / 2) ;
        var ycenter = Math.round(this.level.height / 2) ;

        var x = this.grid_x - xcenter;
        var y = this.grid_y - ycenter;

        console.log(xcenter, ycenter, this.grid_x, this.grid_y);

        this.grid_y = x;
        this.grid_x = -y;

        console.log(xcenter, ycenter, this.grid_x, this.grid_y);
    }

}
