#ifndef _GUI_SETTING_MANAGER_
#define _GUI_SETTING_MANAGER_

#include "ofMain.h"
#include "ofAddons.h"
#include "guiSetting.h"
#include "baseGui.h"

#define MAX_SETTINGS 100

class guiSettingsManager : public baseGui
{
	public:
		
		guiSettingsManager(){
			
			index 			= 0;	//current setting
			section			= -1;	//current section
			selected 		= 0;    //which setting is selected?
			whichSection	= 0;	//which section are we in?
			
			for(int i = 0; i < MAX_SETTINGS; i++) bI[i] = false; 
		}
		
		// ------------------------
		void readFromFile(string filePath){
			//xml.setVerbose(false);
			xml.initFromFile(filePath, "PARAMS");
		}

		// ------------------------
		void addTitle(string title){
			section++;
			titles[section] = title;
		}
		
		
		// ------------------------
		void addSetting(string displayName, string xmlName, int value, int min, int max, int incr){
			bI[index] = true; //we know to display it as an int
			addSetting(displayName, xmlName, (float)value, (float)min, (float)max, (float)incr);
		}
		
		// ------------------------
		void addSetting(string displayName, string xmlName, float value, float min, float max, float incr){
			if(index >= MAX_SETTINGS)return;
			
			gs[index].setName(xmlName, displayName);
			gs[index].setValue(value);
			gs[index].setMinMax(min,max);
			gs[index].setIncr(incr);
			gs[index].setGroupID(section);
			index++;	
			
			//now we actually read the value from
			//the xml file into our setting
			updateValueFromXml(index-1);
		}
		
		// ------------------------
		void updateValueFromXml(int which){
			//if(which >= index)return;
		
			if(bI[which]){
				int tmp = gs[which].getValueI();
				tmp = xml.getValue(gs[which].getXmlName(), tmp);
				gs[which].setValue(tmp);				
			}else{
				float tmp = gs[which].getValueF();
				tmp = xml.getValue(gs[which].getXmlName(), tmp);
				gs[which].setValue(tmp);
			}			

			//don't want to trigger changed events from xml
			//gs[which].setChanged(false);

		}
		
		// ------------------------
		void reloadSettings(){
			for(int i = 0; i < index; i++){
				updateValueFromXml(i);
			}
		}
		
		// ------------------------
		void saveToFile(string filePath){
			
			for(int i = 0; i < index; i++){
			
				if(bI[i])xml.setValue(gs[i].getXmlName(), gs[i].getValueI());
				else xml.setValue(gs[i].getXmlName(), gs[i].getValueF());
			}
			
			xml.saveFile(filePath);
		}
		
		// ------------------------
		int getSettingI(string varName){
			
			for(int i = 0; i < index; i++){
				if( varName == gs[i].getXmlName() ){
					return gs[i].getValueI();
				}
			}
			return 0;
		}	
		
		// ------------------------
		float getSettingF(string varName){
			
			for(int i = 0; i < index; i++){
				if( varName == gs[i].getXmlName() ){
					return gs[i].getValueF();
				}
			}
			return 0;
		}
		
		// ------------------------
		bool hasChanged(string varName){
			for(int i = 0; i < index; i++){
				if( varName == gs[i].getXmlName() ){
					return gs[i].getChanged();
				}
			}
			return false;
		}

		// ------------------------
		int setSetting(string varName, float value){
			
			for(int i = 0; i < index; i++){
				if( varName == gs[i].getXmlName() ){
					gs[i].setValue(value);
				}
			}
			return 0;
		}	
		
		// ------------------------
		void selectNext(){
			selected++;
			if(selected >= index)selected = 0;
			
			whichSection = gs[selected].getGroupID();
		}

		// ------------------------
		void selectPrev(){
			selected--;
			if(selected < 0)selected = index-1;
			
			whichSection = gs[selected].getGroupID();
		}

		// ------------------------
		void increase(){
			gs[selected].increase();
		}
		
		// ------------------------
		void decrease(){
			gs[selected].decrease();
		}
		
		// ------------------------
		void drawSelected(float x, float y, float spacing){
			drawText( gs[selected].getDisplayName(), x, y);
			
			if(bI[selected]) handyString = ofToString(gs[selected].getValueI());
			else  handyString = ofToString(gs[selected].getValueF());
			
			handyString = "["+handyString+"]";
			
			drawText( handyString, x + spacing, y);
		}
		
		// ------------------------
		void drawCollapsedStyle(float x, float y, float spacing, float lineHeight){

			int pos = 0;	
			int k = 0;		
						
			for(int k = 0; k <= section; k++){
							
				pos++;
				float drawPosY = y + ( (float)pos*lineHeight);
				
				if(whichSection != k) glColor4f(0.6f, 0.6f, 0.6f, 1.0f);
				else glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
				drawText( titles[k], x, drawPosY);
				
				if(k == whichSection ){
					pos++;						
					for(int i = 0; i < index; i++){
						if(	whichSection == gs[i].getGroupID() ){
						
							drawPosY = y + ( (float)pos*lineHeight);	
												
							i != selected ? glColor4f(0.6f, 0.6f, 0.6f, 1.0f) : glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
							drawText( gs[i].getDisplayName(), x + 6, drawPosY);
						
							if(bI[i]) handyString = ofToString(gs[i].getValueI());
							else  handyString = ofToString(gs[i].getValueF());
							
							if(i == selected) {
								handyString = "["+handyString+"]";
								drawText( handyString, x + spacing, drawPosY);
							}else{
								drawText( handyString, x + spacing, drawPosY);
							}
							
							pos++;						
						}
					}
				}
			}
		}
				
		// ------------------------
		void drawAll(float x, float y, float spacing, float lineHeight){
			
			int pos = 0;	
			int k = -1;		
						
			for(int i = 0; i < index; i++){
				
				float drawPosY = y + ( (float)pos*lineHeight);
				
				//this is where we draw the titles
				if(k != gs[i].getGroupID() ){
					
					k = gs[i].getGroupID();
				
					pos++;
					drawPosY = y + ( (float)pos*lineHeight);
					
					glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
					drawText( titles[k], x, drawPosY);

					pos++;
					drawPosY = y + ( (float)pos*lineHeight);
				}
				
				//this is where we draw the setting names
				//and their values
				i != selected ? glColor4f(0.6f, 0.6f, 0.6f, 1.0f) : glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
				drawText( gs[i].getDisplayName(), x + 6, drawPosY);
			
				if(bI[i]) handyString = ofToString(gs[i].getValueI());
				else  handyString = ofToString(gs[i].getValueF());
				
				if(i == selected) {
					handyString = "["+handyString+"]";
					drawText( handyString, x + spacing, drawPosY);
				}else{
					drawText( handyString, x + spacing-1, drawPosY);
				}
				
				pos++;
			}
		}
		
		
	protected:
		
		ofXML 			xml;
		guiSetting 		gs[MAX_SETTINGS];
		bool			bI[MAX_SETTINGS];
		string 			handyString;
		string			titles[MAX_SETTINGS];
		int 			selected, index, section, whichSection;
	
};

#endif