class Start extends Phaser.Scene {

    constructor ()
    {
        super({ key: 'Start', active: true });
        console.log('Version: ' + VERSION);
        this.console = new Console();
        this.console.enable();
    }
    
    preload()
    {
        this.load.audio('click', 'assets/sound/click.mp3?ver=' + VERSION);

        this.load.image('bgrstartback', 'assets/bgrstartback.png?ver=' + VERSION);
        this.load.image('bgrstartlogo', 'assets/bgrstartlogo.png?ver=' + VERSION);
        this.load.image('clicktoplay', '../common/assets/clicktoplay.png?ver=' + VERSION);
        this.load.image('taptoplay', '../common/assets/taptoplay.png?ver=' + VERSION);

        this.load.spritesheet('loading', '../common/assets/loadingyellow.png?ver=' + VERSION, { frameWidth: 744, frameHeight: 27 });

        this.load.json('config', '../ServerTable.json?ver=' + VERSION);

        this.load.scenePlugin({key: 'rexgesturesplugin', url: '../common/js/swipeplugin.js?ver=' + VERSION, sceneKey: 'Gestures'});      
	}

    create ()
    {
        let data = this.cache.json.get('config');
        
        let lang = 'en';//default

        if(data != null)
        {
            lang = data.lang;
        }

        this.bgrstartback = this.add.image(640, 360, 'bgrstartback');
        this.bgrstartlogo = this.add.image(640, 360, 'bgrstartlogo');

        this.loading = this.add.image(640, 630, 'loading');
        
        this.scene.bringToTop('SceneStart');

        this.load.image('bgrmain', 'assets/bgrmain.png?ver=' + VERSION);
        this.load.image('bgrjackpots', 'assets/jackpots_background.png?ver=' + VERSION);
        this.load.image('bgrbar', 'assets/bar.png?ver=' + VERSION);
        this.load.image('logo', 'assets/logo.png?ver=' + VERSION);
        this.load.image('stopper', 'assets/stopper.png?ver=' + VERSION);
        this.load.image('overlay', 'assets/overlay.png?ver=' + VERSION);
        this.load.image('bgrsettings', '../common/assets/bgrsettings.png?ver=' + VERSION);
        this.load.image('bgrsettingsmobile', '../common/assets/bgrsettingsmobile.png?ver=' + VERSION);
        this.load.image('msgstrip', '../common/assets/message_strip.png?ver=' + VERSION);
        this.load.image('coin', '../common/assets/coin.png?ver=' + VERSION);
        this.load.image('winbar', '../common/assets/winbar.png?ver=' + VERSION);

        this.load.image('bgrpaytable', 'assets/paytable.png?ver=' + VERSION);

		this.load.image('gameinfo', '../common/docs/' + lang + '/r5_break_the_lines.png?ver=' + VERSION);
        
        this.load.spritesheet('backToGameClick', '../common/assets/' + lang + '/back_to_game_click.png?ver=' + VERSION,{ frameWidth: 350, frameHeight: 66 });
        this.load.spritesheet('backToGameTap', '../common/assets/' + lang + '/back_to_game_tap.png?ver=' + VERSION,{ frameWidth: 350, frameHeight: 66 });

        this.load.spritesheet('connecting', '../common/assets/connecting.png?ver=' + VERSION, { frameWidth: 696, frameHeight: 86 });

        this.load.bitmapFont('fontPaytable', 'assets/fonts/font_paytable.png?ver=' + VERSION, 'assets/fonts/font_paytable.xml?ver=' + VERSION);
		this.load.bitmapFont('fontPaytableH', 'assets/fonts/font_paytable_h.png?ver=' + VERSION, 'assets/fonts/font_paytable_h.xml?ver=' + VERSION);
        this.load.bitmapFont('totalwinNumbers', 'assets/fonts/totalwin_numbers.png?ver=' + VERSION, 'assets/fonts/totalwin_numbers.xml?ver=' + VERSION);
        this.load.bitmapFont('totalwinLetters', 'assets/fonts/totalwin_letters.png?ver=' + VERSION, 'assets/fonts/totalwin_letters.xml?ver=' + VERSION);
        this.load.bitmapFont('totalLineWinFont', 'assets/fonts/total_line_font.png?ver=' + VERSION, 'assets/fonts/total_line_font.xml?ver=' + VERSION);

        this.load.bitmapFont('barLetters', '../common/fonts/bar_letters.png?ver=' + VERSION, '../common/fonts/bar_letters.xml?ver=' + VERSION);
        this.load.bitmapFont('barNumbers', '../common/fonts/bar_numbers.png?ver=' + VERSION, '../common/fonts/bar_numbers.xml?ver=' + VERSION);
        this.load.bitmapFont('fontJackpotGrand', '../common/fonts/font_jackpot_grand.png?ver=' + VERSION, '../common/fonts/font_jackpot_grand.xml?ver=' + VERSION);
        this.load.bitmapFont('fontJackpotMaxi', '../common/fonts/font_jackpot_maxi.png?ver=' + VERSION, '../common/fonts/font_jackpot_maxi.xml?ver=' + VERSION);
        this.load.bitmapFont('fontJackpotMini', '../common/fonts/font_jackpot_mini.png?ver=' + VERSION, '../common/fonts/font_jackpot_mini.xml?ver=' + VERSION);
        this.load.bitmapFont('fontBet', '../common/fonts/font_bet.png?ver=' + VERSION, '../common/fonts/font_bet.xml?ver=' + VERSION);
        this.load.bitmapFont('fontUnits', '../common/fonts/font_units.png?ver=' + VERSION, '../common/fonts/font_units.xml?ver=' + VERSION);
        this.load.bitmapFont('fontLegalLimitWin', '../common/fonts/legal_limit_win.png?ver=' + VERSION, '../common/fonts/legal_limit_win.xml?ver=' + VERSION);

        this.load.spritesheet('startBtn', '../common/assets/start.png?ver=' + VERSION,{ frameWidth: 182, frameHeight: 183 });
        this.load.spritesheet('betBtn', '../common/assets/bet.png?ver=' + VERSION,{ frameWidth: 182, frameHeight: 183 });
        this.load.spritesheet('infoBtn', '../common/assets/info.png?ver=' + VERSION,{ frameWidth: 136, frameHeight: 136 });
        this.load.spritesheet('takeBtn', '../common/assets/take.png?ver=' + VERSION,{ frameWidth: 182, frameHeight: 182 });
        this.load.spritesheet('stopBtn', '../common/assets/stop.png?ver=' + VERSION,{ frameWidth: 131, frameHeight: 131 });
        this.load.spritesheet('autoStopBtn', '../common/assets/auto_stop.png?ver=' + VERSION,{ frameWidth: 181, frameHeight: 156 });
        this.load.spritesheet('autoCounterBtn', '../common/assets/auto_counter.png?ver=' + VERSION,{ frameWidth: 181, frameHeight: 156 });
        this.load.spritesheet('settingsBtn', '../common/assets/settings.png?ver=' + VERSION,{ frameWidth: 110, frameHeight: 110 });
        this.load.spritesheet('optionsBtn', '../common/assets/options.png?ver=' + VERSION,{ frameWidth: 182, frameHeight: 183 });
        this.load.spritesheet('homeBtn', '../common/assets/home.png?ver=' + VERSION,{ frameWidth: 131, frameHeight: 131 });
        this.load.spritesheet('checkBox', '../common/assets/checkbox.png?ver=' + VERSION,{ frameWidth: 40, frameHeight: 46 });
        this.load.spritesheet('stars', '../common/assets/stars.png?ver=' + VERSION, { frameWidth: 60, frameHeight: 60 });
        this.load.spritesheet('settingsBtnSmall', '../common/assets/settings_small.png?ver=' + VERSION,{ frameWidth: 180, frameHeight: 180 });
        this.load.spritesheet('rulesBtnSmall', '../common/assets/rules_small.png?ver=' + VERSION,{ frameWidth: 180, frameHeight: 180 });

        this.load.image('holdForMaxBet', '../common/assets/hold_for_max_bet.png?ver=' + VERSION);
        this.load.image('holdForAuto', '../common/assets/hold_for_auto.png?ver=' + VERSION);
        
        this.load.audio('reelstop', 'assets/sound/reel_stop.mp3?ver=' + VERSION);
        this.load.audio('reelstopearly', 'assets/sound/reel_earlier_stop.mp3?ver=' + VERSION);

		this.load.audio('intro', 'assets/sound/intro.mp3?ver=' + VERSION);
        this.load.audio('spin', 'assets/sound/spin.mp3?ver=' + VERSION);
        this.load.audio('win', 'assets/sound/win.mp3?ver=' + VERSION);
        this.load.audio('jackpotShow', 'assets/sound/jackpot_show.mp3?ver=' + VERSION);
        this.load.audio('jackpotWin', 'assets/sound/jackpot_win.mp3?ver=' + VERSION);
        this.load.audio('winToCredit', 'assets/sound/win_to_credit.mp3?ver=' + VERSION);
        this.load.audio('winToCreditEnd', 'assets/sound/win_to_credit_end.mp3?ver=' + VERSION);
        this.load.audio('special', 'assets/sound/special.mp3?ver=' + VERSION);
        this.load.audio('special_end', 'assets/sound/special_end.mp3?ver=' + VERSION);
        this.load.audio('credit', 'assets/sound/credit.mp3?ver=' + VERSION);
        
        this.load.spritesheet('cherry', 'assets/reels/cherry.png?ver=' + VERSION,{ frameWidth: 209, frameHeight: 187 });
        this.load.spritesheet('lemon', 'assets/reels/lemon.png?ver=' + VERSION,{ frameWidth: 182, frameHeight: 187 });
        this.load.spritesheet('orange', 'assets/reels/orange.png?ver=' + VERSION,{ frameWidth: 191, frameHeight: 187 });
        this.load.spritesheet('plum', 'assets/reels/plum.png?ver=' + VERSION,{ frameWidth: 187, frameHeight: 188 });
        this.load.spritesheet('bell', 'assets/reels/bell.png?ver=' + VERSION,{ frameWidth: 191, frameHeight: 183 });
        this.load.spritesheet('grape', 'assets/reels/grape.png?ver=' + VERSION,{ frameWidth: 229, frameHeight: 173 });
        this.load.spritesheet('melon', 'assets/reels/melon.png?ver=' + VERSION,{ frameWidth: 192, frameHeight: 188 });
        this.load.spritesheet('star', 'assets/reels/star.png?ver=' + VERSION,{ frameWidth: 224, frameHeight: 209 });                
        this.load.spritesheet('seven', 'assets/reels/7.png?ver=' + VERSION,{ frameWidth: 194, frameHeight: 192 });
        this.load.spritesheet('break', 'assets/reels/break.png?ver=' + VERSION,{ frameWidth: 188, frameHeight: 180 });
        this.load.spritesheet('scatter', 'assets/reels/scatter.png?ver=' + VERSION,{ frameWidth: 224, frameHeight: 209 }); 
        this.load.spritesheet('mystery', 'assets/reels/mystery.png?ver=' + VERSION,{ frameWidth: 184, frameHeight: 160 });
        this.load.spritesheet('break_anim', 'assets/reels/break_anim.png?ver=' + VERSION,{ frameWidth: 244, frameHeight: 236 });

        this.waitConnectionTimer = -1;

        this.nmfiles = 0;

        this.load.start();
        
        this.load.on('fileprogress', this.progressFn, this);
    
        this.load.on('complete', function ()
        {
            this.intro = this.sound.add('intro');
           
            this.main = this.scene.add('Main', Main, false);

	        this.main.start(this);

            this.connectingSprite = this.add.sprite(640, 640, 'connecting').setVisible(false);

            this.connectingAnim = this.anims.create({
                key: 'connectingAnim',
                frames: this.anims.generateFrameNumbers('connecting'),
                frameRate: 5,
                repeat: -1
            });

            setTimeout(this.onProgressComplete.bind(this), 1000);

        },this);
    }

	onProgressComplete()
	{
        this.loading.setVisible(false);
        
        if(!this.sys.game.device.os.desktop)
        {
            this.clicktoplay = this.add.image(640, 650, 'taptoplay');
        }
        else
        {
            this.clicktoplay = this.add.image(640, 650, 'clicktoplay');
        }

        if(!this.main.criticalError)
        {
            this.input.on('pointerup', this.onIntroSound, this);

            this.keyStart = this.input.keyboard.addKey('SPACE');
            this.keyStart.on('down', this.onIntroSound, this);
        }
    }

    progressFn(file, value)
    {
        if(value == 1)
        {
            ++this.nmfiles;

            let v = this.nmfiles / 81;

            this.loading.setFrame(Math.round(v * 13));
        }
    };

    onIntroSound()
    {
        this.input.off('pointerup', this.onIntroSound, this);
        this.keyStart.off('down', this.onIntroSound, this);
        
        this.sound.play("click");
        
        if(this.main.criticalError)
        {
            return;
        }

        if(!this.sys.game.device.os.desktop && this.sys.game.device.fullscreen.available)
        {
            console.log('Fullscreen available');

            this.scale.on('fullscreenunsupported', function() {console.log('fullscreen not supported');});
            this.scale.on('fullscreenfailed', function() {console.log('fullscreen failed');});
            this.scale.on('enterfullscreen', function() {console.log('entered fullscreen');});
            this.scale.startFullscreen();
        }
        else
        {
            console.log('Fullscreen not available');
        }

        this.clicktoplay.setVisible(false);
        this.playtime = 0.0;
        this.intro.play();
    }

    waitForConnection()
    {
        this.connectingSprite.setVisible(true);
        this.connectingSprite.play(this.connectingAnim);
        this.waitConnectionTimer = 30000;
    }

    updateWaitForConnection(delta)
    {
        this.waitConnectionTimer -= delta;
        if(this.waitConnectionTimer < 0)
        {
            this.main.onError('CONNECTION FAILED (WEBSOCKET)');
        }
    }

    update(time, delta) 
    {
        if(this.waitConnectionTimer >= 0)
        {
            this.updateWaitForConnection(delta);
        }

        if(this.hasOwnProperty('playtime') )
        {
            this.playtime += delta;

            if(this.playtime > 3000)
            {
                delete this.playtime;

                this.tweens.add({

                    targets: this.bgrstartlogo,
                    scale: 0,
        
                    ease: 'Linear',
                    duration: 500,
                    onCompleteScope: this,
                    onComplete: function()
                    {
                        this.tweens.killTweensOf(this.bgrstartlogo);
//                        this.bgrstartback.setVisible(false);
                        this.bgrstartlogo.setVisible(false);
                        this.main.startSceneCompleted();
                    }
                });
            }
        }
    }
}

let config = 
{
    type: Phaser.CANVAS,
    width: GAME_WIDTH,
    height: GAME_LANDSCAPE_HEIGHT,
    scale: 
    {
        mode: Phaser.Scale.FIT,
        autoCenter: Phaser.Scale.CENTER_BOTH,
        fullscreenTarget: 'game'
    },
    
    parent: 'game',
    scene: [ Start ]
};


let game = new Phaser.Game(config);
