const saveKey = "DataKey_Cocos_FT11";
import BaseUI from './BaseUI.js'
cc.Class({

	extends: BaseUI,

	properties: {
		audEnter: cc.AudioClip,
		pfbItemAnswer: cc.Prefab,
		pfbItemQuestion: cc.Prefab,
	},

	// 生命周期 onLoad
	onLoad() {
		this._super();
		this.initNodes();
		this.initSceneData();
		this.initSize();
	},
	initNodes() {

	},

	onEnable() {

	},

	onDisable() {

	},

	_resList: null,
	initSceneData() {
		this._resList = [];
		window.GameData = {};
	},

	_designSize: null,  // 设计分辨率
	_frameSize: null,  // 屏幕分辨率
	_mapScaleMin: null,  // 场景中常用缩放（取大值）
	_mapScaleMax: null,  // 场景中常用缩放（取小值）
	_cocosScale: null,  // cocos 自缩放 （较少用到）
	initSize() {

	},


	// 生命周期 start
	start() {
		this.getData((data) => {
			console.log('data:', data);
			this.data = data;
			if (data) {
				this.preloadItem()
			}
		})
	},

	getData(cb) {
		// 获取数据
		if (window.courseware) {
			const getData = window.courseware.getData;
			getData((data) => {
				data = data || this.getDefaultData();
				cb && cb(data);
			}, saveKey);
		} else {
			cb && cb(this.getDefaultData());
		}
	},

	getDefaultData() {
		let dataJson = '{"questionList":[{"questionAudio":"http://staging-teach.cdn.ireadabc.com/d2a9d8815d0370dc3b9aa18987dee584.mp3","questionText":"这是标题文本1","optionList":[{"text":"i am jumping","optionDragonBone":{"skeJsonData":{"url":""},"texJsonData":{"url":""},"texPngData":{"url":""},"skeJsonData2":{"url":""},"texJsonData2":{"url":""},"texPngData2":{"url":""}},"optionAudio":"http://staging-teach.cdn.ireadabc.com/4e267b9c0b58ebb153bfc0ae520cb7fe.mp3"},{"text":"i am rolling","optionDragonBone":{"skeJsonData":{"url":"http://staging-teach.cdn.ireadabc.com/48b884b3c7b3a717f6e4b055f490a1f1.json","name":"walkingfish_ske.json"},"texJsonData":{"url":"http://staging-teach.cdn.ireadabc.com/608b2c5f85297f0b7930325733b7bea9.json","name":"walkingfish_tex.json"},"texPngData":{"url":"http://staging-teach.cdn.ireadabc.com/ecfd66f932659635f5b66642378d826b.png","name":"walkingfish_tex.png"},"skeJsonData2":{"url":""},"texJsonData2":{"url":""},"texPngData2":{"url":""}},"optionAudio":"http://staging-teach.cdn.ireadabc.com/90fd9908a33a3d087ac53e6f7d794c18.mp3"},{"text":"i am dancing","optionDragonBone":{"skeJsonData":{"url":""},"texJsonData":{"url":""},"texPngData":{"url":""},"skeJsonData2":{"url":""},"texJsonData2":{"url":""},"texPngData2":{"url":""}},"optionAudio":""},{"text":"i am drving","optionDragonBone":{"skeJsonData":{"url":""},"texJsonData":{"url":""},"texPngData":{"url":""},"skeJsonData2":{"url":""},"texJsonData2":{"url":""},"texPngData2":{"url":""}},"optionAudio":""}]},{"questionAudio":"","questionText":"这是标题文本2","optionList":[{"text":"标题文本21","optionDragonBone":{"skeJsonData":{"url":""},"texJsonData":{"url":""},"texPngData":{"url":""},"skeJsonData2":{"url":""},"texJsonData2":{"url":""},"texPngData2":{"url":""}},"optionAudio":""},{"text":"标题文本22","optionDragonBone":{"skeJsonData":{"url":""},"texJsonData":{"url":""},"texPngData":{"url":""},"skeJsonData2":{"url":""},"texJsonData2":{"url":""},"texPngData2":{"url":""}},"optionAudio":""},{"text":"标题文本23","optionDragonBone":{"skeJsonData":{"url":""},"texJsonData":{"url":""},"texPngData":{"url":""},"skeJsonData2":{"url":""},"texJsonData2":{"url":""},"texPngData2":{"url":""}},"optionAudio":""}]}]}';
		let data = JSON.parse(dataJson);
		return data;
	},

	preloadItem() {
		this.addPreloadItem();
		this.preload();
	},

	addPreloadItem() {
		for (let i = 0; i < this.data.questionList.length; i++) {
			this._resList.push({ url: this.data.questionList[i].questionAudio });
			for (let j = 0; j < this.data.questionList[i].optionList.length; j++) {
				this._resList.push({ url: this.data.questionList[i].optionList[j].optionAudio });
				this._resList.push({ url: this.data.questionList[i].optionList[j].optionDragonBone.skeJsonData.url });
				this._resList.push({ url: this.data.questionList[i].optionList[j].optionDragonBone.texJsonData.url });
				this._resList.push({ url: this.data.questionList[i].optionList[j].optionDragonBone.texPngData.url });
				this._resList.push({ url: this.data.questionList[i].optionList[j].optionDragonBone.skeJsonData2.url });
				this._resList.push({ url: this.data.questionList[i].optionList[j].optionDragonBone.texJsonData2.url });
				this._resList.push({ url: this.data.questionList[i].optionList[j].optionDragonBone.texPngData2.url });
			}
		}
	},

	preload() {
		cc.assetManager.loadAny(this._resList, null, null, (err, data) => {
			this.loadEnd();
			if (window && window["air"]) {
				window["air"].hideAirClassLoading();
			}

			cc.debug.setDisplayStats(false);
		});
	},


	loadEnd() {
		this.initData();

		this.startGame();
	},

	initData() {
		// 所有全局变量 默认都是null 
		GameData.questionIndex = 0;
		this.initQuestionASpace();
		this.initQuestionAndAnswer();
		this.initTitle();
	},

	startGame() {
		this.playAudioEntrer();
	},

	initQuestionASpace() {
		let optionList = this.data.questionList[GameData.questionIndex].optionList;
		if (optionList.length == 3) {
			this.nodeDict.nodQuestionParent.getComponent(cc.Layout).spacingX = 56;
			this.nodeDict.nodAnswerParent.getComponent(cc.Layout).spacingX = 56;
		} else {
			this.nodeDict.nodQuestionParent.getComponent(cc.Layout).spacingX = 10;
			this.nodeDict.nodAnswerParent.getComponent(cc.Layout).spacingX = 10;
		}
	},

	initTitle() {
		let label = this.nodeDict.labTitle.getComponent(cc.Label);
		label.string = this.data.questionList[GameData.questionIndex].questionText;
		label._forceUpdateRenderData();
		if (this.nodeDict.labTitle.width > 567) {
			this.nodeDict.labTitle.scale = 567 / this.nodeDict.labTitle.width
		}
	},

	// 初始化题干以及选项
	initQuestionAndAnswer() {
		let optionList = this.data.questionList[GameData.questionIndex].optionList;
		for (let i = 0; i < optionList.length; i++) {
			let question = cc.instantiate(this.pfbItemQuestion);
			question.parent = this.nodeDict.nodQuestionParent;
			question.getComponent('ItemQuestion').setInfo(optionList[i], i);

			let answer = cc.instantiate(this.pfbItemAnswer);
			answer.parent = this.nodeDict.nodAnswerParent;
			answer.getComponent('ItemAnswer').setInfo(optionList[i], i);
		}
	},

	playAudioEntrer() {
		GameData.isAudio = true;
		let id = cc.audioEngine.play(this.audEnter, false, 1);
		cc.audioEngine.setFinishCallback(id, () => {
			GameData.isAudio = false;
			this.playAudioQuestion();
		})
	},

	playAudioQuestion() {
		GameData.isAudio = true;
		let url = this.data.questionList[GameData.questionIndex].questionAudio;
		// url = 'http://127.0.0.1/iamflying.mp3';
		cc.assetManager.loadRemote(url, null, (err, clip) => {
			if (err) {
				console.log(err)
				this.showRandomQuestion();
				return;
			}
			let id = cc.audioEngine.play(clip, false, 1);
			cc.audioEngine.setFinishCallback(id, () => {
				GameData.isAudio = false;
				this.showRandomQuestion();
			})
		});
	},

	showRandomQuestion() {

	},

	buttonListener(button) {
		if (button.node.name == 'btnTitle') {
			if (!GameData.isAudio) {
				this.playAudioQuestion();
			}
		}
	},

});
