#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){	 
	ofBackground(255,255,255);	
	
	//-----------
	//basic loading and reading values
	
	message = "loading mySettings.XML";
	
	//currently ofxXMLSettings doesn't default to the data folder
	//so we have to use ofToDataPath to load the file from data/ 
	if( XML.loadFile(ofToDataPath("mySettings.XML")) ){
		message = "mySettings.XML loaded!";
	}else{
		message = "unable to load mySettings.XML check data/ folder";
	}
	
	//read the colors from XML
	//if the file doesn't yet exist it assigns default values 
	
	red		= XML.getValue("BACKGROUND:COLOR:RED", 170);
	green	= XML.getValue("BACKGROUND:COLOR:GREEN", 190);
	blue	= XML.getValue("BACKGROUND:COLOR:BLUE", 240);
	
	/*
		"BACKGROUND:COLOR:RED" creates a structure like this:
		
		<BACKGROUND>
			<COLOR>
				<RED>101.103516</RED>
			</COLOR>
		</BACKGROUND>
		
		then "BACKGROUND:COLOR:GREEN" makes the XML become:

		<BACKGROUND>
			<COLOR>
				<RED>101.103516</RED>
				<GREEN>190.103516</GREEN>
			</COLOR>
		</BACKGROUND>		
	*/
	
	//SOME OTHER EXAMPLES

	//---------------------
	//multiple tag names
	
	//you can also have multiple values with the same name at the top 
	//most level (root level) of you're file
	//to add a value with the same name.
	
	XML.addValue("TIME", ofGetElapsedTimeMillis() );
	XML.addValue("TIME", ofGetElapsedTimeMillis() );
	XML.addValue("TIME", ofGetElapsedTimeMillis() );
	XML.addValue("TIME", ofGetElapsedTimeMillis() );
	
	/*
		This might add something like this to the file
		
		<TIME>101229</TIME>
		<TIME>101230</TIME>
		<TIME>101231</TIME>
		<TIME>101232</TIME>
	*/	
	
	//if you need to read a value you can do.
	
	//3 specifies you want to read value number 3
	//which is the fourth value - so you should get 101232 as the value
	XML.getValue("TIME", 0, 3);
	
	//you can also set a specific value
	XML.setValue("TIME", 9999999, 3);
	
	/*
		The structure would now look like this
		
		<TIME>101229</TIME>
		<TIME>101230</TIME>
		<TIME>101231</TIME>
		<TIME>999999</TIME>
	*/	
	
	
		
	//---------------	
	//pushTag and popTag
	
	//Also if you need to out multitags inside other tags 
	//you can use pushTag and popTag to temporarily set your document root
	//to the inside of a created tag.
	
	//first we check to see if the parent tag 
	//exists - if it doesn't lets make it
	if( !XML.tagExists("RECORDING") ){
		XML.addTag("RECORDING");
	}
	
	/* We have an XML structure with just:
		
		<RECORDING></RECORDING>
	
	*/
	
	//now we want to push inside this tag so we can have multiple time values within it
	if( XML.pushTag("RECORDING") ){
	
		XML.addValue("TIME", ofGetElapsedTimeMillis() );
		XML.addValue("TIME", ofGetElapsedTimeMillis() );
		XML.addValue("TIME", ofGetElapsedTimeMillis() );
		XML.addValue("TIME", ofGetElapsedTimeMillis() );	
		
		//we pop back out - so we are now at the document level
		XML.popTag();
	}
	
	/*
		
		The structure would now look like this
		<RECORDING>
			<TIME>101229</TIME>
			<TIME>101230</TIME>
			<TIME>101231</TIME>
			<TIME>999999</TIME>
		</RECORDING>
	
	*/

}

//--------------------------------------------------------------
void testApp::update(){
	//we change the background color here
	ofBackground(red,green,blue);	
}

//--------------------------------------------------------------
void testApp::draw(){

	ofSetColor(0xDDDDDD);
	ofRect(0, 0, ofGetWidth(), 20);
	ofRect(0, ofGetHeight()-20, ofGetWidth(), 20);

	ofSetColor(210, 90, 100);
	ofDrawBitmapString(message, 10, 14);
	
	ofDrawBitmapString("drag mouse to change color - 's' key saves to XML", 10, ofGetHeight() - 7);
}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){ 
		
		if(key == 's'){
			XML.saveFile(ofToDataPath("mySettings.XML"));
		}
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){ 
	
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
	
	//calculate r g and b for the two mouse coords coming in
	float xpct = (float)x / ofGetWidth();
	float ypct = (float)y / ofGetHeight();
	
	red = xpct * 255.0;
	green = ypct * 255.0;
	blue = (int)(red - green) % 255;
		
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(){

	//update the colors to the XML structure when the mouse is released
	XML.setValue("BACKGROUND:COLOR:RED", red);
	XML.setValue("BACKGROUND:COLOR:GREEN", green);
	XML.setValue("BACKGROUND:COLOR:BLUE", blue);
	
}
