#ifndef _TAG_SAVER_
#define _TAG_SAVER_

#include "ofMain.h"
#include "ofAddons.h"
#include "ofXMLSettings.h"

//conservative number avoid slowdowns
#define NUM_TAGS_PER_FILE 40

class tagSaver
{
	public:

		//-----------------------------------------------
		tagSaver(){
			tagNumber	= 0;
			timeAmnt	= 0;
			timeOffset	= 0;
			sessionCount = 0;
			tagCount	= 0;
			fileCount	= 0;
			bNewTag = false;
			bSetup = false;
		}
		
		//-----------------------------------------------
		~tagSaver(){
			//possible clearing of empty data
			//so we avoid it
			//clearXmlData();	
		}
		
		//-----------------------------------------------		
		void setup(string tagFolder, bool newSession = true){
			folderLocation = ofToDataPath(tagFolder);

			if( folderLocation.size() == 0) return;
						 
			if( folderLocation[folderLocation.size()-1] != '/' ){
				folderLocation += "/";
			}
			
			countPath = folderLocation+"_count.xml";
			prefsPath = folderLocation+"_tagInfo.xml";
			
			xmlCount.loadFile(countPath);
			xmlPrefs.loadFile(prefsPath);
			
			if(newSession){
				sessionCount = xmlCount.getValue("count", 10000) + 1;
				xmlCount.setValue("count", sessionCount);
				xmlCount.saveFile(countPath);
			}
			
			
			sessionPath = folderLocation+"session"+ofToString(sessionCount)+"-"+ofToString(fileCount)+".xml";
			
			string city		= xmlPrefs.getValue("city", "");
			string country	= xmlPrefs.getValue("country", "");
			string crew		= xmlPrefs.getValue("crew", "");
			string comments = xmlPrefs.getValue("comments", "");
			
			//this shouldn't exist but we load it anyway
			xmlTag.loadFile(sessionPath);
			
			if( !xmlTag.tagExists("tag_info") ) xmlTag.addTag("tag_info");
			
			xmlTag.setValue("tag_info:city", city);
			xmlTag.setValue("tag_info:country", country);
			xmlTag.setValue("tag_info:crew", crew);
			xmlTag.setValue("tag_info:comments", comments);
			
			xmlTag.saveFile(sessionPath);

			bSetup = true;
			
		}
		
		//-----------------------------------------------
		void newTag(){
			xmlTag.saveFile(sessionPath);

			if(tagCount >= NUM_TAGS_PER_FILE){
				clearXmlData();
				setup(folderLocation, false);
				tagCount = 0;
				fileCount++;
			}

			bNewTag		= true;
			  
		}
		
		//-----------------------------------------------		
		void addPoint(float x, float y){
			
			if(bNewTag){
				timeOffset  = ofGetElapsedTimeMillis();
				timeAmnt	= 0;
				tagNumber	= xmlTag.addTag("tag");	
				xmlTag.setValue("tag:time", timeOffset, tagNumber);	
				tagCount++;
				bNewTag = false;
			}

			timeAmnt = ofGetElapsedTimeMillis() - timeOffset;
			
			if( xmlTag.pushTag("tag", tagNumber) ){
				int pos = xmlTag.addTag("pt");
				
				xmlTag.setValue("pt:time", timeAmnt, pos);
				xmlTag.setValue("pt:x", x, pos);
				xmlTag.setValue("pt:y", y, pos);
				
				xmlTag.popTag();
			}
		}
		
		bool bNewTag, bSetup;

	protected:
		//-----------------------------------------------
		void clearXmlData(){
			xmlTag.clear();
			xmlCount.clear();
			prefsPath.clear();
		}


		ofXMLSettings xmlTag;
		ofXMLSettings xmlCount;
		ofXMLSettings xmlPrefs;
		
		string countPath;
		string sessionPath;
		string prefsPath;
		string folderLocation;
		
		int tagNumber, tagCount, sessionCount, timeAmnt, fileCount, timeOffset;
		
};

#endif