/******************************************************************************************
 * 
 * Monterey Bay Aquarium Research Institute
 * Francois Boudet summer intern 2007    
 * 07/24/2007
 *  
 * 
 * 
 * ******************************************************************************************/

#include "Raster/Raster.H"
#include "SIFT/Keypoint.H"
#include "MbariVisualObject.H"
#include "MbariVisualObjectDB.H"
#include "Image/DrawOps.H"
#include <time.h>
#include <iostream>
#include <fstream>


using namespace std;

/*! Load a database, enrich it with new VisualObject entities
  extracted from the given images, and save it back. */

int main(const int argc, const char **argv)
{
  MYLOGVERB = LOG_INFO;

  // check command-line args:
  if (argc < 2 )
    LFATAL("USAGE: match data base to image <dbname.vdb> <file.txt> ");
           //"... <imageN.png>");

  // load the database:
  MbariVisualObjectDB vdb;
  LINFO("Load DB \n");  
  int save = 0;
  if (!vdb.loadFrom(argv[1]))  
 	vdb.setName(argv[1]);
    			

  /*********************************************************************************************/
  /*******************              app match sift database              ***********************/
  /*********************************************************************************************/

  // Open the file zith all the images filepath

  std::string ImName;

  std::ifstream inf(argv[2]);


  if (inf.is_open() == false)
  {
	LERROR("Cannot open '%s' -- USING EMPTY", argv[2]);
  } else {

	std::getline(inf, ImName); 	
	int pos1 = ImName.find_last_of('_');
	int pos3 = ImName.find_last_of('/');
	std::string numero_image = ImName.substr(pos1+1);
        std::string event= ImName.substr(pos3+1,(pos1-2)-pos3+1);
        std::string fold = ImName.substr(0,pos3+1);
	std::string event_old = event;
	
	int rock, flatfish, flatfishSometin, leuk, leukFat, leukRath, leukRathFat, leuk2, org, rath, unk;
	rock = 0; flatfish=0; flatfishSometin=0; leuk=0 ; leukFat=0; leukRath=0; leuk2=0; leukRathFat=0; rath=0; org=0; unk=0;
	std::string evt1, evt2; 
	std::cout << "fichier ,  event nb , rockfish , flatfish , leuk , rath , org , unknow "<<endl;

  	while(ImName!="")
  	{
		//	std::cout << " folder " << fold.c_str() <<" \nevent "<<  event.c_str() <<" \nnum image "<< numero_image.c_str() << endl;
			
			// get input image:
  			Image< PixRGB<byte> > colim = Raster::ReadRGB(ImName);

  			// create visual object and extract keypoints:
  			rutz::shared_ptr<MbariVisualObject> vo(new MbariVisualObject(ImName, ImName, colim));

  			// get the matching objects:
  			std::vector< rutz::shared_ptr<MbariVisualObjectMatch> > matches;
  			const uint nmatches = vdb.getObjectMatches(vo, matches, VOMA_KDTREEBBF);

		
  			// if no match, forget it:
  			if (nmatches == 0U)
			{
    				unk++;//std::cout << "### No matching object found." <<endl;				
			}
  			else
    			{
	      			// let the user know about the matches:
      			for (uint i = 0; i < nmatches; i ++)
        		{
          			rutz::shared_ptr<MbariVisualObjectMatch> vom = matches[i];
        	  		rutz::shared_ptr<MbariVisualObject> obj = vom->getVoTest();
				if (obj->getName().compare("flatfish"))
					flatfish++;
				else if (obj->getName().compare("flatfish-something"))
					flatfishSometin++;
				else if (obj->getName().compare("leuk"))
				{
					leuk++;
					std::cout << " leuk + 1 !! " << endl;
				}
				else if (obj->getName().compare("leuk+fat"))
					leukFat++;
				else if (obj->getName().compare("leuk+rath"))
					leukRath++;
				else if (obj->getName().compare("leuk+rath+fat"))
					leukRathFat++;
				else if (obj->getName().compare("2leukr"))
					leuk2++;
				else if (obj->getName().compare("org"))
					org++;
				else if (obj->getName().compare("rath"))
				{
			 		rath++;
					std::cout << " rath + 1 !! " << endl;
				}
				else if (obj->getName().compare("rockfish"))
					rock++;
				
				evt1 = vo->getName();
				evt2 = obj->getImageFname();
				int p1 = evt1.find_last_of('_');
				int p2 = evt1.find_last_of('/');
				evt1 = evt1.substr(p2+1, (p1-1)-p2);

				p1 = evt2.find_last_of('_');
				evt2 = evt2.substr(0,p1);

				if (evt1.compare(evt2)!=0)
				{
					std::cout << " evt1 = "<<evt1<<" evt2 = "<<evt2<<endl;
					std::cout << "," << vo->getName()<<","<< obj->getName().c_str()<<","<<vom->getScore() <<","<<obj->getImageFname().c_str()<<endl;
				} 
        		} 

			
    
    		} 
			
			
			std::getline(inf, ImName); 			
			int pos1 = ImName.find_last_of('_');
			int pos3 = ImName.find_last_of('/');
          		event= ImName.substr(pos3+1,(pos1-2)-pos3+1);
			if (event.compare(event_old)!=0)
			{		
				std::cout << fold.c_str() <<","<< event.c_str() <<","<< rock <<","<<flatfish+flatfishSometin <<","<< leuk+leukFat+leukRath+leukRathFat+leuk2 <<","<< rath <<","<< org <<","<< unk <<endl;
			
				rock = 0; flatfish=0; flatfishSometin=0; leuk=0 ; leukFat=0; leukRath=0; leuk2=0; 	leukRathFat=0; rath=0; org=0; unk=0;
			}
          		numero_image = ImName.substr(pos1+1);
          		fold = ImName.substr(0,pos3+1);
			

		event_old = event;

  	}
  }
/*

  
*/

  return 0;
}

