class Checkbox 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, 0, 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.name = config.name;
        this.extraval = config.extraval;

        this.group = null;

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

        if (config.state)
        {
            if (config.state !== 'checked')
            {
                this.alpha = 0.0001;
            }
        }

        this.parent.add.existing(this);

        this.setInteractive();

        this.on('pointerup', this.onUp, this);

    }
    
    setGroup(group)
    {
        this.group = group;
    }

    setCbAlpha(alpha)
    {
        this.setAlpha(alpha);
    }

    setChecked(v)
    {
        if(v)
        {
            this.setCbAlpha(0.8);
        }
        else
        {
            this.setCbAlpha(0.0001);
        }
    }
    
    isChecked()
    {
        return (this.alpha == 1.0)?true:false;
    }

    onUp()
    {
        if (this.group != null)
        {
            this.setAlpha(1.0);

            for (let i = 0; i < this.group.length; ++i)
            {
                this.group[i].setCbAlpha(0.0001);
            }
        }
        else
        {
            let alpha = this.alpha;
            
            if (alpha == 0.0001)
            {
                alpha = 1.0;
            }
            else
            {
                alpha = 0.0001;
            }
            
            this.setAlpha(alpha);
        }

        if(this.onClick != null)
        {
            this.onClick(this.extraval);
        }

        this.parent.sound.play("click");
    }
}