//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
// G4tgrMaterialFactory implementation
//
// Author: P.Arce, CIEMAT (November 2007)
// --------------------------------------------------------------------

#include "G4tgrMaterialFactory.hh"
#include "G4tgrUtils.hh"
#include "G4tgrElementSimple.hh"
#include "G4tgrElementFromIsotopes.hh"
#include "G4tgrMaterialSimple.hh"
#include "G4tgrMaterialMixture.hh"
#include "G4tgrFileReader.hh"
#include "G4tgrMessenger.hh"

G4ThreadLocal G4tgrMaterialFactory* G4tgrMaterialFactory::theInstance = nullptr;

// --------------------------------------------------------------------
G4tgrMaterialFactory::G4tgrMaterialFactory()
{
}

// --------------------------------------------------------------------
G4tgrMaterialFactory* G4tgrMaterialFactory::GetInstance()
{
  if(theInstance == nullptr)
  {
    theInstance = new G4tgrMaterialFactory;
  }
  return theInstance;
}

// --------------------------------------------------------------------
G4tgrMaterialFactory::~G4tgrMaterialFactory()
{
  for(auto isotcite = theG4tgrIsotopes.cbegin();
           isotcite != theG4tgrIsotopes.cend(); ++isotcite)
  {
    delete(*isotcite).second;
  }
  theG4tgrIsotopes.clear();

  for(auto elemcite = theG4tgrElements.cbegin();
           elemcite != theG4tgrElements.cend(); ++elemcite)
  {
    delete(*elemcite).second;
  }
  theG4tgrElements.clear();

  for(auto matcite = theG4tgrMaterials.cbegin();
           matcite != theG4tgrMaterials.cend(); ++matcite)
  {
    delete(*matcite).second;
  }
  theG4tgrMaterials.clear();
  delete theInstance;
}

// --------------------------------------------------------------------
G4tgrIsotope* G4tgrMaterialFactory::AddIsotope(const std::vector<G4String>& wl)
{
  //---------- Look if isotope exists
  if(FindIsotope(G4tgrUtils::GetString(wl[1])) != 0)
  {
    ErrorAlreadyExists("isotope", wl);
  }

  G4tgrIsotope* isot = new G4tgrIsotope(wl);
  theG4tgrIsotopes[isot->GetName()] = isot;

  return isot;
}

// --------------------------------------------------------------------
G4tgrElementSimple*
G4tgrMaterialFactory::AddElementSimple(const std::vector<G4String>& wl)
{
  //---------- Look if element exists
  if(FindElement(G4tgrUtils::GetString(wl[1])) != 0)
  {
    ErrorAlreadyExists("element", wl);
  }

  G4tgrElementSimple* elem = new G4tgrElementSimple(wl);
  theG4tgrElements[elem->GetName()] = elem;

  return elem;
}

// --------------------------------------------------------------------
G4tgrElementFromIsotopes*
G4tgrMaterialFactory::AddElementFromIsotopes(const std::vector<G4String>& wl)
{
  //---------- Look if element exists
  if(FindElement(G4tgrUtils::GetString(wl[1])) != 0)
  {
    ErrorAlreadyExists("element", wl);
  }

  G4tgrElementFromIsotopes* elem = new G4tgrElementFromIsotopes(wl);
  theG4tgrElements[elem->GetName()] = elem;

  return elem;
}

// --------------------------------------------------------------------
G4tgrMaterialSimple*
G4tgrMaterialFactory::AddMaterialSimple(const std::vector<G4String>& wl)
{
#ifdef G4VERBOSE
  if(G4tgrMessenger::GetVerboseLevel() >= 2)
  {
    G4cout << " G4tgrMaterialFactory::AddMaterialSimple" << wl[1] << G4endl;
  }
#endif

  //---------- Look if material exists
  if(FindMaterial(G4tgrUtils::GetString(wl[1])) != 0)
  {
    ErrorAlreadyExists("material simple", wl);
  }

  G4tgrMaterialSimple* mate = new G4tgrMaterialSimple("MaterialSimple", wl);

  //---------- register this material
  theG4tgrMaterials[mate->GetName()] = mate;

  return mate;
}

// --------------------------------------------------------------------
G4tgrMaterialMixture*
G4tgrMaterialFactory::AddMaterialMixture(const std::vector<G4String>& wl,
                                         const G4String& mixtType)
{
#ifdef G4VERBOSE
  if(G4tgrMessenger::GetVerboseLevel() >= 2)
  {
    G4cout << " G4tgrMaterialFactory::AddMaterialMixture " << wl[1] << G4endl;
  }
#endif

  //---------- Look if material already exists
  if(FindMaterial(G4tgrUtils::GetString(wl[1])) != 0)
  {
    ErrorAlreadyExists("material mixture", wl);
  }

  G4tgrMaterialMixture* mate;
  mate = new G4tgrMaterialMixture(mixtType, wl);

  //---------- register this material
  theG4tgrMaterials[mate->GetName()] = mate;

  return mate;
}

// --------------------------------------------------------------------
G4tgrIsotope* G4tgrMaterialFactory::FindIsotope(const G4String& name) const
{
#ifdef G4VERBOSE
  if(G4tgrMessenger::GetVerboseLevel() >= 3)
  {
    G4cout << " G4tgrMaterialFactory::FindIsotope() - " << name << G4endl;
  }
#endif

  G4mstgrisot::const_iterator cite;
  cite = theG4tgrIsotopes.find(name);
  if(cite == theG4tgrIsotopes.cend())
  {
    return nullptr;
  }
  else
  {
#ifdef G4VERBOSE
    if(G4tgrMessenger::GetVerboseLevel() >= 3)
    {
      G4cout << " G4tgrIsotope found: " << ((*cite).second)->GetName()
             << G4endl;
    }
#endif
    return (*cite).second;
  }
}

// --------------------------------------------------------------------
G4tgrElement* G4tgrMaterialFactory::FindElement(const G4String& name) const
{
#ifdef G4VERBOSE
  if(G4tgrMessenger::GetVerboseLevel() >= 3)
  {
    G4cout << " G4tgrMaterialFactory::FindElement() - " << name << G4endl;
  }
#endif
  G4mstgrelem::const_iterator cite;
  cite = theG4tgrElements.find(name);
  if(cite == theG4tgrElements.cend())
  {
    return nullptr;
  }
  else
  {
#ifdef G4VERBOSE
    if(G4tgrMessenger::GetVerboseLevel() >= 3)
    {
      DumpElementList();
      G4cout << " G4tgrElement found: " << ((*cite).second)->GetName()
             << G4endl;
    }
#endif
    return (*cite).second;
  }
}

// --------------------------------------------------------------------
G4tgrMaterial* G4tgrMaterialFactory::FindMaterial(const G4String& name) const
{
#ifdef G4VERBOSE
  if(G4tgrMessenger::GetVerboseLevel() >= 3)
  {
    G4cout << " G4tgrMaterialFactory::FindMaterial() - " << name << G4endl;
  }
#endif
  G4mstgrmate::const_iterator cite;
  cite = theG4tgrMaterials.find(name);
  if(cite == theG4tgrMaterials.cend())
  {
    return nullptr;
  }
  else
  {
    return (*cite).second;
  }
}

// --------------------------------------------------------------------
void G4tgrMaterialFactory::DumpIsotopeList() const
{
  G4cout << " @@@@@@@@@@@@@@@@ DUMPING G4tgrIsotope's List " << G4endl;
  for(auto cite = theG4tgrIsotopes.cbegin();
           cite != theG4tgrIsotopes.cend(); ++cite)
  {
    G4cout << " ISOT: " << (*cite).second->GetName() << G4endl;
  }
}

// --------------------------------------------------------------------
void G4tgrMaterialFactory::DumpElementList() const
{
  G4cout << " @@@@@@@@@@@@@@@@ DUMPING G4tgrElement's List " << G4endl;
  for(auto cite = theG4tgrElements.cbegin();
           cite != theG4tgrElements.cend(); ++cite)
  {
    G4cout << " ELEM: " << (*cite).second->GetName() << G4endl;
  }
}

// --------------------------------------------------------------------
void G4tgrMaterialFactory::DumpMaterialList() const
{
  G4cout << " @@@@@@@@@@@@@@@@ DUMPING G4tgrMaterial's List " << G4endl;
  for(auto cite = theG4tgrMaterials.cbegin();
           cite != theG4tgrMaterials.cend(); ++cite)
  {
    G4tgrMaterial* mate = (*cite).second;
    G4cout << " MATE: " << mate->GetName() << " Type: " << mate->GetType()
           << " NoComponents= " << mate->GetNumberOfComponents() << G4endl;
  }
}

// --------------------------------------------------------------------
void G4tgrMaterialFactory::ErrorAlreadyExists(const G4String& object,
                                              const std::vector<G4String>& wl,
                                              const G4bool bNoRepeating)
{
  G4String msg = object + G4String(" repeated");
  if(bNoRepeating)
  {
    G4tgrUtils::DumpVS(wl, (G4String("!!!! EXITING: ") + msg).c_str());
    G4Exception("G4tgrMaterialFactory", "FatalError", FatalException,
                "Aborting...");
  }
  else
  {
#ifdef G4VERBOSE
    if(G4tgrMessenger::GetVerboseLevel() >= 1)
    {
      G4tgrUtils::DumpVS(wl, (G4String("!! WARNING: ") + msg).c_str());
    }
#endif
  }
}
