#include <complex>
#include <cstring>

#include "latlib/o815/o815.h"

#ifdef ALGORITHM_METRO
#define ALGORITHM "metro"
#include "sim-metro.hpp"
#elif ALGORITHM_AHBMHB
#include "sim-nonmetro.hpp"
#define ALGORITHM "AhbMhb"
#elif ALGORITHM_AHBMOR
#include "sim-nonmetro.hpp"
#define ALGORITHM "AhbMor"
#elif ALGORITHM_AORMHB
#include "sim-nonmetro.hpp"
#define ALGORITHM "AorMhb"
#elif ALGORITHM_AORMOR
#include "sim-nonmetro.hpp"
#define ALGORITHM "AorMor"
#elif ALGORITHM_AORM0
#include "sim-nonmetro.hpp"
#define ALGORITHM "AorM0"
#elif ALGORITHM_AHBM0
#include "sim-nonmetro.hpp"
#define ALGORITHM "AhbM0"
#elif ALGORITHM_A0MOR
#include "sim-nonmetro.hpp"
#define ALGORITHM "A0Mor"
#elif ALGORITHM_A0MHB
#include "sim-nonmetro.hpp"
#define ALGORITHM "A0Mhb"
#else
#error NO UPDATE-ALGORITHM DEFINED!
#endif

#ifdef NONMETRO_A_HB
#error SADSADS
#endif

o815 *O815;
sim *Sim;

const complex<double> _i_ = complex<double>(0.0,1.0);

#include "obs_phi2.hpp"
#include "obs_phi2_hist.hpp"
#include "obs_phip2_hist.hpp"

o815::comoption specOps[] = {
  { "mass", required_argument, NULL, 'm', "set mass", "min:max:inc" },
  { "", 0, NULL, 0, "", "" }
};

void parseSpecOps() 
{
  for (int isopt = 0; isopt < O815->parsedSpecOps.size(); isopt++)
    switch(O815->parsedSpecOps[isopt].first) {
    case 'm':
      O815->paraQ->addRange("mass", O815->parsedSpecOps[isopt].second);
      break;
    }
}

void helpHeader() 
{ 
  cout << "Usage: ./heatbath-"ALGORITHM" [OPTIONS] [obs1] ... [obsN]" << endl << endl;
}

void parseLonelyArgs()
{
  for (vector<char*>::iterator lonit = O815->lonelyArgs.begin(); lonit != O815->lonelyArgs.end(); ++lonit) {
    if ( strcmp(*lonit, "phi2") == 0 ) {
      *O815->out->log << "MASTER: registered observable: phi2" << endl << flush;
      O815->observables.push_back(new obs_phi2(O815));
    }
    else if ( strcmp(*lonit, "phi2_hist") == 0 ) {
      *O815->out->log << "MASTER: registered observable: phi2_hist" << endl << flush;
      O815->observables.push_back(new obs_phi2_hist(O815));
    }
    else if ( strcmp(*lonit, "phip2_hist") == 0 ) {
      *O815->out->log << "MASTER: registered observable: phip2_hist" << endl << flush;
      O815->observables.push_back(new obs_phip2_hist(O815));
    }
  }
}

int main (int argc, char *argv[])
{
  O815 = new o815(argc, argv, "heatbath-"ALGORITHM, specOps, &helpHeader);

  O815->addPara("mass", 1);

  parseSpecOps();

  O815->postParaInit();

  O815->Sim = new sim(O815);

  parseLonelyArgs();
  
  O815->mainLoop();

  delete O815;
  return 0;
}
