// test_AcdWireReader.cxx
//
// David Adams
// April 2017
//
// Test AcdWireReader.

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include "art/Framework/Services/Registry/ServiceHandle.h"
#include "dune/DuneInterface/Service/RawDigitPrepService.h"
#include "dune/DuneInterface/Tool/TpcDataTool.h"
#include "dune/ArtSupport/ArtServiceHelper.h"
#include "dune/ArtSupport/DuneToolManager.h"
#include "lardataobj/RawData/RawDigit.h"
#include "lardata/DetectorInfoServices/DetectorClocksService.h"

#undef NDEBUG
#include <cassert>

using std::string;
using std::cout;
using std::endl;
using std::ostringstream;
using std::ofstream;
using std::setw;
using std::setprecision;
using std::fixed;
using fhicl::ParameterSet;
using art::ServiceHandle;

#include "TestDigit.h"

//**********************************************************************

bool sigequal(AdcSignal sig1, AdcSignal sig2) {
  AdcSignal sigdiff = sig2 - sig1;
  if ( sigdiff < -0.5 ) return false;
  if ( sigdiff >  0.5 ) return false;
  return true;
}

//**********************************************************************

int test_AcdWireReader(bool useExistingFcl =false) {
  const string myname = "test_AcdWireReader: ";
#ifdef NDEBUG
  cout << myname << "NDEBUG must be off." << endl;
  abort();
#endif
  string line = "-----------------------------";

  cout << myname << line << endl;
  string fclfile = "test_AcdWireReader.fcl";
  if (useExistingFcl) {
    cout << myname << "Using existing top-level FCL." << endl;
    ArtServiceHelper::load_services(fclfile, ArtServiceHelper::FileOnPath);
  } else {
    cout << myname << "Creating top-level FCL." << endl;
    // To convert digit to wire, use the DUNE fcl for 35-ton reco.
    // Disable noise removal and deconvolution because these make it difficult
    // to predict the result.
    std::ofstream config{fclfile};
    config << "#include \"services_dune.fcl\"" << endl;
    config << "services: @local::dune35tdata_reco_services" << endl;
    config << "services.RawDigitPrepService.DoNoiseRemoval: false" << endl;
    config << "services.RawDigitPrepService.DoDeconvolution: false" << endl;
    config << "services.RawDigitPrepService.DoIntermediateStates: true" << endl;
    config << "services.AdcChannelDataCopyService.CopyFlags: true" << endl;
    // Need the standard tool to read channel data from a digit.
    config << "#include \"dataprep_tools.fcl\"" << endl;
    // Build local wire reader.
    config << "tools.mytool: {" << endl;
    config << "  tool_type: AcdWireReader" << endl;
    config << "  LogLevel: 2" << endl;
    config << "}" << endl;
  }

  // We explicitly initialize the DuneToolManager first so that the
  // above configuration wins.  If we load the services first, then a
  // default services configuration is loaded, and the one for this
  // test is ignored.
  cout << myname << line << endl;
  cout << myname << "Fetching tool manager." << endl;
  DuneToolManager* ptm = DuneToolManager::instance(fclfile);
  ArtServiceHelper::load_services(fclfile, ArtServiceHelper::FileOnPath);

  assert ( ptm != nullptr );
  DuneToolManager& tm = *ptm;
  tm.print();
  assert( tm.toolNames().size() );

  cout << myname << line << endl;
  cout << myname << "Fetch services." << endl;
  ServiceHandle<RawDigitPrepService> hrdp;

  cout << myname << line << endl;
  cout << myname << "Construct test digit." << endl;
  TestDigit tdig(2);
  AdcIndex nsig = tdig.nsig;
  const raw::RawDigit* pdig = tdig.pdig;

  cout << myname << line << endl;
  cout << myname << "Create channel dat map and set digits." << endl;
  AdcChannelDataMap acds;
  AdcChannelData& acd = acds[tdig.channel];
  acd.digit = pdig;
  assert( acd.raw.size() == 0 );

  cout << myname << line << endl;
  cout << myname << "Construct test wire." << endl;
  std::vector<recob::Wire> wires;
  wires.reserve(acds.size());
  assert( acd.samples.size() == 0 );
  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataForJob();
  assert( hrdp->prepare(clockData, acds, &wires) == 0 );
  assert( acd.raw.size() == nsig );
  assert( acd.samples.size() == nsig );

  cout << myname << line << endl;
  cout << myname << "Fetch wire read tool." << endl;
  auto prdr = tm.getPrivate<TpcDataTool>("mytool");
  assert( prdr != nullptr );

  cout << myname << line << endl;
  cout << myname << "Read wire into channel data." << endl;
  AdcChannelData newacd;
  newacd.wire = &wires[0];
  assert( newacd.samples.size() == 0 );
  assert( prdr->update(newacd) == 0 );
  assert( newacd.samples.size() == nsig );

  cout << myname << line << endl;
  cout << myname << "Check samples." << endl;
  int dbg = 1;
  for ( unsigned int isig=0; isig<nsig; ++isig ) {
    if ( dbg ) cout << myname << isig << ": " << acd.samples[isig]
                    << " ?= " << newacd.samples[isig]
                    << " (" << acd.signal[isig] << " "
                    << newacd.signal[isig] << ")" << endl;
    assert(sigequal(acd.samples[isig], newacd.samples[isig]));
  }
  cout << myname << "NROI: " << wires[0].SignalROI().size() << " "
                 << acd.rois.size() << " " << newacd.rois.size() << endl;
  assert( newacd.rois.size() == acd.rois.size() );

  cout << myname << line << endl;
  cout << myname << "Done." << endl;
  return 0;
}

//**********************************************************************

int main(int argc, char* argv[]) {
  bool useExistingFcl = false;
  if ( argc > 1 ) {
    string sarg(argv[1]);
    if ( sarg == "-h" ) {
      cout << "Usage: " << argv[0] << " [ARG]" << endl;
      cout << "  If ARG = true, existing FCL file is used." << endl;
      return 0;
    }
    useExistingFcl = sarg == "true" || sarg == "1";
  }
  return test_AcdWireReader(useExistingFcl);
}

//**********************************************************************
