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

int checkmodel( dbtmpfeature &orig, dbhmm &dbh,
                GLCP::mask &mymask, int correctid);

int checkmodel( dbtmpfeature &orig, dbhmm &dbh,
                GLCP::mask &mymask, int correctid)
{
  unsigned int maxid;
  int bufsz;
  orig.getrecsize( bufsz );
  maxid = orig.maxid();
  char *z;
  z = new char[bufsz];
  GLCP myglcp;
  int sz;
  int pos, neg;
  pos = neg = 0;
  for( unsigned i = 1; i < maxid; i = i + 1 )
    {
      orig.get( i, z );
      myglcp.del();
      myglcp.deserialize( z, bufsz );

      std::vector< double* > myts;
      myts.clear();
      myglcp.getfeaturets( myts, mymask, sz );

      int cid;
      cid = dbh.classify( myts, sz );
      if( cid != correctid )
        neg++;
      else
        pos++;

      for( unsigned int x = 0; x < myts.size(); x++ )
        delete [] myts[x];
    }

  std::cout << "positives: " <<  pos  
            << " negitives: " << neg << "\n";

  delete [] z;
  return 0;
}

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,
                                         "", "databaase");
      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" );

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

      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;

      bdbenv bdb;
      dbtmpfeature dbtf_g, dbtf_b;
      dbhmm dbh;
      dbalnimage dba;

      dbtf_g.setname( "good.db" );
      dbtf_b.setname( "bad.db" );

      cmd.parse(argc, argv);

      bdb.open( dbdir.getValue() );
      dbtf_g.init( bdb.envptr(), 0 );
      dbtf_b.init( bdb.envptr(), 0 );
      dba.init( bdb.envptr() );

      bdb.adddb( &dba );
      bdb.adddb( &dbtf_g );
      bdb.adddb( &dbtf_b );
      bdb.cleanlog();

      checkmodel( dbtf_b, dbh, mymask, 1);

      checkmodel( dbtf_g, dbh, mymask, 0);

      dbtf_g.close();
      dbtf_b.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;
}

