class Button extends Phaser.GameObjects.Sprite
 {
    constructor(config)
    {
        if (!config.scene)
        {
            console.log('missing scene');
            return;
        }
        
        if (!config.key)
        {
            console.log("missing key!");
            return;
        }

        super(config.scene, 0 + config.horizontalOffset, 0 + config.vDelta, config.key, config.up);

        this.parent = config.scene;
        this.up = config.up;
        this.down = config.down;
        this.over = config.over;
        this.x = config.x;
        this.y = config.y;
        this.alpha = 0.8;

        this.downClicked = false;

        this.parent.add.existing(this);

        this.onClick = config.onClick;
        if(config.onClick != null)
        {
            this.onClick = config.onClick.bind(config.handlerScene);
        }

        this.onPressed = null;
        if(config.onPressed != null)
        {
            this.onPressed = config.onPressed.bind(config.handlerScene);
        }

        this.repeatTime = 1000;
        this.repeatTimer = 0;

        this.setInteractive();

        this.on('pointerdown', this.onDown, this);
        this.on('pointerup', this.onUp, this);
        this.on('pointerover', this.onOver, this);
        this.on('pointerout', this.onUp, this);

        this.disabled = false;
    }

    enable_()
    {
        this.disabled = false;
    }

    disable_()
    {
        this.disabled = true;
    }

    setPosition_(x, y)
    {
        this.setPosition(x, y);
    }
    
    setFrame_(v)
    {
        switch(v)
        {
            case this.down:
                {
                    this.setFrame(this.down);
                    this.alpha = 0.8;
                };break;
            case this.over:
                {
                    this.setFrame(this.over);
                    this.alpha = 0.9;
                };break;

            case this.up:
                {
                    this.setFrame(this.up);
                    this.alpha = 0.8;
                };break;
        }
    }

    getFrame()
    {
        return this.frame.name;
    }

    onDown()
    {
        if(this.disabled)
        {
            return;
        }

        this.setFrame(this.down);
        this.alpha = 0.8;
        this.downClicked = true;
    }
    
    onOver()
    {
        if(this.disabled)
        {
            return;
        }

        if(this.downClicked)
        {
            return;
        }

        this.setFrame(this.over);
        this.alpha = 0.90;
    }
    
    onUp()
    {
        if(this.disabled)
        {
            return;
        }

        this.setFrame(this.up);
        this.alpha = 0.8;
        
        if(this.downClicked)
        {
            if(this.repeatTimer <= this.repeatTime)
            {
                if(this.onClick != null)
                {
                    this.onClick();
                }
            }

            this.parent.sound.play("click");
        }                
        
        this.downClicked = false;
        this.repeatTimer = 0;
    }

    onPressedComplete()
    {
        this.setFrame(this.up);
        this.alpha = 0.8;
        
        if(this.downClicked)
        {
            this.parent.sound.play("click");
        }                
        
        this.downClicked = false;
        this.repeatTimer = 0;
    }

    update(time, delta)
    {
        if(this.downClicked)
        {
            this.repeatTimer += delta;
            if(this.repeatTimer > this.repeatTime)
            {
                if(this.onPressed != null)
                {
                    this.onPressed();
                }
            }
        }
    }
}