/* eslint-disable no-undef */
const GAME_INIT = (cbFN) => {
	var settings = window._CCSettings;
	// window._CCSettings = undefined;
	var onProgress = null;

	var RESOURCES = cc.AssetManager.BuiltinBundleName.RESOURCES;
	var INTERNAL = cc.AssetManager.BuiltinBundleName.INTERNAL;
	var MAIN = cc.AssetManager.BuiltinBundleName.MAIN;

	var jsList = settings.jsList

	if (jsList) {
		jsList = jsList.map(function (x) {
			return './static/cocos-build/web-mobile/src/' + x
		})
	}

	var option = {
		id: 'GameCanvas',
		debugMode: settings.debug ? cc.debug.DebugMode.INFO : cc.debug.DebugMode.ERROR,
		showFPS: settings.debug,
		frameRate: 60,
		groupList: settings.groupList,
		collisionMatrix: settings.collisionMatrix,
	}

	// init assets
	cc.AssetLibrary.init({
		bundleVers: settings.bundleVers,
		remoteBundles: settings.remoteBundles,
		server: settings.server
	})

	var bundleRoot = [INTERNAL];
	settings.hasResourcesBundle && bundleRoot.push(RESOURCES);

	var count = 0;
	function cb(err) {
		if (err) return console.error(err.message, err.stack);
		count++;
		if (count === bundleRoot.length + 1) {
			cc.assetManager.loadBundle(MAIN, function (err) {
				// if (!err) cc.game.run(option, onStart);
				if (!err)
					cc.game.run(option, () => {
						cbFN && cbFN()
					});
			});
		}
	}

	cc.assetManager.loadScript(settings.jsList.map(function (x) { return 'src/' + x; }), cb);

	for (var i = 0; i < bundleRoot.length; i++) {
		cc.assetManager.loadBundle(bundleRoot[i], cb);
	}

	// cc.game.run(option, () => {
	// 	cb && cb()
	// })
}

export default GAME_INIT
