#include "GLCP.h"
#include "dbimage.h"
#include "bdbenv.h"
#include "dbalnimage.h"
#include "dbtmpfeature.h"
#include <tclap/CmdLine.h>
#include "emcluster.h"
#include <string>

int main( int argc, char *argv[] )
{

#ifndef _NOMAGICK_
  try
    {
      ///set up the command line parsing
      TCLAP::CmdLine cmd("Image alignment program", ' ', "0.001");
      TCLAP::ValueArg<std::string> dbdir("d", "db", "database dirctory", true, "", "directory");
      TCLAP::ValueArg<std::string> tdbg("f", "featuredb", "feature db name", true, "", "filename");
      TCLAP::ValueArg< bool > fmean("m", "mean", "mean flag", false, true, "true/false" );
      TCLAP::ValueArg< bool > fentropy("e", "entropy", "entropy flag", false, true, "true/false" );
      TCLAP::ValueArg< bool > fcontrast("c", "contrast", "contrast flag", false, true, "true/false" );
      TCLAP::ValueArg< bool > fcorrelation("r", "correlation", "correlation flag", false, true, "true/false" );
      TCLAP::ValueArg< bool > fpower("p", "power", "power flag", false, true, "true/false" );

      TCLAP::ValueArg< int > llimit("l", "low", "lower limit for bic", true, 1, "integer" );
      TCLAP::ValueArg< int > ulimit("u", "up", "upper limit for bic", true, 1, "integer" );

      cmd.add(dbdir);
      cmd.add( tdbg );
      cmd.add( fmean );
      cmd.add( fentropy );
      cmd.add( fcontrast );
      cmd.add( fcorrelation );
      cmd.add( fpower );
      cmd.add( llimit );
      cmd.add( ulimit );

      bdbenv bdb;
      dbtmpfeature dbtf;
      dbtf.setrecsize( 72*sizeof(double) );

      cmd.parse(argc, argv);

      int fid;
      float fval;

      bdb.open( dbdir.getValue() );
      dbtf.setname( tdbg.getValue() );
      dbtf.init( bdb.envptr(), 0 );
      bdb.adddb( &dbtf );
      bdb.cleanlog();
      std::vector< double* > samples;
      unsigned int maxid;
      int numparams;
      maxid = dbtf.maxid();
      int dsz;
      dbtf.getrecsize( dsz );

      numparams = 0;
      GLCP::mask mymask;
      if( fmean.getValue() == true )
        mymask.mean = 1;
      else
        mymask.mean = 0;

      if( fentropy.getValue() == true )
        mymask.entropy = 1;
      else
        mymask.entropy = 0;

      if( fcontrast.getValue() == true )
        mymask.contrast = 1;
      else
        mymask.contrast = 0;

      if( fcorrelation.getValue() == true )
        mymask.correlation = 1;
      else
        mymask.correlation = 0;

      if( fpower.getValue() == true )
        mymask.power = 1;
      else
        mymask.power = 0;

      char *buf;
      buf = new char[dsz];
      GLCP myglcp;
      for( unsigned i = 1; i < maxid; i = i + 100 )
        {
          int nparam;
          std::vector< double* > f;
          dbtf.get(i, buf);
          myglcp.deserialize(buf, dsz);
          f.clear();
          myglcp.getfeaturets( f, mymask, nparam );
          if( nparam != numparams && i > 1 )
            {
              std::cout << "ERROR number of params not equal\n";
              exit(0);
            }
          numparams = nparam;
          for( unsigned j = 0; j < f.size(); j++ )
            samples.push_back( f[j] );
        }

      emcluster emg;
      
      std::cout << numparams << " parameters\n";
      emg.loaddata( samples, numparams, 5);

      emg.bicrun( llimit.getValue(), ulimit.getValue() );

      for( unsigned i = 0; i < samples.size(); i++ )
        delete [] samples[i];

      dbtf.close();
      bdb.close();
    }
  catch (TCLAP::ArgException &e)
    {
      std::cout << "ERROR: " << e.error() 
                << " for arg " << e.argId() << "\n";
      return 0;
    }
  
#else
  std::cout << "compiled with no image magick\n";
#endif
  return 0;
}
