// C++ common header
#include <iostream>
#include <memory>
#include <vector>
#include <fstream>

#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CondFormats/Common/interface/FileBlob.h"
#include "CondFormats/DataRecord/interface/DQMXMLFileRcd.h"
#include "DQMServices/Core/interface/DQMStore.h"

namespace edmtest {
  class DQMXMLFileEventSetupAnalyzer : public edm::one::EDAnalyzer<> {
  public:
    explicit DQMXMLFileEventSetupAnalyzer(const edm::ParameterSet& pset);
    explicit DQMXMLFileEventSetupAnalyzer(int i);
    ~DQMXMLFileEventSetupAnalyzer() override;
    void analyze(const edm::Event& event, const edm::EventSetup& setup) override;
    void beginRun(edm::Run const&, edm::EventSetup const&);

  private:
    const edm::ESGetToken<FileBlob, DQMXMLFileRcd> fileBlobToken_;
    bool init_;
    std::string labelToGet_;
  };

  DQMXMLFileEventSetupAnalyzer::DQMXMLFileEventSetupAnalyzer(const edm::ParameterSet& ps)
      : fileBlobToken_(
            esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", ps.getParameter<std::string>("labelToGet")))),
        labelToGet_(ps.getParameter<std::string>("labelToGet")) {
    init_ = false;
    edm::LogPrint("DQMXMLFileEventSetupAnalyzer")
        << "DQMXMLFileEventSetupAnalyzer(const edm::ParameterSet &ps)" << std::endl;
  }

  DQMXMLFileEventSetupAnalyzer::DQMXMLFileEventSetupAnalyzer(int i) {
    init_ = false;
    edm::LogPrint("DQMXMLFileEventSetupAnalyzer") << "DQMXMLFileEventSetupAnalyzer(int i) " << i << std::endl;
  }

  DQMXMLFileEventSetupAnalyzer::~DQMXMLFileEventSetupAnalyzer() {
    init_ = false;
    edm::LogPrint("DQMXMLFileEventSetupAnalyzer") << "~DQMXMLFileEventSetupAnalyzer" << std::endl;
  }

  void DQMXMLFileEventSetupAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) { return; }

  void DQMXMLFileEventSetupAnalyzer::beginRun(edm::Run const& run, edm::EventSetup const& iSetup) {
    edm::LogPrint("DQMXMLFileEventSetupAnalyzer") << "DQMXMLFileEventSetupAnalyzer::beginRun()" << std::endl;
    if (!init_) {
      init_ = true;
      edm::eventsetup::EventSetupRecordKey recordKey(
          edm::eventsetup::EventSetupRecordKey::TypeTag::findType("DQMXMLFileRcd"));
      if (recordKey.type() == edm::eventsetup::EventSetupRecordKey::TypeTag()) {
        throw cms::Exception("Record not found") << "Record \"DQMXMLFileRcd"
                                                 << "\" does not exist!" << std::endl;
      }

      const auto& rootgeo = &iSetup.getData(fileBlobToken_);
      edm::LogPrint("DQMXMLFileEventSetupAnalyzer") << "XML FILE IN MEMORY 1 with label " << labelToGet_ << std::endl;
      std::unique_ptr<std::vector<unsigned char> > tb1((*rootgeo).getUncompressedBlob());
      //here you can implement the stream for putting the TFile on disk...
      std::string outfile1("XML1_retrieved.xml");
      std::ofstream output1(outfile1.c_str());
      output1.write((const char*)&(*tb1)[0], tb1->size());
      output1.close();

      // 	iSetup.get<DQMXMLFileRcd>().get("XML2_mio",rootgeo);
      // 	std::cout<<"ROOT FILE IN MEMORY 2"<<std::endl;
      // 	std::unique_ptr<std::vector<unsigned char> > tb2( (*rootgeo).getUncompressedBlob() );
      // 	//here you can implement the stream for putting the TFile on disk...
      // 	std::string outfile2("XML2_retrieved.xml") ;
      // 	std::ofstream output2(outfile2.c_str()) ;
      // 	output2.write((const char *)&(*tb2)[0], tb2->size());
      // 	output2.close() ;
      //	std::unique_ptr<std::vector<unsigned char> > tb( (*rootgeo).getUncompressedBlob() );
    }
  }

  DEFINE_FWK_MODULE(DQMXMLFileEventSetupAnalyzer);
}  // namespace edmtest
