class Settings extends Phaser.Scene {

    constructor ()
    {
        super({ key: 'Settings', active: true });
    }
    
    preload()
    {
    }

    create(data)
    {
        this.parent = data.scene;
        this.x = data.x;
        this.y = data.y;
        
        this.add.image(this.x, this.y, data.key);
       
        this.scene.sleep();

        this.checkMuteSound = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkMuteSound',
            'state':'checked',
            'up': 0,
            'x': 256,
            'y': 280,
            'extraval': null,
            'onClick': this.onMuteAllSounds
        });
        
        this.checkMuteSound.setChecked(this.parent.conf.allSoundMuted);
        this.checkMuteSound.input.hitArea.setTo(0, 0, 425, 46);


        this.checkBackgroundSound = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkBackgroundSound',
            'state':'checked',
            'up': 0,
            'x': 256,
            'y': 330,
            'extraval': null,
            'onClick': this.onBackgroundSoundChange
        });
        
        this.checkBackgroundSound.setChecked(this.parent.conf.bgrSoundMuted);
        this.checkBackgroundSound.input.hitArea.setTo(0, 0, 575, 46);

        this.checkSettings = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkSettings',
            'state':'checked',
            'up': 0,
            'x': 256,
            'y': 383,
            'extraval': null,
            'onClick': this.onSettingsChange
        });
        
        this.checkSettings.setChecked(this.parent.conf.keySettings);
        this.checkSettings.input.hitArea.setTo(0, 0, 262, 46);

        this.checkSpin = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkSpin',
            'state':'checked',
            'up': 0,
            'x': 256,
            'y': 435,
            'extraval': null,
            'onClick': this.onSpinKeyEnable
        });
        
        this.checkSpin.setChecked(this.parent.conf.keyStart);
        this.checkSpin.input.hitArea.setTo(0, 0, 262, 46);

        this.checkAutoSpin = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkAutoSpin',
            'state':'checked',
            'up': 0,
            'x': 256,
            'y': 486,
            'extraval': null,
            'onClick': this.onAutoSpinKeyEnable
        });
        
        this.checkAutoSpin.setChecked(this.parent.conf.keyAutoStart);
        this.checkAutoSpin.input.hitArea.setTo(0, 0, 262, 46);

        this.checkMaxBet = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkMaxBet',
            'state':'checked',
            'up': 0,
            'x': 493,
            'y': 486,
            'extraval': null,
            'onClick': this.onMaxBetKeyEnable
        });
        
        this.checkMaxBet.setChecked(this.parent.conf.keyMaxBet);
        this.checkMaxBet.input.hitArea.setTo(0, 0, 262, 46);

        this.checkInfo = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkInfo',
            'state':'checked',
            'up': 0,
            'x': 714,
            'y': 435,
            'extraval': null,
            'onClick': this.onInfoKeyEnable
        });
        
        this.checkInfo.setChecked(this.parent.conf.keyInfo);
        this.checkInfo.input.hitArea.setTo(0, 0, 162, 46);

        this.checkExit = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkExit',
            'state':'checked',
            'up': 0,
            'x': 714,
            'y': 486,
            'extraval': null,
            'onClick': this.onExitKeyEnable
        });
        
        this.checkExit.setChecked(this.parent.conf.keyExit);
        this.checkExit.input.hitArea.setTo(0, 0, 162, 46);

        this.checkBet = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkBet',
            'state':'checked',
            'up': 0,
            'x': 493,
            'y': 435,
            'extraval': null,
            'onClick': this.onBetKeyEnable
        });
        
        this.checkBet.setChecked(this.parent.conf.keyBet);
        this.checkBet.input.hitArea.setTo(0, 0, 162, 46);

        this.checkAutoSpin5 = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkAutoSpin5',
            'state':'checked',
            'up': 0,
            'x': 885,
            'y': 315,
            'extraval': 5,
            'onClick': this.onAutoSpinLimit
        });
        
        this.checkAutoSpin5.setChecked( this.parent.conf.autoSpinLimit == 5 );
        this.checkAutoSpin5.input.hitArea.setTo(0, 0, 170, 46);

        this.checkAutoSpin10 = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkAutoSpin10',
            'state':'unchecked',
            'up': 0,
            'x': 885,
            'y': 370,
            'extraval': 10,
            'onClick': this.onAutoSpinLimit
        });
        
        this.checkAutoSpin10.setChecked( this.parent.conf.autoSpinLimit == 10 );
        this.checkAutoSpin10.input.hitArea.setTo(0, 0, 170, 46);

        this.checkAutoSpin20 = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkAutoSpin20',
            'state':'unchecked',
            'up': 0,
            'x': 885,
            'y': 426,
            'extraval': 20,
            'onClick': this.onAutoSpinLimit
        });
        
        this.checkAutoSpin20.setChecked( this.parent.conf.autoSpinLimit == 20 );
        this.checkAutoSpin20.input.hitArea.setTo(0, 0, 170, 46);

        this.checkAutoSpinUnlimited = new Checkbox({
            'scene': this,
            'key':'checkBox',
            'name':'checkAutoSpinUnlimited',
            'state':'unchecked',
            'up': 0,
            'x': 885,
            'y': 482,
            'extraval': -1,
            'onClick': this.onAutoSpinLimit
        });
        
        this.checkAutoSpinUnlimited.setChecked( this.parent.conf.autoSpinLimit == -1 );
        this.checkAutoSpinUnlimited.input.hitArea.setTo(0, 0, 178, 46);

        this.checkAutoSpin5.setGroup([this.checkAutoSpin10, this.checkAutoSpin20, this.checkAutoSpinUnlimited]);
        this.checkAutoSpin10.setGroup([this.checkAutoSpin5, this.checkAutoSpin20, this.checkAutoSpinUnlimited]);
        this.checkAutoSpin20.setGroup([this.checkAutoSpin5, this.checkAutoSpin10, this.checkAutoSpinUnlimited]);
        this.checkAutoSpinUnlimited.setGroup([this.checkAutoSpin5, this.checkAutoSpin10, this.checkAutoSpin20]);
    }

    onMuteAllSounds()
    {
        if(localStorage)
        {
            localStorage.allSoundMuted = this.checkMuteSound.isChecked();
        }

        this.parent.conf.allSoundMuted = this.checkMuteSound.isChecked();

        this.parent.sound.setMute(this.checkMuteSound.isChecked());
    }

    onBackgroundSoundChange()
    {
        if(localStorage)
        {
            localStorage.bgrSoundMuted = this.checkBackgroundSound.isChecked();
        }

        this.parent.conf.bgrSoundMuted = this.checkBackgroundSound.isChecked();

        this.parent.sndBackground.setMute(this.checkBackgroundSound.isChecked());
        this.parent.sndBackgroundFreeGames.setMute(this.checkBackgroundSound.isChecked());
        this.parent.sndBackgroundLonger.setMute(this.checkBackgroundSound.isChecked());
    }

    onSettingsChange()
    {
        if (this.parent.hasOwnProperty('keySettings'))
        {
            this.parent.keySettings.off('down', this.parent.onKbdSettings, this.parent);
            this.parent.keySettings.destroy();
        }

        if(this.checkSettings.isChecked())
        {
            this.parent.keySettings = this.parent.input.keyboard.addKey('S');
            this.parent.keySettings.on('down', this.parent.onKbdSettings, this.parent);
        }

        if(localStorage)
        {
            localStorage.keySettings = this.checkSettings.isChecked();
        }

        this.parent.conf.keySettings = this.checkSettings.isChecked();
    }

    onSpinKeyEnable()
    {
        if (this.parent.hasOwnProperty('keyStart'))
        {
            this.parent.keyStart.off('down', this.parent.onKbdStart, this.parent);
            this.parent.keyStart.destroy();
        }

        if(this.checkSpin.isChecked())
        {
            this.parent.keyStart = this.parent.input.keyboard.addKey('SPACE');
            this.parent.keyStart.on('down', this.parent.onKbdStart, this.parent);
        }

        if(localStorage)
        {
            localStorage.keyStart = this.checkSpin.isChecked();
        }

        this.parent.conf.keyStart = this.checkSpin.isChecked();
    }
    
    onAutoSpinKeyEnable()
    {
        if (this.parent.hasOwnProperty('keyAutoStart'))
        {
            this.parent.keyAutoStart.off('down', this.parent.onKbdAutoStart, this.parent);
            this.parent.keyAutoStart.destroy();
        }

        if(this.checkAutoSpin.isChecked())
        {
            this.parent.keyAutoStart = this.parent.input.keyboard.addKey('A');
            this.parent.keyAutoStart.on('down', this.parent.onKbdAutoStart, this.parent);
        }

        if(localStorage)
        {
            localStorage.keyAutoStart = this.checkAutoSpin.isChecked();
        }

        this.parent.conf.keyAutoStart = this.checkAutoSpin.isChecked();
    }

    onMaxBetKeyEnable()
    {
        if (this.parent.hasOwnProperty('keyMaxBet'))
        {
            this.parent.keyMaxBet.off('down', this.parent.onKbdAutoStart, this.parent);
            this.parent.keyMaxBet.destroy();
        }
        
        if(this.checkMaxBet.isChecked())
        {
            this.parent.keyMaxBet = this.parent.input.keyboard.addKey('M');
            this.parent.keyMaxBet.on('down', this.parent.onKbdMaxBet, this.parent);
        }

        if(localStorage)
        {
            localStorage.keyMaxBet = this.checkMaxBet.isChecked();
        }

        this.parent.conf.keyMaxBet = this.checkMaxBet.isChecked();
    }
    
    onInfoKeyEnable()
    {
        if (this.parent.hasOwnProperty('keyInfo'))
        {
            this.parent.keyInfo.off('down', this.parent.onKbdPaytable, this.parent);
            this.parent.keyInfo.destroy();
        }
        
        if(this.checkInfo.isChecked())
        {
            this.parent.keyInfo = this.parent.input.keyboard.addKey('I');
            this.parent.keyInfo.on('down', this.parent.onKbdPaytable, this.parent);
        }

        if(localStorage)
        {
            localStorage.keyInfo = this.checkInfo.isChecked();
        }

        this.parent.conf.keyInfo = this.checkInfo.isChecked();
    }
    
    onExitKeyEnable()
    {
        if (this.parent.hasOwnProperty('keyExit'))
        {
            this.parent.keyExit.off('down', this.parent.onKbdExit, this.parent);
            this.parent.keyExit.destroy();
        }
        
        if(this.checkExit.isChecked())
        {
            this.parent.keyExit = this.parent.input.keyboard.addKey('E');
            this.parent.keyExit.on('down', this.parent.onKbdExit, this.parent);
        }

        if(localStorage)
        {
            localStorage.keyExit = this.checkExit.isChecked();
        }

        this.parent.conf.keyExit = this.checkExit.isChecked();
    }
    
    onBetKeyEnable()
    {
        if (this.parent.hasOwnProperty('keyBet'))
        {
            this.parent.keyBet.off('down', this.parent.onKbdBet, this.parent);
            this.parent.keyBet.destroy();
        }
        
        if(this.checkBet.isChecked())
        {
            this.parent.keyBet = this.parent.input.keyboard.addKey('B');
            this.parent.keyBet.on('down', this.parent.onKbdBet, this.parent);
        }

        if(localStorage)
        {
            localStorage.keyBet = this.checkBet.isChecked();
        }

        this.parent.conf.keyBet = this.checkBet.isChecked();
    }
   
    onSpinSpeedChange(v)
    {
        if(localStorage)
        {
            localStorage.reelsSpeed = v;
        }

        this.parent.conf.reelsSpeed = v;//medium, 0 - slow, 1 - medium, 2 - fast

        this.parent.reelmanager.setReelsSpeed(v);
    }

    onAutoSpinLimit(v)
    {
        if(localStorage)
        {
            localStorage.autoSpinLimit = v;
        }

        this.parent.conf.autoSpinLimit = v;

        this.parent.setAutoSpinLimit(v);
    }

    wake()
    {
        this.scene.wake();
        this.scene.bringToTop();

        this.checkMuteSound.setChecked(this.parent.conf.allSoundMuted);
        this.checkBackgroundSound.setChecked(this.parent.conf.bgrSoundMuted);
        this.checkSettings.setChecked(this.parent.conf.keySettings);
        this.checkSpin.setChecked(this.parent.conf.keyStart);
        this.checkAutoSpin.setChecked(this.parent.conf.keyAutoStart);
        this.checkMaxBet.setChecked(this.parent.conf.keyMaxBet);
        this.checkInfo.setChecked(this.parent.conf.keyInfo);
        this.checkExit.setChecked(this.parent.conf.keyExit);
        this.checkBet.setChecked(this.parent.conf.keyBet);
        this.checkAutoSpin5.setChecked( this.parent.conf.autoSpinLimit == 5 );
        this.checkAutoSpin10.setChecked( this.parent.conf.autoSpinLimit == 10 );
        this.checkAutoSpin20.setChecked( this.parent.conf.autoSpinLimit == 20 );
        this.checkAutoSpinUnlimited.setChecked( this.parent.conf.autoSpinLimit == -1 );
    }
}

