import { onHomeworkFinish, RandomInt, playAudioByUrl, loadDragonBones, getSpriteFrimeByUrl } from "../script/util";
import { defaultData } from "../script/defaultData";
import { assign, createMachine, interpret, actions, forwardTo, matchesState  } from "../script/xstate";
const { pure , send, raise, sendParent  } = actions;


cc.Class({

	extends: cc.Component,

	properties: {
		/*ant: {
			default: null,
			type: cc.Node,
		},
		apple: {
			default: null,
			type: cc.Node,
		},
		bird: {
			default: null,
			type: cc.Node,
		},
		car: {
			default: null,
			type: cc.Node,
		},
		cat: {
			default: null,
			type: cc.Node,
		},
		cow: {
			default: null,
			type: cc.Node,
		},
		duck: {
			default: null,
			type: cc.Node,
		},
		egg: {
			default: null,
			type: cc.Node,
		},
		farmer: {
			default: null,
			type: cc.Node,
		},
		fish: {
			default: null,
			type: cc.Node,
		},
		girl: {
			default: null,
			type: cc.Node,
		},
		hen: {
			default: null,
			type: cc.Node,
		},
		house: {
			default: null,
			type: cc.Node,
		},*/
		rightAudio: {
			default: null,
			type: cc.AudioClip,
		},
		wrongAudio: {
			default: null,
			type: cc.AudioClip,
		},
		logoCat: {
			default: null,
			type: cc.Node
		}

	},

	playAni(idx, name, options) {
		const times = (options && options.times) || 1;
		const cb = (options && options.callback) || null;
		// const cat = cc.find(`Canvas/frame-border/ground/${node}`);
		const node = this._figurePointRectArray[idx].node
		var dragonDisplay = node.getComponent(dragonBones.ArmatureDisplay);

		const state = dragonDisplay.playAnimation(name, times);
		// dragonDisplay.addEventListener(dragonBones.EventObject.COMPLETE, (event) => {
		// 	//TODO 这里写触发事件
		// 	//event.name为事件名称
		// 	console.log(event.name)
		// }, this);
		// animationDisplay.addEventListener(dragonBones.EventObject.COMPLETE, completeCallback);
		return state;
	},
	// 生命周期 onLoad
	onLoad() {
		this._timeoutIds = [];
		this._intervalIds = [];
		this.initSceneData();
		this.initSize();
		window.ccc = this;
	},

	_imageResList: null,
	_audioResList: null,
	_animaResList: null,

	_lastFingerPosition: null,

	_hintsNodeList : null,
	_figurePointRectArray: null,

	_allAnimationNodes: null,

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

		this._allAnimationNodes = [];
		this._hintsNodeList = [];
		this._lastFingerPosition= {
			pos: null,
			dirty: false
		};
		this._figurePointRectArray = [];
	},

	_designSize: null,  // 设计分辨率
	_frameSize: null,  // 屏幕分辨率
	_mapScaleMin: null,  // 场景中常用缩放（取大值）
	_mapScaleMax: null,  // 场景中常用缩放（取小值）
	_cocosScale: null,  // cocos 自缩放 （较少用到）
	
	initSize() {
		// 注意cc.winSize只有在适配后(修改fitHeight/fitWidth后)才能获取到正确的值,因此使用cc.getFrameSize()来获取初始的屏幕大小
		let screen_size = cc.view.getFrameSize().width / cc.view.getFrameSize().height
		let design_size = cc.Canvas.instance.designResolution.width / cc.Canvas.instance.designResolution.height
		let f = screen_size >= design_size
		cc.Canvas.instance.fitHeight = f
		cc.Canvas.instance.fitWidth = !f

		const frameSize = cc.view.getFrameSize();
		this._frameSize = frameSize;

		this._designSize = cc.view.getDesignResolutionSize();

		let sx = cc.winSize.width / frameSize.width;
		let sy = cc.winSize.height / frameSize.height;
		this._cocosScale = Math.min(sx, sy);

		sx = frameSize.width / this._designSize.width;
		sy = frameSize.height / this._designSize.height;
		this._mapScaleMin = Math.min(sx, sy) * this._cocosScale;
		this._mapScaleMax = Math.max(sx, sy) * this._cocosScale;
	},
	createStateMachine(){
		const check =  [
							{ target: '#game.end', cond: (ctx, evt) => {
								console.log('[answer NEXT] check if end', ctx.questionIndex >= ctx.questionsCount - 1);
								return ctx.questionIndex >= ctx.questionsCount -1

							} },
        					{ target: 'hint' } // reenter 'hint' state
						]
		const QuestionState = {
			id: 'question',
			initial: 'hint',
			states: {
				hint: {
					invoke: {
						src: (ctx, event) => {
							console.log(`[hint] play question ${ctx.questionIndex}: ${ctx.questionPlayTimes } audio`);
							ctx.questionPlayTimes += 1;
							return new Promise((resolve,reject) => {
								
								playAudioByUrl(ctx.questions[ctx.questionIndex].audio_url, () => {
									console.log('hint finish');
									resolve()
								})
							})
						},
						onDone: 'answer',
						onError: 'answer'
					}
				},
				answer: {
					after: {
						8000: [
							// { 
							// 	target: '#QuestionsArray.next', 
							// 	actions: raise('CHECK'),
							// 	cond: (ctx, evt) => {
							// 		console.log('[timeout check] #QuestionsArray.next', ctx.questionPlayTimes , ctx.maxQuestionTryTimes, (ctx.questionPlayTimes >= ctx.maxQuestionTryTimes));
							// 		return ctx.questionPlayTimes >= ctx.maxQuestionTryTimes;
							// 	}, 
							// },
        					{ 
								target: 'wrong',
								// cond: (ctx, evt) => {
								// 	console.log('[timeout check] wrong', ctx.questionPlayTimes , ctx.maxQuestionTryTimes, (ctx.questionPlayTimes < ctx.maxQuestionTryTimes));
								// 	return ctx.questionPlayTimes < ctx.maxQuestionTryTimes;
								// }, 
							} // reenter 'hint' state
						]
						
					},
					
					invoke: {
						
						src: (context, event) => (callback, onReceive) => {
							
							console.log('[answer] thinking and playing');
							// check every second
							const id = setInterval(() => {
								
								const region = context.questions[context.questionIndex];
								const result = this.checkInRegion(region.rect);
								// console.log('checkInRegion', JSON.stringify(this._lastFingerPosition), JSON.stringify(region.rect), result);
								if (result) {
									callback('RIGHT')
									// playAudioByUrl(this.data.begin_audio, () => {
									// 	console.log('game begin audio finish');
									// })
								} else if (this._lastFingerPosition.dirty) {
									callback('WRONG')
								}
							}, 300);
							this._intervalIds.push(id);
					
							// Perform cleanup
							return () => clearInterval(id);
						},
						
					},
					on: {
						RIGHT: {
							target: 'right',
						},
						WRONG: {
							target: 'wrong',
						}
					}
				},
				
				right: {
					invoke: {
						src: (context, event) => {
							this._lastFingerPosition.pos = null;
							this._lastFingerPosition.dirty = false;
							return new Promise((resolve, reject) => {
								console.log('answer right');
								// this.playAni(context.questionIndex, 'normal')
								const an = context.questions[context.questionIndex].node;
								console.log('in right ' + (typeof an))
								an.active = true;
								const dragonDisplay = an.getComponent(dragonBones.ArmatureDisplay);
								dragonDisplay.playAnimation('normal', 0);
								const ddLogoCat = this.logoCat.getComponent(dragonBones.ArmatureDisplay)
								ddLogoCat.playAnimation('right', 1)
								ddLogoCat.once(dragonBones.EventObject.COMPLETE, function() {
									this.logoCat.getComponent(dragonBones.ArmatureDisplay).playAnimation('normal', 0)
								}, this)

								playAudioByUrl(this.rightAudio, () => {
									resolve()
								})
							});
						},
						onDone: {
							target: '#QuestionsArray.next',
							actions: raise('CHECK')
						},
						onError: {
							target: '#QuestionsArray.next',
							actions: raise('CHECK')
						}
					}
				},
				wrong:{
					invoke: {
						src: (context, event) => {
							this._lastFingerPosition.pos = null;
							this._lastFingerPosition.dirty = false;
							return new Promise((resolve, reject) => {
								console.log('answer wrong', context.questionIndex, context.questionPlayTimes , context.maxQuestionTryTimes);
								const ddLogoCat = this.logoCat.getComponent(dragonBones.ArmatureDisplay)
								ddLogoCat.playAnimation('wrong', 1)
								ddLogoCat.once(dragonBones.EventObject.COMPLETE, function() {
									this.logoCat.getComponent(dragonBones.ArmatureDisplay).playAnimation('normal', 0)
								}, this);
								let wrongEffect = this.wrongAudio;
								// 第一和第二题的提示音
								if ([0, 1].includes(context.questionIndex)) {
									wrongEffect = this.data[`wrong_audio${context.questionPlayTimes}`]
								} else {
									// 非第一和第二题，并且做第二遍的提示音
									if (context.questionPlayTimes == 2) {
										wrongEffect = this.data[`wrong_audio${context.questionPlayTimes}`]
									}
								}


								if (context.questionPlayTimes >= context.maxQuestionTryTimes) {
									const an = context.questions[context.questionIndex].node;
									an.active = true;
									const dragonDisplay = an.getComponent(dragonBones.ArmatureDisplay);
									dragonDisplay.playAnimation('normal', 0);
									console.log('show animation', an.active);
								}
								
								playAudioByUrl(wrongEffect, () => {
									if (context.questionPlayTimes >= context.maxQuestionTryTimes) {
										// const an = context.questions[context.questionIndex].node;
										// an.active = true;
										// const dragonDisplay = an.getComponent(dragonBones.ArmatureDisplay);
										// dragonDisplay.playAnimation('normal', 0);
										// console.log('show animation', an.active);
										reject()
									} else {
										resolve()
									}
								})
							});
						},
						onDone: {
							target: 'hint',
							// actions: raise('CHECK')
						},
						onError: {
							target: '#QuestionsArray.next',
							actions: raise('CHECK')
						}
					}
				},
			}
		}
		const QuestionsArrayStatus = {
			id: 'QuestionsArray',
			initial: 'play',
			states: {
				play: {
					...QuestionState
				},
				next: {
					on: {
						CHECK: [
							{
								target: '#game.end',
								cond: (ctx, evt) => {
									ctx.questionIndex += 1;
									ctx.questionPlayTimes = 0
									console.log('#game.end',  ctx.questionIndex >= ctx.questionsCount )
									return ctx.questionIndex >= ctx.questionsCount
								}
							},
							{target: 'play'}
						]
					
					}
				}
			}
		}

		
		const state = {
			id: 'game',
			initial: 'begin',
			context: {
				questionIndex: 0,
				questionPlayTimes: 0,
				maxQuestionTryTimes: 2,
				questionsCount: this._figurePointRectArray.length,
				questions: this._figurePointRectArray,
			},
			states: {
				begin: {
					invoke: {
						src: (ctx) => {
							console.log('game begin 1');
							if(window.courseware && window.courseware.openOsmoFingerRead) {
								this.isOpenedOsmoFingerRead = true;
								window.courseware.openOsmoFingerRead({
									"page_id": 27,
									"book_id": "opw1"
								})
							}
							var dragonDisplay = this.logoCat.getComponent(dragonBones.ArmatureDisplay);
							const state = dragonDisplay.playAnimation('begin', 0);
							return new Promise((resolve,reject) => {
								playAudioByUrl(this.data.begin_audio, () => {
									console.log('game begin audio finish');
									resolve()
								})
							});
						},
						onDone:  'guide',
						onError: 'guide'
					}
				},
				
				guide:{
					invoke: {
						src: (ctx) => {
							return new Promise((resolve,reject) => {
								playAudioByUrl(this.data.guide_audio, () => {
									console.log('game guide audio finish');
									reject()
								})
							});
						},
						onDone:  'playing',
						onError: 'playing'
					},
					
				},
				playing: {
					entry: (ctx) => {
						console.log('entry play stage');
						var dragonDisplay = this.logoCat.getComponent(dragonBones.ArmatureDisplay);
						const state = dragonDisplay.playAnimation('normal', 0);
						cc.assetManager.loadRemote(this.data.playing_audio, (err, audioClip) => {
							cc.audioEngine.playMusic(audioClip, true, 0.8);
						});
						ctx.questions.forEach((block) => {
							block.node.active = false;
						});
					},
					exit: () => {
						console.log('exit play stage');
						cc.audioEngine.stopMusic();
					},
					on: {
						NEXT: 'end'
					},
					// ...QuestionsStates
					...QuestionsArrayStatus
				},
				end: {
						entry: (ctx, event) => {
							console.log('game end');
							var dragonDisplay = this.logoCat.getComponent(dragonBones.ArmatureDisplay);
							const state = dragonDisplay.playAnimation('finish', 0);
							if(window.courseware && window.courseware.closeOsmoFingerRead) {
								this.isOpenedOsmoFingerRead = false;
								window.courseware.closeOsmoFingerRead()
							}
							playAudioByUrl(this.data.end_audio, () => {
								console.log('game finish');
							})
							// return new Promise((resolve,reject) => {
							// 	setTimeout(() => {
							// 		console.log('hint finish');
							// 		resolve()
							// 	}, 2000)
							// })
						}
					
				},
			}
		};
		const after = {};
		for(let i = 0 ;i  < this._hintsNodeList.length; i++) {
			const t = this._hintsNodeList[i].node;
			const k = this._hintsNodeList[i].after;
			after[k]= {
				actions: () => {
					const dragonDisplay = t.getComponent(dragonBones.ArmatureDisplay);
					dragonDisplay.playAnimation('enlarge', 1);
					console.log(dragonDisplay._armatureKey, 'enlarge')
					dragonDisplay.addEventListener(dragonBones.EventObject.COMPLETE, (event) => {
						// TODO 这里写触发事件
						// event.name为事件名称
						// event.armature.animation.play('normal', 0);
						// console.log(event.animationState.name, dragonDisplay._armatureKey, 'end')
					}, this);


					// const dragonDisplay = t.getComponent(dragonBones.ArmatureDisplay);
					// dragonDisplay.playAnimation('enlarge', 1);
					// console.log(dragonDisplay._armatureKey, 'enlarge')
					// dragonDisplay.addEventListener(dragonBones.EventObject.COMPLETE, (event) => {
					// 	//TODO 这里写触发事件
					// 	//event.name为事件名称
					// 	event.armature.animation.play('normal', 0);
					// 	console.log(event.animationState.name, dragonDisplay._armatureKey, 'end')
					// }, this);
				} 
			}
		}
		if (this.data.begin_sep_time) {
			const t = +this.data.begin_sep_time * 1000;
			after[t] = {
				actions: () => {
					console.log('game begin 2');

					for(let i = 0 ;i  < this._allAnimationNodes.length; i++) {
						const t = this._allAnimationNodes[i];
						const dragonDisplay = t.getComponent(dragonBones.ArmatureDisplay);
						dragonDisplay.playAnimation('normal', 0);
					}
				}
			}
		}
		
		state.states.begin.after = after;



		const gameMachine = createMachine(state);
		window.gameMachine = gameMachine;
		this.gameMachineService = interpret(gameMachine).onTransition((state) => {
		// console.log(1, state.value, state.context);
		});
		this.gameMachineService.start();
		window.gameMachineService = gameMachineService;


	},

	// 生命周期 start
	start() {
		let getData = this.getData.bind(this);
		if (window && window.courseware) {
			getData = window.courseware.getData;
		}

		getData((data) => {
			// console.log('data:', data);
			this.data = data || this.getDefaultData();
			this.data = JSON.parse(JSON.stringify(this.data))
			
			this.preloadItem()
		});
		window.air = window.air || {}
		if (window.air) {
			window.air.osmoFingerReadCallback = (result) => {
				if (!this.gameMachineService) {
					return;
				}
				// const st = this.gameMachineService.state.value.constructor.name == 'Object' 
				// && this.gameMachineService.state.value['playing']
				// && this.gameMachineService.state.value['playing'].constructor.name == 'Object'
				// && this.gameMachineService.state.value['playing']['play']
				// && this.gameMachineService.state.value['playing']['play'] == 'answer'
				// if (st) {
				// }
				const resultObj = JSON.parse(result);
				const p = resultObj.finger_pos_in_page;
				this._lastFingerPosition = {
					pos:{
						x: p.x,
						y: p.y
					},
					dirty: true
				}
			}
		}
		this.isDebug = true;
		
	},
	checkInRegion(r) {
		const p = this._lastFingerPosition ? this._lastFingerPosition.pos || {x:0, y:0} : {x:0, y:0};
		const w = p.x > r.x && p.x < (r.x + r.width);
		const h = p.y > r.y && p.y < (r.y + r.height);
		return w && h;

	},

	getData(func) {
		if (window && window.courseware) {
			window.courseware.getData(func, 'scene');
			return;
		}

		const middleLayer = cc.find('middleLayer');
		if (middleLayer) {
			const middleLayerComponent = middleLayer.getComponent('middleLayer');
			middleLayerComponent.getData(func);
			return;
		}

		func(this.getDefaultData());
	},

	getDefaultData() {
		return defaultData;
	},

	preloadItem() {
		this.addPreloadImage();
		this.addPreloadAudio();
		this.addPreloadAnima();
		this.preload();
	},


	addPreloadImage() {

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

		for (const q of this.data.hotZoneItemArr) {
			if (q.gIdx =='0') {
				this._imageResList.push({ url: q.texPngData.url});
			}

			if (q.gIdx =='1') {
				this._imageResList.push({ url: q.texPngData.url });
			}
			if (q.gIdx =='2') {
				this._imageResList.push({ url: q.pic_url});
			}
		}
	},
	prepareAudioClip(url, key) {
		if (url) {
			cc.assetManager.loadRemote(url, (err, audioClip) => {
				if (audioClip) {
					this[key] = audioClip;
				}
			});
		}
	},
	addPreloadAudio() {

		this._audioResList.push({ url: this.data.begin_audio });
		// this.prepareAudioClip(this.data.begin_audio1, 'begin_audio1')
		// this._audioResList.push({ url: this.data.begin_audio2 });
		// this.prepareAudioClip(this.data.begin_audio2, 'begin_audio2')
		this._audioResList.push({ url: this.data.guide_audio });
		this._audioResList.push({ url: this.data.playing_audio });
		if (this.data.wrong_audio) {
			this._audioResList.push({ url: this.data.wrong_audio });
			this.wrongAudio = this.data.wrong_audio
		}
		if (this.data.right_audio) {
			this._audioResList.push({ url: this.data.right_audio });
			this.rightAudio = this.data.right_audio
		}
		this._audioResList.push({ url: this.data.wrong_audio1 });
		this._audioResList.push({ url: this.data.wrong_audio2 });
		this._audioResList.push({ url: this.data.end_audio });


		for (const q of this.data.hotZoneItemArr) {
			if (q.gIdx =='0') {
				this._audioResList.push({ url: q.audio_url });
			}
		}
	},
	debugGraphics() {
		const boxDebugger = cc.find('Canvas/box-debugger');
		if (!this.isDebug) {
			boxDebugger.active = false;
			return;
		}
		boxDebugger.active = true;
		console.log('show debug graphics');
		const ground = cc.find('Canvas/frame-border/ground');
		const canvas = cc.find('Canvas');
		const {width: baseW, height: baseH} = ground;
		const {width: bgW, height: bgH} = this.data.bgItem.rect
		const scaleX = bgW / baseW;
		const scaleY = bgH / baseH;
		let added  =false
		this.data.hotZoneItemArr.forEach(area => {
			
			if (added) {
				// return
			}
			if (area.gIdx != '0') {
				return;
			}
			added = true;
			const dn = new cc.Node();
			const lblNode = new cc.Node();
			lblNode.parent = dn
			const lbl = lblNode.addComponent(cc.Label);
			lblNode.color = cc.color(0,0,0)
			lbl.string = area.skeJsonData.name.substring(0, area.skeJsonData.name.length - 9)
			dn.name = lbl.string;
			dn.parent = boxDebugger;
			dn.width = baseW;
			dn.height = baseH;
			
			const grap = dn.addComponent(cc.Graphics);
			const r = RandomInt(1,255)
			const g = RandomInt(1,255)
			const b = RandomInt(1,255)
			grap.fillColor=cc.color(r, g, b, 127);
			const {x: rx, y: ry, width: rw, height: rh} =  area.rect;
			let w = (rw / bgW) * baseH ;
			let h = (rh / bgH) * baseH;
			let x = (rx / bgW) * baseW;// - baseW / 2;
			let y = baseH - (ry / bgH) * baseH - rh;// - baseH;
			// w *= minScale
			// h *= minScale
			// x *= minScale
			// y *= minScale
            // const p = boxDebugger.convertToNodeSpaceAR(cc.v2(x,y));
			// x = p.x;
			// y = p.y;
			lblNode.x = x;
			lblNode.y = y + h;
			// dn.scale = area.mapScale; // minScale; // this._mapScaleMax * area.mapScale; // ;
			console.log('[debug]', area.skeJsonData.name, x, y, w, h);
			grap.fillRect(x, y, w, h);
			grap.stroke();
			
		});

	},
	addPreloadAnima() {

	},

	preload() {

		const preloadArr = this._imageResList.concat(this._audioResList).concat(this._animaResList);
		// console.log(preloadArr);
		cc.assetManager.loadAny(preloadArr, null, (f, t, item) => {
			// console.log(f, t, item)
		}, (err, data) => {
			// console.log(err, data);
			const ground = cc.find(`Canvas/frame-border/ground`);
			const groundBg = cc.find(`Canvas/frame-border/ground/bg`);
			groundBg.active = false;
			const baseRect = this.data.bgItem.rect;
			getSpriteFrimeByUrl(this.data.bgItem.url, (sf) => {
				const gs = groundBg.getComponent(cc.Sprite);
				gs.spriteFrame = sf;
				groundBg.active = true;

				let dbCount = 0;
				let finishDbCount = 0
				for(const block of this.data.hotZoneItemArr) {
					if (block.gIdx == '0' || block.gIdx == '1') {
						dbCount++;
					}
				}
				for(const block of this.data.hotZoneItemArr) {

					
					if (block.gIdx == '2') {
						const s1 = ((ground.width  / block.imgSizeW) + (ground.height  / block.imgSizeH)) / 2
						const node = new cc.Node();
						node.parent = ground;
						const spr = node.addComponent(cc.Sprite);
						getSpriteFrimeByUrl(block.pic_url, (sf) => {
						spr.spriteFrame = sf;
						node.scale = s1;
						})

					}
					if (block.gIdx == '0' || block.gIdx == '1') {
						const n = new cc.Node();
						n.scale = 0.25;
						const db = n.addComponent(dragonBones.ArmatureDisplay);
						n.parent = ground;
						n.active = false;
						loadDragonBones(db, {
							tex: block.texPngData.url, 
							atlas: block.texJsonData.url,
							ske: block.skeJsonData.url,
						}).then(({width, height}) => {
							n.width = width;
							n.height = height;	
							n.active = true;
							db.playAnimation('null', 0);
							this._allAnimationNodes.push(n);
							finishDbCount++;
							console.log(finishDbCount, dbCount)
							if (finishDbCount == dbCount) {
								this.loadEnd();
							}
						}).catch(() => {
							finishDbCount++;
							console.log(finishDbCount, dbCount)
							if (finishDbCount == dbCount) {
								this.loadEnd();
							}
						});
						if (block.gIdx == '0') {
							const rectInfo = {
								node: n,
								audio_url: block.audio_url ,
								rect: {
									x: block.rect.x / baseRect.width,
									y: block.rect.y / baseRect.height,
									width: block.rect.width / baseRect.width,
									height: block.rect.height / baseRect.height,
								}
							};
							this._figurePointRectArray.push(rectInfo);
		
							if (block.labelText) {
								const k = +block.labelText * 1000;
								if(!isNaN(k)) {
									this._hintsNodeList.push({
										node: n,
										after: k
									});
								}
							}
						}
					}
				}
			});
			
			// console.log(this.data);
			console.log('resource perload finish');

// <<<<<<< HEAD
// =======
// 			if (window && window["air"]) {
// 				// cc.find('Canvas').opacity = 0;
// 				window.air.onCourseInScreen = (next) => {
// 					// cc.find('Canvas').opacity = 255;
// 					this.loadEnd();
// 					next();
// 				}
// 				window.air.hideAirClassLoading();
// 			} else {
// 				this.loadEnd();
// 			}
// >>>>>>> 2d461e1910571bcfc6ad4dc93c2cedafc20923ea

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


	loadEnd() {
		if (window && window["air"] && window["air"].hideAirClassLoading) {
			window["air"].hideAirClassLoading();
		}
		this.initData();
		// this.initAudio();
		// this.initView();
		this.initBg();
		// this.initListener();
		this.debugGraphics();
		this.createStateMachine();
	},

	_cantouch: null,
	initData() {
		// 所有全局变量 默认都是null 
		this._cantouch = true;
	},

	// audioBtn: null,
	// initAudio() {
	// 	const audioNode = cc.find('Canvas/res/audio');

	// 	const getAudioByResName = (resName) => {
	// 		return audioNode.getChildByName(resName).getComponent(cc.AudioSource);
	// 	}

	// 	this.audioBtn = getAudioByResName('btn');

	// },


	// initView() {

	// 	// this.initBg();
	// 	// this.initPic();
	// 	// this.initBtn();
	// 	this.initIcon();
	// },

	initBg() {
		const bgNode = cc.find('Canvas/bg');
		bgNode.scale = this._mapScaleMax;
		console.log('this._mapScaleMax', this._mapScaleMax);
	},

	
	

	/*initBtn() {

		this.curPage = 0;
		const bottomPart = cc.find('Canvas/bottomPart');
		bottomPart.zIndex = 5; // 提高层级

		bottomPart.x = bottomPart.parent.width / 2;
		bottomPart.y = -bottomPart.parent.height / 2;

		const leftBtnNode = bottomPart.getChildByName('btn_left');
		//节点中添加了button组件 则可以添加click事件监听
		leftBtnNode.on('click', () => {
			if (!this._cantouch) {
				return;
			}
			if (this.curPage == 0) {
				return;
			}
			this.curPage = 0
			this.leftMove();

			// 游戏结束时需要调用这个方法通知系统作业完成
			onHomeworkFinish();

			cc.audioEngine.play(this.audioBtn.clip, false, 0.8)
		})

		const rightBtnNode = bottomPart.getChildByName('btn_right');
		//节点中添加了button组件 则可以添加click事件监听
		rightBtnNode.on('click', () => {
			if (!this._cantouch) {
				return;
			}
			if (this.curPage == 1) {
				return;
			}

			this.curPage = 1
			this.rightMove();

			cc.audioEngine.play(this.audioBtn.clip, false, 0.5)
		})
	},*/




	// update (dt) {},




















	_timeoutIds: null,
	_intervalIds: null,
	// 生命周期
	onDestroy() {
		console.log('op15 onDestroy');
		this._timeoutIds.forEach(id => {
			clearTimeout(id);
		});
		this._intervalIds.forEach(id => {
			clearInterval(id);
		});
		this.gameMachineService.stopChild();
		this.gameMachineService.stop();
		if(window.courseware && window.courseware.closeOsmoFingerRead) {
			if (this.isOpenedOsmoFingerRead) {
				window.courseware.closeOsmoFingerRead()
			}
		}
	},





});
