// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
// cc.audioEngine = require('../components/karaoke/scripts/audioEngineEx.js')

cc.Class({
	extends: cc.Component,

	properties: {
		baseWidth: {
			default: 1280,
			type: cc.Integer
		},
		baseHeight: {
			default: 720,
			type: cc.Integer
		}
		// foo: {
		//     // ATTRIBUTES:
		//     default: null,        // The default value will be used only when the component attaching
		//                           // to a node for the first time
		//     type: cc.SpriteFrame, // optional, default is typeof default
		//     serializable: true,   // optional, default is true
		// },
		// bar: {
		//     get () {
		//         return this._bar;
		//     },
		//     set (value) {
		//         this._bar = value;
		//     }
		// },
	},

	// LIFE-CYCLE CALLBACKS:

	// baseWidth: 1280;
	// baseHeight: 720;

	onLoad() {
		cc.debug.setDisplayStats(false);
		this.initSceneData();
		this.initSize();
		window['ss'] = this;
	},

	initSceneData() {
		this._imageResList = [];
		this._audioResList = [];
	},

	_designSize: null,
	initSize() {
		this._designSize = cc.view.getDesignResolutionSize();

	},

	start() {
		let getData = this.getData.bind(this);
		if (window && window.courseware) {
			// console.log('has window');
			getData = window.courseware.getData;
		}
		getData((data) => {
			console.log('data:', data);
			this.data = data || this.getDefaultData();
			cc.director.emit('GAME_DATA_FETCHED', {...this.data})
			this.preloadItem()
		})
	},


	_imageResList: null,
	_audioResList: null,
	_animaResList: null,
	preloadItem() {
		this.addPreloadImage();
		this.addPreloadAudio();
		this.preload();
	},

	getData(func) {
		func(this.getDefaultData());
	},

	getDefaultData() {

		const data = {"title":"aaaaa","pic_url":"http://staging-teach.cdn.ireadabc.com/7324245447e15cca42e0d4c88a32f88a.jpg","accompany_audio_url":"http://staging-teach.cdn.ireadabc.com/4a59032f9b5f726cb2cde2178a7f51fc.mp3","lrcData":{"audio_url":"http://staging-teach.cdn.ireadabc.com/1d1184717ebff259c72fedcb6b249431.mp3","fontSize":50,"lineHeight":100,"lyrics":[{"time":11.09,"data":"yi shan yi shan liang jing jing","newLine":true},{"time":14.9,"data":"满天都是小星星","newLine":true},{"time":19.31,"data":"挂在天上放光明","newLine":true},{"time":23.62,"data":"好像许多小眼睛","newLine":true},{"time":27.93,"data":"一闪一闪亮晶晶","newLine":true},{"time":32.32,"data":"满天都是小星星","newLine":true},{"time":41.1,"data":"一闪一闪亮晶晶","newLine":true},{"time":45.45,"data":"满天都是小星星","newLine":true},{"time":49.74,"data":"挂在天上放光明","newLine":true},{"time":54.16,"data":"好像许多小眼睛","newLine":true},{"time":58.39,"data":"一闪一闪亮晶晶","newLine":true},{"time":62.9,"data":"满天都是小星星","newLine":true},{"time":76.36,"data":"一闪一闪亮晶晶","newLine":true},{"time":80.28,"data":"满天都是小星星","newLine":true},{"time":84.65,"data":"挂在天上放光明","newLine":true},{"time":88.92,"data":"好像许多小眼睛","newLine":true},{"time":93.41,"data":"一闪一闪亮晶晶","newLine":true},{"time":97.72,"data":"满天都是小星星","newLine":true}]},"title_audio_url":"http://staging-teach.cdn.ireadabc.com/39065c5f63a31bd1433c1a046e407a98.mp3"}

		
		// if (data.picArr) {
		// 	data.picArr = data.picArr.concat(data.picArr);
		// 	data.picArr = data.picArr.concat(data.picArr);
		// 	data.picArr = data.picArr.concat(data.picArr);
		// 	data.picArr = data.picArr.concat(data.picArr);
		// 	data.picArr = data.picArr.concat(data.picArr);
		// }
		return data;
	},

	preload() {

		const preloadArr = this._imageResList.concat(this._audioResList);
		cc.assetManager.preloadAny(preloadArr, null, null, (err, data) => {
			console.log(' preload data, ', data);

			// typeof
			this.loadEnd();
			if (window && window["air"]) {
				window["air"].hideAirClassLoading();
			}
		});
	},

	addPreloadImage() {

		this._imageResList.push({ url: this.data.pic_url });


		// if (!this.data.hotZoneItemArr) {
		// 	return;
		// }
		// this.data.hotZoneItemArr.forEach((item) => {
		// 	if (item.pic_url) {
		// 		this._imageResList.push({ url: item.pic_url });
		// 	}
		// })
	},

	addPreloadAudio() {

		// if (!this.data.hotZoneItemArr) {
		// 	return;
		// }
		// this.data.hotZoneItemArr.forEach((item) => {
		// 	this._audioResList.push({url: item.audio_url});
		// });
		// if (this.data.lrcData && this.data.lrcData.audio_url) {
		// 	this._audioResList.push({url: this.data.lrcData.audio_url });
		// }	
		// if (this.data.accompany_audio_url) {
		// 	this._audioResList.push({url: this.data.accompany_audio_url});
		// }

		this._audioResList.push({url: this.data.accompany_audio_url});
		this._audioResList.push({url: this.data.lrcData.audio_url});
		this._audioResList.push({url: this.data.title_audio_url});
		
	},

	loadEnd(preloadedData) {
		// this.initView();
		cc.director.emit('GAME_RESOURCE_LOADED', preloadedData);
	},

	initView() {
		// this.initBg();
		// this.initHotZone();
		// this.initBottomPart();
		// this.initMusic();
	},

	// initMusic() {
	// 	cc.assetManager.loadRemote(this.data.bg_audio_url, (err, clip) => {
	// 		cc.audioEngine.setMusicVolume(0.3);
	// 		const audioID = cc.audioEngine.playMusic(clip, true);
	// 	});
	// },

	_cocosScale: null,
	
	addPicAudio(picNode) {
		const data = picNode.data;
		
		cc.assetManager.loadRemote(data.audio_url, (err, audioClip) => {
			picNode.audioClip = audioClip;
		});

		picNode.addComponent(cc.Button);
		picNode.on('click', () => {
				if (picNode.audioClip) {
					cc.audioEngine.stopAllEffects();
					cc.audioEngine.playEffect(picNode.audioClip, false, 0.8);
				}
		});
	},


	// update (dt) {},
});
