#ifndef _CSCBADCHAMBERSCONDITIONS_H
#define _CSCBADCHAMBERSCONDITIONS_H

#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ESProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/SourceFactory.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include <cmath>
#include <memory>

#include "CondFormats/CSCObjects/interface/CSCBadChambers.h"
#include "CondFormats/DataRecord/interface/CSCBadChambersRcd.h"
#include <DataFormats/MuonDetId/interface/CSCDetId.h>

class CSCBadChambersConditions : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
public:
  CSCBadChambersConditions(const edm::ParameterSet &);
  ~CSCBadChambersConditions() override;

  inline static CSCBadChambers *prefillBadChambers();

  typedef std::unique_ptr<CSCBadChambers> ReturnType;

  ReturnType produceBadChambers(const CSCBadChambersRcd &);

private:
  // ----------member data ---------------------------
  void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &,
                      const edm::IOVSyncValue &,
                      edm::ValidityInterval &) override;
  CSCBadChambers *cndbBadChambers;
};

#include <fstream>
#include <iostream>
#include <vector>

// to workaround plugin library
inline CSCBadChambers *CSCBadChambersConditions::prefillBadChambers() {
  //  const int MAX_SIZE = 468;
  // cndbbadchambers = new CSCBadChambers();

  int new_chambers;
  std::vector<int> new_badchambers;
  int old_chamber = -1;
  int new_nrlines;
  new_nrlines = 0;

  std::ifstream newdata;

  newdata.open("badchambers.dat", std::ios::in);
  if (!newdata) {
    std::cerr << "Error: badchambers.dat -> no such file!" << std::endl;
    exit(1);
  }

  while (!newdata.eof()) {
    newdata >> new_chambers;
    if (new_chambers != old_chamber) {
      new_badchambers.push_back(new_chambers);
      std::cout << new_chambers << std::endl;
      ++new_nrlines;
    }
    old_chamber = new_chambers;
  }
  newdata.close();

  CSCBadChambers *cndbbadchambers = new CSCBadChambers(new_nrlines, new_badchambers);

  // std::cout <<"numberOfBadChambers "<<new_nrlines<<std::endl;

  return cndbbadchambers;
}

#endif
