#include <stdexcept>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TextTestProgressListener.h>
#include <cppunit/TextOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/TestSuite.h>
#include <cppunit/Exception.h>
#include "Log.h"
#include <unistd.h>

void printHelp() {
  std::cout << "Usage: xtest <options>" << std::endl
	    << " -c level : crop flag level" << std::endl
	    << " -g level : global flag level" << std::endl
	    << " -s level : slice flag level" << std::endl    
	    << " -b level : background flag level" << std::endl    
	    << " -d level : BLD flag level" << std::endl
	    << " -r level : reference image level" << std::endl
	    << " -e level : energy calculation level" << std::endl
	    << " -a level : set ALL flags to level" << std::endl
	    << " -t testName : test to be run" << std::endl;
}

int main(int argc, char* argv[]) {
  int gvalue = 0;
  int rvalue = 0;
  int cvalue = 0;
  int svalue = 0;
  int bvalue = 0;
  int dvalue = 0;
  int evalue = 0;
  int avalue = -1;
  char *tvalue = "";
  int index;
  int c;
  
  opterr = 0;
  
  while ((c = getopt (argc, argv, "a:c:g:r:t:s:b:e:h")) != -1) {
    switch (c) {
    case 'c':
      cvalue = atoi(optarg);
      break;

    case 'g':
      gvalue = atoi(optarg);
      break;
      
    case 'r':
      rvalue = atoi(optarg);
      break;

    case 's':
      svalue = atoi(optarg);
      break;

    case 'b':
      bvalue = atoi(optarg);
      break;

    case 'd':
      dvalue = atoi(optarg);
      break;

    case 'e':
      evalue = atoi(optarg);
      break;

    case 't':
      tvalue = optarg;
      break;

    case 'a':
      avalue = atoi(optarg);
      break;

    case 'h':
      printHelp();
      return 0;
      break;

    default:
      gvalue = 0;
      rvalue = 0;
      tvalue = "";
    }
  }

  if (avalue == -1) {
    Log::getInstance().setDebugLevel(Log::flagGlobal, gvalue);
    Log::getInstance().setDebugLevel(Log::flagReference, rvalue);
    Log::getInstance().setDebugLevel(Log::flagCrop, cvalue);
    Log::getInstance().setDebugLevel(Log::flagSlice, svalue);
    Log::getInstance().setDebugLevel(Log::flagBackground, bvalue);
    Log::getInstance().setDebugLevel(Log::flagBLD, dvalue);
    Log::getInstance().setDebugLevel(Log::flagEnergy, evalue);
  }
  else {
    Log::getInstance().setDebugLevel(Log::flagGlobal, avalue);
    Log::getInstance().setDebugLevel(Log::flagReference, avalue);
    Log::getInstance().setDebugLevel(Log::flagCrop, avalue);
    Log::getInstance().setDebugLevel(Log::flagSlice, avalue);
    Log::getInstance().setDebugLevel(Log::flagBackground, avalue);
    Log::getInstance().setDebugLevel(Log::flagBLD, avalue);
    Log::getInstance().setDebugLevel(Log::flagEnergy, avalue);
  }


  // Command line argument is the test path. Defaults to "", which resolves to the
  // top level suite
//   std::string testPath = (argc > 1) ? std::string(argv[1]) : std::string("");
  std::string testPath = (argc > 1) ? std::string(tvalue) : std::string("");

    if (testPath == "info") {
        CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry("XTCAVUnitTest");
        CppUnit::Test *suite = registry.makeTest();

        int testCount = suite->getChildTestCount();
        std::cout << "There are " << testCount << " test units:" << std::endl;
        for (int i = 0; i < testCount; ++i) {
            CppUnit::Test *t = suite->getChildTestAt(i);
            std::cout << "  " << t->getName() << "\n";
        }
        return 0;
    }

    CppUnit::TestResult controller;
    CppUnit::TestResultCollector result;
    controller.addListener(&result);
    CppUnit::TextTestProgressListener progress;
    controller.addListener(&progress);

    CppUnit::TestRunner runner;
    runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());

    try {
        std::cout << "Running " << testPath.c_str() << std::endl;
        runner.run(controller, testPath);
        CppUnit::TextOutputter outputter(&result, std::cout);
        outputter.write();
    } catch (std::invalid_argument &e) {
        std::cerr << std::endl << "ERROR: " << e.what() << std::endl;
        return 1;
    }

    return result.wasSuccessful() ? 0 : 1;
}
