window.PlayOnline = function( filePath = "", emulatorJsConfig = {} ){

    return {

        fileUrl: filePath,
        emuConfig: emulatorJsConfig,

        init(){
            this.launchEmulatorJs();
        },

        cleanEmulatorJsVars() {
            ['EJS_player','EJS_core','EJS_gameUrl','EJS_pathtodata',
               'EJS_startOnLoaded','EJS_threads']
                .forEach(k => delete window[k]);
        },

        prepareEmulatorJs(){
            window.EJS_player = '#game';
            window.EJS_core = this.emuConfig.core;
            window.EJS_gameUrl = this.filePath;
            window.EJS_pathtodata = "https://cdn.emulatorjs.org/stable/data/";
            window.EJS_startOnLoaded = true;
            window.EJS_threads = this.emuConfig.threads ?? false;
        },

        launchEmulatorJs(){

            this.cleanEmulatorJsVars();
            this.prepareEmulatorJs();

            const script   = document.createElement('script');
            script.id      = 'ejs-loader';
            script.src     = 'https://cdn.emulatorjs.org/stable/data/loader.js';
            document.body.appendChild(script);

            this.launchGame = true;

        }

    }

}
