/**
 * Created by renjianfeng on 2020-03-13.
 */

const customId = 'playWithPlane';

(async function () {
	let customModule = await fetch(`../meta.json`);
	customModule = await customModule.json();
	console.log(customModule);

	await loadAssets(customModule.assets);
	launchWithCustomModule(customModule);
})();

function launchWithCustomModule(customModule) {
	//engine.registerCustomCodeModule(customModule);
	engine.registerCustomModule(customId, window[customId]);

	const { props: propsOption, assets } = customModule;
	let props = engine.computeProps(customModuleProps, propsOption);
	const customModuleIns = {
		id: customId,
		props,
		assets,
	};

	engine.registerCustomModules([customModuleIns]);
	engine.launchWithConfig({
		options: {
			entrySceneView: 'entry',
		},
		assets: [],
		views: [{
			name: 'entry',
			type: 'node',
			properties: {
				x: 0,
				y: 0,
			}
		}],
		customs: [],
	}, null, function () {
		setTimeout(() => {
			engine.addCustomModule(customId, engine.gameStage.sceneContainer.getChildAt(0));
		}, 100);

		setTimeout(() => {
			engine.globalEvent.dispatchEvent('pictures-start', {
				picUrl: "//yun.duiba.com.cn/aurora/assets/57725dcf8cb23905afb87f42d7aba2ffa4c8d2d1.png",
				blockUrl: "308742a0-0ea2-4610-b34a-a230add82021",
			});

			// const d = engine.gameStage.sceneContainer.getChildAt(0);
			// engine.gameStage.sceneContainer.getChildAt(0).x = (d.stage.width-props.W)/2;
			// engine.gameStage.sceneContainer.getChildAt(0).y = (d.stage.height-props.H)/2;
		}, 1000);

		// setTimeout(() => {
		// 	engine.globalEvent.dispatchEvent('pictures-start', {
		// 		picUrl: "http://yun.duiba.com.cn/aurora/assets/e1593b97c27077b85b92f7eaaeae1ed64a1eb79a.png",
		// 		// picUrl: "http://yun.duiba.com.cn/aurora/assets/d23e73d37ec01931e48cbd0a4095367044c5675c.png"
		// 		blockUrl: "888"
		// 	});

		// }, 30*1000);
	});
	engine.globalEvent.addEventListener('pictures-time-update', (e) => {
		// console.log(e.type, e.data);
	});
	engine.globalEvent.addEventListener('pictures-game-fail', (e) => {
		console.log(e.type, e.data);
	});
	engine.globalEvent.addEventListener('pictures-game-success', (e) => {
		console.log(e.type, e.data);
	});
}

function getAssetByUUID(uuid) {
	return engine.resolveCustomAsset(customId, uuid);
}

function getProps() {
	return engine.getProps(customId);
}
