//tiinav
//legends
//controller
//events


package controller{
	
	//import
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.display.Sprite;
	
	import model.EventsModel;
	import model.EventDataModel;
	
	
	
	public class EventsController{
		
		// variables
		private var myXML:XML = new XML();
		private var myXMLURL:URLRequest = new URLRequest("timeline.xml");
		private var myLoader:URLLoader = new URLLoader(myXMLURL);
		//an array to put all the data in so that I can use it to show on flash or whatever...
		private var dateArray:Array = new Array();
		private var infoArray:Array = new Array();
		private var peopleArray:Array = new Array();
		private var questionArray:Array = new Array();
		private var answersArray:Array = new Array();
		private var followUpArray:Array = new Array();
		private var bonusArray:Array = new Array();
		private var bonusAnswersArray:Array = new Array();
		private var bonusFollowUpArray:Array = new Array();

		//keep track of the event number when parsing the xml
		private var eventNum:Number;
		//working with the events model
		private var _model:EventsModel;

		//contructor function
		public function EventsController(m:EventsModel){
			
			//get the events model to work with
			_model=m;

			//parse the xml first
			parseXML();
			
		}
		
		private function parseXML ():void
		{
			// listener for it to load and then do something with it
			myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
			myLoader.load(myXMLURL);
		}// end of public function Main

		private function xmlLoaded(event:Event):void
		{
			// once the data is loaded, assign it to a variable
			myXML = new XML(event.target.data);
			//get the parts and put them into the corresponding arrays
			getXMLParts(myXML);
			//trace("Data loaded.");
			//trace(myXML);
		}

		//gets the parts of the xml and puts them in their proper arrays
		private function getXMLParts(a_xmlData:XML):void
		{
			trace("XML Output: Test");
			trace("------------------------");
			//grabs all the event blocks
			var timelineList:XMLList = a_xmlData.children();
			//start with no event
			eventNum = -1;
			for (var i:int = 0; i < timelineList.length(); i ++)
			{
				//there is only one timelineXML ,i = 0, - do I need this??
				var timelineXML:XML = timelineList[i];
				//trace("----------timeline---------------");
				//trace(timelineXML.name() + " = " + timelineXML);

				//takes all the parts of the question block and gets the name or tag and the text in side
				//for each question block we get the list of children in it
				var eventList:XMLList = timelineXML.children();

				// j is the number for each event part
				for (var j:int = 0; j < eventList.length(); j ++)
				{
					var eventXML:XML = eventList[j];
					trace("----------event part---------------");
					trace(eventXML.name() + " = " + eventXML);
					
					//depending on the name of the event part (eventXML),
					//then put those into their perspective arrays (with their own loops)
					var theEventName:String = eventXML.name();
					switch(theEventName)
					{
						case "date":
						dateArray.push(eventXML);
						//trace("dateArray length = " + dateArray.length);
						//trace("date = " +dateArray[dateArray.length-1]);
						//keeps track of which event for people and answers
						eventNum++;
						answersArray[eventNum] = new Array();
						peopleArray[eventNum] = new Array();
						bonusAnswersArray[eventNum] = new Array();
						break;
						
						case "date_info":
						infoArray.push(eventXML);
						break;
						
						case "people":
						var peopleList:XMLList = eventXML.children();
						//make a multi-dimensional array per event
						
						
						for (var k:int = 0; k < peopleList.length(); k ++)
						{
							var peopleXML:XML = peopleList[k];
							trace("----------people---------------");
							trace(peopleXML.name() + " = " + peopleXML);
							
							peopleArray[eventNum].push(peopleXML);
							trace(peopleArray.length + "   " + peopleArray[eventNum].length);
							//peopleArray[i][k] = peopleXML;
							trace("person #" + ((peopleArray[eventNum].length)-1) + " = " +peopleArray [eventNum][peopleArray[eventNum].length-1]);
						
						}//end for loop (event people)
						break;
						
						case "question":
						var questionList:XMLList = eventXML.children();
						for (var l:int = 0; l < questionList.length(); l ++)
						{
							var questionXML:XML = questionList[l];
							trace("----------question---------------");
							trace(questionXML.name() + " = " + questionXML);
							
							var theQuestionName:String = questionXML.name();
							
							switch(theQuestionName)
							{
								case "ques":
								questionArray.push(questionXML);
								break;
								
								case "followup":
								followUpArray.push(questionXML);
								break;
								
								default:
								//if it is not a question or followup, then its the answers
								//pushed in order will be:
								//ans1 = 0  (a)
								//ans2 = 1  (b)
								//ans3 = 2   (c)
								//correct = 3 (correct answer)
								
								answersArray[eventNum].push(questionXML);
								trace("answer #" + ((answersArray[eventNum].length)-1) + " = " +answersArray [eventNum][answersArray[eventNum].length-1]);
								break;

							}
							
						
						}//end for loop (event question)
						break;
						
						case "bonus":
						var bonusList:XMLList = eventXML.children();
						for (var m:int = 0; m < bonusList.length(); m ++)
						{
							var bonusXML:XML = bonusList[m];
							trace("----------bonus---------------");
							trace(bonusXML.name() + " = " + bonusXML);
							
							var theBonusName:String = bonusXML.name();
							switch(theBonusName)
							{
								case "ques":
								bonusArray.push(bonusXML);
								break;
								
								case "followup":
								bonusFollowUpArray.push(bonusXML);
								break;
								
								case "person":
								//do nothing
								break;
								
								default:
								//if it is not a question or followup, then its the answers
								//pushed in order will be:
								//ans1 = 0  (a)
								//ans2 = 1  (b)
								//ans3 = 2   (c)
								//correct = 3 (correct answer)
								//bonusAnswersArray.push(bonusXML);
								bonusAnswersArray[eventNum].push(bonusXML);
								trace("bonus answer #" + ((bonusAnswersArray[eventNum].length)-1) + " = " +bonusAnswersArray [eventNum][bonusAnswersArray[eventNum].length-1]);
								
								break;

							}
						
						}//end for loop (event bonus)
						break;
						
						default:
						trace("not working");
						break;
						
					}//end switch statement for event parts					
					
					
				}//end for loop (event)
				
				

			}//end for loop (timeline list)
			trace("------------------------");
			trace("END - XML Output: Test");
			
			//change the event for the first time
			changeEvent(0);

		}//end of getXMLParts function
		
		
		public function changeEvent(eventNumber:int):void{
			trace("changing the Event data");
			//this function is called in EventsView
			//when the user clicks on something in the view, the controller changes stuff
			//this way more than just the event.target can be passed
			
			//get the info from the Arrays and assign them into the correct spots for the EventDataModel
			var date:Number = dateArray[eventNumber];
			var info:String = infoArray[eventNumber];
			//last person is always for bonus
			var people:Array = peopleArray[eventNumber];
			var question:String = questionArray[eventNumber];
			//three answers (0-2 )and the correct answer 3: (a,b,c)
			var answers:Array = answersArray[eventNumber];
			var followUp:String = followUpArray[eventNumber];
			var bonusQuestion:String = bonusArray[eventNumber];
			//three answers (0-2 )and the correct answer 3: (a,b,c)
			//except one in which z is the correct answer: all!
			var bonusAnswers:Array = bonusAnswersArray[eventNumber];
			var bonusFollowUp:String = bonusFollowUpArray[eventNumber];
			
			//assign a new event data model
			var newEvent:EventDataModel = new EventDataModel(eventNumber, date, info, people, question, answers, followUp, bonusQuestion, bonusAnswers, bonusFollowUp);
			
			//put the event data model into the event model
			_model.legendEvent = newEvent;
			
			//trace(_model.legendEvent.date);
			
		}


		
	}//end class
	
}//end package