#include "KM3Cathods.hh"
#include "G4SystemOfUnits.hh"


KM3Cathods::KM3Cathods() {
 IDs = new std::vector<G4int>;
 NumOfCathods = 0;
 }

KM3Cathods::~KM3Cathods() {
  for (size_t i = 0; i < theCathods.size(); i++) {
    theCathods[i]->Tree->clear();
    delete theCathods[i]->Tree;
    free(theCathods[i]);
  }
  theCathods.clear();
  delete IDs;
}

void KM3Cathods::addCathod(const G4Transform3D &trans, const G4ThreeVector &Pos,
                           const G4ThreeVector &Dir, const G4double Radius,
                           const G4double Height, const G4int Dep, const G4int detx_id) {
  Cathod *aCathod = (Cathod *)malloc(sizeof(Cathod));
  aCathod->trans = trans;
  aCathod->Position = Pos;
  aCathod->Direction = Dir;
  aCathod->Radius = Radius;
  aCathod->Height = Height;
  aCathod->Depth = Dep;
  //aCathod->ID = 0;
  IDs->push_back(detx_id);
  std::vector<G4int> *aTree = new std::vector<G4int>;
  aTree->reserve(Dep);
  aCathod->Tree = aTree;
  theCathods.push_back(aCathod);
  NumOfCathods++;
}

void KM3Cathods::addToTree(const G4int hist) {
  theCathods[NumOfCathods - 1]->Tree->push_back(hist);
  if ((G4int)(theCathods[NumOfCathods - 1]->Tree->size()) >
      theCathods[NumOfCathods - 1]->Depth)
    G4cout << "Warning: You add more History than declared" << G4endl;
}

// this method returns the cathod id  from the history tree
G4int KM3Cathods::GetCathodId(const G4int dep, const G4int hist[]) {
  G4int ih;
  ih = 0;
  iterator = 0;
  while (ih < dep) {
    while (hist[ih] != (*(theCathods[iterator]->Tree))[ih])
      iterator++;
    ih++;
  }
  return (*IDs)[iterator];
}

void KM3Cathods::SetCathodId(const G4int dep, const G4int hist[], G4int ID) {
  G4int ih;
  ih = 0;
  iterator = 0;
  while (ih < dep) {
    while (hist[ih] != (*(theCathods[iterator]->Tree))[ih])
      iterator++;
    ih++;
  }
  (*IDs)[iterator] = ID;
}

void KM3Cathods::PrintAllCathods(FILE *outfile) {
  for (G4int i = 0; i < NumOfCathods; i++) {
    fprintf(outfile, "%d\n", theCathods[i]->Depth);
    for (size_t ihi = 0; ihi < theCathods[i]->Depth; ihi++) {
      fprintf(outfile, "%d\n", (*(theCathods[i]->Tree))[ihi]);
    }
    fprintf(outfile, "%.6e %.6e %.6e %.6e %.6e %.6e\n",
            theCathods[i]->Position(0) / cm, theCathods[i]->Position(1) / cm,
            theCathods[i]->Position(2) / cm, theCathods[i]->Direction(0),
            theCathods[i]->Direction(1), theCathods[i]->Direction(2));
  }
}