/** 
* @file GenericLFSelection.h
* @brief This class provides a generic chooser function for different descriptors, which are derived from LocalFeatureRepresentation.
* @date 26.10.2011
*/

#ifndef _NICE_OBJREC_LF_SELECTION_INCLUDE
#define _NICE_OBJREC_LF_SELECTION_INCLUDE

#include <core/basics/Exception.h>

#include "vislearning/features/localfeatures/LocalFeatureRepresentation.h"
#include "vislearning/features/localfeatures/LFMikolajczyk.h"
#include "vislearning/features/localfeatures/LFPatches.h"
#include "vislearning/features/localfeatures/LFGenericLocal.h"
#include "vislearning/features/localfeatures/LFSiftPP.h"
// #include "vislearning/features/localfeatures/LFSegmentation.h" //located in objrec, please move this to ensure its functionality
#include "vislearning/features/localfeatures/LFWriteCache.h"
#include "vislearning/features/localfeatures/LFReadCache.h"
// #include "vislearning/features/localfeatures/LFSegContour.h" //located in objrec, please move this to ensure its functionality
#include "vislearning/features/localfeatures/LFColorSande.h"
#include "vislearning/features/localfeatures/LFonHSG.h"


namespace OBJREC {

  /** @class GenericLFSelection
 * @brief Select a specific LF-Type (local feature representation). LFs compute Descriptors, which DO NOT need given positions, but calculate them ON THEIR OWN!
 * All Descriptor-Methods calculate there own positions previously, and on DIFFERENT ways.
 */
class GenericLFSelection
{
    public:
      
      //! enum specifying for which step the feature extractor shall be used (this influences the config section which will be used)
      enum UsageForTrainOrTest { NOTSPECIFIED = 0,
             TRAINING,
             TESTING
      };

      /** LocalFeature Selector
       * @brief This methode switches between the different LocalFeature-Types. One has to set the "localfeature_type" on a valid value and the methode returns a LocalFeatureRepresentation derived Object.
       * @param[in] conf - A pointer to the given configfile, which must contain "section" - "localfeature_type"
       * @param[in] section - This string defines the value for "section" in the configfile.
       * @param[in] useForTrainOrTest - Specify whether we use the LFRep for Training, Testing, or for both - this influences the choice of the config section that is handed over to the LFRep-method
       * @return LocalFeatureRepresentation* - The LocalFeatureRepresentation which contains the selected LocalFeature-Type.
       */
      static
      LocalFeatureRepresentation *selectLocalFeatureRep ( const NICE::Config *conf, std::string section = "features", const UsageForTrainOrTest & useForTrainOrTest = GenericLFSelection::NOTSPECIFIED )
      {
        // return Value
        OBJREC::LocalFeatureRepresentation *lfrep = NULL;
      
        // string which defines the localfeature_type (for Ex. Sande, ...)
        std::string localfeature_type = conf->gS(section, "localfeature_type", "");
      
        if ( ( localfeature_type == "mikolajczyk" )|| ( localfeature_type == "LFMikolajczyk" ) )
        {
          lfrep = new OBJREC::LFMikolajczyk ( conf );
        }
        else if ( ( localfeature_type == "VANDESANDE" ) || ( localfeature_type == "LFColorSande" ) )//previously: "color"
        {
          if ( useForTrainOrTest == TRAINING )
          {
            lfrep = new OBJREC::LFColorSande ( conf, "LFColorSandeTrain" );
          }
          else if ( useForTrainOrTest == TESTING )
          {
            lfrep = new OBJREC::LFColorSande ( conf, "LFColorSandeTest" );
          }
          else //not specified whether for training or testing, so we do not care about
          {
            lfrep = new OBJREC::LFColorSande ( conf );            
          }          
        }
        else if ( ( localfeature_type == "sift" ) || ( localfeature_type == "siftpp" ) || ( localfeature_type == "LFSiftPP" ) ) 
        {
          lfrep = new OBJREC::LFSiftPP ( conf );
        }
        else if ( ( localfeature_type == "generic_local" ) || ( localfeature_type == "generic" ) || ( localfeature_type == "LFGenericLocal" ) )
        {
          int numFeatures = conf->gI(section, "localfeature_count");
          lfrep = new OBJREC::LFGenericLocal ( conf, numFeatures );
        }
        else if ( ( localfeature_type == "grey" ) || ( localfeature_type == "patches" ) || ( localfeature_type == "LFPatches" ) )
        {
          int numFeatures = conf->gI(section, "localfeature_count");
          lfrep = new OBJREC::LFPatches ( conf, numFeatures );
        } 
        else if ( ( localfeature_type == "onHSG" ) || ( localfeature_type == "LFonHSG" ) )
        {          
          if ( useForTrainOrTest == TRAINING )
          {
            lfrep = new OBJREC::LFonHSG ( conf, "HSGTrain" );
          }
          else if ( useForTrainOrTest == TESTING )
          {
            lfrep = new OBJREC::LFonHSG ( conf, "HSGTest" );
          }
          else //not specified whether for training or testing, so we do not care about
          {
            lfrep = new OBJREC::LFonHSG( conf );
          }           
          
        }
        else 
        {
          lfrep = NULL;
        }

        // read features?
        bool readfeat;
        readfeat = conf->gB ( section, "localfeature_cache_read", false );
        // write features?
        bool writefeat;  
        writefeat = conf->gB ( section, "localfeature_cache_write", false );   

        LocalFeatureRepresentation *writeFeats = NULL;
        LocalFeatureRepresentation *readFeats = NULL;
        if ( writefeat )
        {
          // write the features to a file, if there isn't any to read
          
          if ( useForTrainOrTest == TRAINING )
          {    
            writeFeats = new OBJREC::LFWriteCache ( conf, lfrep, "cacheTrain" );
            lfrep = writeFeats;
          }
          else if ( useForTrainOrTest == TESTING )
          {
            writeFeats = new OBJREC::LFWriteCache ( conf, lfrep, "cacheTest" );
            lfrep = writeFeats;
          }
          else //not specified whether for training or testing, so we do not care about
          {
            writeFeats = new OBJREC::LFWriteCache ( conf, lfrep );
            lfrep = writeFeats;
          }              

        }

        if ( readfeat )
        {
          int numFeatures = conf->gI(section, "localfeature_count", -1);
          // read the features from a file
          if ( writefeat )
          {
            if ( useForTrainOrTest == TRAINING )
            {
              readFeats = new OBJREC::LFReadCache ( conf, writeFeats, numFeatures, "cacheTrain" );
            }
            else if ( useForTrainOrTest == TESTING )
            {
              readFeats = new OBJREC::LFReadCache ( conf, writeFeats, numFeatures, "cacheTest" );
            }
            else //not specified whether for training or testing, so we do not care about
            {
              readFeats = new OBJREC::LFReadCache ( conf, writeFeats, numFeatures, "cache" );
            }                         
          }
          else
          {
            if ( useForTrainOrTest == TRAINING )
            {
              readFeats = new OBJREC::LFReadCache (conf, lfrep, numFeatures, "cacheTrain" );                        
            }
            else if ( useForTrainOrTest == TESTING )
            {
              readFeats = new OBJREC::LFReadCache (conf, lfrep, numFeatures, "cacheTest" );
            }
            else //not specified whether for training or testing, so we do not care about
            {
              readFeats = new OBJREC::LFReadCache (conf, lfrep, numFeatures, "cache" );
            }
          }
          lfrep = readFeats; 
        }  
          
        return lfrep;
      };
      
      static
      void restoreLocalFeatureRep ( LocalFeatureRepresentation * _lfrep, std::istream & is, int format = 0 )
      {
                
        if ( is.good() )
        {
          if ( _lfrep != NULL )
            delete _lfrep;
          
          
          std::string className;
          is >> className; //class name
                    
          if ( className == "<LFMikolajczyk>" )
          {
            _lfrep = new OBJREC::LFMikolajczyk();
          }
          else if ( className == "<LFColorSande>" )
          {
            _lfrep = new OBJREC::LFColorSande();            
          }
          else if ( className == "<LFSiftPP>" )
          {
            _lfrep = new OBJREC::LFSiftPP();
          }
          else if ( className == "<LFGenericLocal>" )
          {
            _lfrep = new OBJREC::LFGenericLocal();
          }
          else if ( className == "<LFPatches>" )
          {
            _lfrep = new OBJREC::LFPatches();
          } 
          else if ( className == "<LFonHSG>" )
          {          
            _lfrep = new OBJREC::LFonHSG();          
          }
          else
          {
            fthrow ( NICE::Exception, "GenericLFSelection::restoreLocalFeatureRep -- class name " << className << "unknown. Aborting." );
          }
          
          //undo reading of class name
          
          for ( uint i = 0; i < className.size(); i++)
          {
            is.unget();
          }
          
          //now, call the restore method of the underlying object
          //NOTE this could be also done externally, leaving only the actual instantiation of the derived objects here
          _lfrep->restore ( is );
            
        }
        else
        {
          fthrow ( NICE::Exception, "GenericLFSelection::restoreLocalFeatureRep -- InStream not initialized - restoring not possible!" );
        }      
    };
    
};

}

#endif
