//sceneTreeLoad
function sceneTreeLoad(){
	
	//ilk grup haric diger tüm gruplarin gizlenmesi icin flag
	var first=true;
	
	//option objeleri
	var dataObjects=dataScene[sceneCurrent]['objects'];
	for(key in dataObjects){
		
		var dataObject=dataObjects[key];
		
		//grpTree		
		var grpTree = new Konva.Group({
			id: key,
			x: dataObject['x'],
			y: dataObject['y'],
		});
		grpScene.add(grpTree);
		grpTree.moveToBottom();
		if(first){
			first=false;
			} else {
			grpTree.hide();
		}
		
		//image
		src=dataObject['src'];
		if(src){
			var object=qcImage({
				id:"image",
				name:"image",
				src:src,
			});
			grpTree.add(object);
		}
		
		//buttonTrue
		src=dataObject['trueSrc'];
		if(src){
			var buttonTrue=qcImage({
				name:"true",
				x:dataObject['trueX'],
				y:dataObject['trueY'],
				src:src,
			});
			grpTree.add(buttonTrue);
			initButton(buttonTrue,1);
			buttonTrue.setAttr('target',dataObject['trueTarget']);
			buttonTrue.setAttr('endScene',dataObject['trueEndScene']);
			buttonTrue.on('pointerdown ', function () {
				sceneTreeClick(this);
			});
		}
		
		//buttonFalse
		src=dataObject['falseSrc'];
		if(src){
			var buttonFalse=qcImage({
				name:"false",
				x:dataObject['falseX'],
				y:dataObject['falseY'],
				src:src,
			});
			grpTree.add(buttonFalse);
			initButton(buttonFalse,1);
			buttonFalse.setAttr('target',dataObject['falseTarget']);
			buttonFalse.setAttr('endScene',dataObject['falseEndScene']);
			buttonFalse.on('pointerdown ', function () {
				sceneTreeClick(this);
			});
		}
		
		
		//checkerTrue
		var checkerTrue=qcImage({
			name:"checkerTrue",
			src:dataScene[sceneCurrent]['checkerTrueSrc'],
			x:0,
			y:0,
			width:dataScene[sceneCurrent]['checkerWidth'],
			height:dataScene[sceneCurrent]['checkerHeight'],
		});
		checkerTrue.hide();
		grpTree.add(checkerTrue);
		
		//checkerFalse
		var checkerFalse=qcImage({
			name:"checkerFalse",
			src:dataScene[sceneCurrent]['checkerFalseSrc'],
			x:0,
			y:0,
			width:dataScene[sceneCurrent]['checkerWidth'],
			height:dataScene[sceneCurrent]['checkerHeight'],
		});
		checkerFalse.hide();
		grpTree.add(checkerFalse);
		
		
	}
	
	sceneResults[sceneCurrent]={
		'scene':sceneCurrent,
		'type':'tree',
		'answer':'Yok',
		'correct':dataScene[sceneCurrent]['answer'],
	};
}
function sceneTreeClick(button){
	btnCheckPopup.hide();
	checkLock=false;
	var targetID=button.getAttr('target');
	var target=grpScene.findOne('#'+targetID);
	
	var grp=button.getParent();
	var buttonTrue=grp.findOne('#true');
	var buttonFalse=grp.findOne('#false');
	
	if(button.id()=="true"){
		buttonFalse.hide();
		
		} else {
		buttonTrue.hide();
	}
	
	buttonTrue.off('pointerdown');
	buttonTrue.off('pointerup');
	buttonTrue.off('pointerover');
	
	buttonFalse.off('pointerdown');
	buttonFalse.off('pointerup');
	buttonFalse.off('pointerover');
	
	dataScene[sceneCurrent]['targetID']=targetID;
	
	target.show();
	
	sceneTreeCheck();
	
	if(button.getAttr('endScene')!=null){
		checkScene();
	}
	
	
}

function sceneTreeCheck(check=null){
	var targetID=dataScene[sceneCurrent]['targetID'];
	var target=grpScene.findOne('#'+targetID);
	var exitID=dataScene[sceneCurrent]['exit'];
	
	
	sceneResults[sceneCurrent]={
		'scene':sceneCurrent,
		'type':'option',
		
	};
	
	
	if(targetID==exitID){
		//kazandın
		sceneResults[sceneCurrent]['success']=true;
		btnSolve.hide();
		if(check && target){ 
			sceneTreeChecker(target,true);
		}
		
		} else {
		if(check && target){ 
			sceneTreeChecker(target,false);
		}
	}
}

function sceneTreeReset(){
	objects = grpScene.find('Group');
	
	
	for(key in objects){
		var obj = objects[key];
		
		//obj.width(0);
		//obj.height(0);
		obj.opacity(.4);
	}
}
function sceneTreeSolve(){
	
	objects = grpScene.find('Group');
	for(key in objects){
		
		var obj = objects[key];
		
		obj.show();
		
		
		var buttonTrue=obj.findOne('#true');
		if(buttonTrue){
			buttonTrue.show();
			buttonTrue.off('pointerdown');
			buttonTrue.off('pointerup');
			buttonTrue.off('pointerover');
		}
		
		var buttonFalse=obj.findOne('#false');
		if(buttonFalse){
			buttonFalse.show();
			buttonFalse.off('pointerdown');
			buttonFalse.off('pointerup');
			buttonFalse.off('pointerover')
		}
		
		
		var checkerTrue=obj.findOne('#checkerTrue');
		var checkerFalse=obj.findOne('#checkerFalse');
		
		checkerTrue.hide();
		checkerFalse.hide();
		
		if(obj.id()==dataScene[sceneCurrent]['exit']){
			sceneTreeChecker(obj,true);
		}
	}
	
	
}

function sceneTreeChecker(obj,trueFalse){
	
	if(dataScene[sceneCurrent]['check']){
		
		checkerTrue=obj.findOne('#checkerTrue');
		checkerFalse=obj.findOne('#checkerFalse');
		
		checkerTrue.hide();
		checkerFalse.hide();
		
		checkerX=0;
		checkerY=0;
		
		if(dataScene[sceneCurrent]['checkerX']){ checkerX=dataScene[sceneCurrent]['checkerX'];}
		if(dataScene[sceneCurrent]['checkerY']){ checkerY=dataScene[sceneCurrent]['checkerY'];}
		
		if(obj.getAttr('checkerX')){ checkerX=obj.getAttr('checkerX');}
		if(obj.getAttr('checkerY')){ checkerY=obj.getAttr('checkerY');}
		
		checkerTrue.x(checkerX);
		checkerTrue.y(checkerY);
		
		checkerFalse.x(checkerX);
		checkerFalse.y(checkerY);
		
		if(trueFalse){
			checkerTrue.show();
			} else {
			checkerFalse.show();
		}
	}
}		