#include <string>
#include <iostream>
#include <iomanip>

#include "km3net-dataformat/offline/Head.hh"

#include "JAAnet/JHead.hh"
#include "JAAnet/JAAnetDictionary.hh"

#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JMonteCarloFileSupportkit.hh"
#include "JSupport/JSupport.hh"

#include "JROOT/JRootPrinter.hh"
#include "JROOT/JSonObjectOutput.hh"

#include "JLang/JStreamObjectOutput.hh"

#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"


/**
 * \file
 * Auxiliary program to print sum of Monte Carlo run headers.
 *
 * \author mdejong
 */
int main(int argc, char **argv)
{
  using namespace std;
  using namespace JPP;
  
  JMultipleFileScanner<JHeaderTypes_t> inputFile;
  string         key;
  bool           print_as_head;
  bool           print_as_json;
  int            debug;

  try { 

    JParser<> zap("Auxiliary program to print sum of Monte Carlo run headers.");
    
    zap['f'] = make_field(inputFile);
    zap['k'] = make_field(key)             = "";
    zap['H'] = make_field(print_as_head);
    zap['J'] = make_field(print_as_json);
    zap['d'] = make_field(debug)           = 1;

    zap(argc, argv);
  }
  catch(const exception &error) {
    FATAL(error.what() << endl);
  }

  const JHead header = getHeader(inputFile);
 
  if (key == "") {

    Head buffer;

    copy(header, buffer);

    typedef JTYPELIST<Head, JHead>::typelist  typelist;
    typedef JObjectOutput<typelist>*          output_type;
    
    output_type out = (print_as_json ? 
		       (output_type) new JSonObjectOutput   <typelist>(cout) : 
		       (output_type) new JStreamObjectOutput<typelist>(cout));

    if (print_as_head)
      out->put(buffer);
    else
      out->put(header);

    delete out;

  } else {

    JRootPrinter::print(cout, header, key, JAAnetDictionary::getInstance());
  }
}