// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef toolx_raxml
#define toolx_raxml

#include <tools/xml/aidas>

#include "xml/loader"

namespace toolx {

class raxml : public tools::xml::aidas, public xml::loader {
  typedef tools::xml::aidas parent_aidas;
  typedef xml::loader parent;
public:
  raxml(tools::xml::factory& a_fac,std::ostream& a_out,bool a_verbose = false)
  :parent_aidas()
  ,parent(a_fac,a_out,a_verbose)
  ,m_read_tag(0)
  {
    set_default_tags(parent::m_tags);
  }
  virtual ~raxml() {}
public:
  void set_read_tag(void* a_tag) {m_read_tag = a_tag;}
protected:
  raxml(const raxml& a_from)
  :parent_aidas(a_from)
  ,parent(a_from)
  {}
  raxml& operator=(const raxml& a_from){
    parent_aidas::operator=(a_from);
    parent::operator=(a_from);
    return *this;
  }
public:
  bool load_file(const std::string& a_file,bool a_compressed){
    m_objects.clear();
    if(!parent::load_file(a_file,a_compressed)) return false;
    tools::xml::tree* top = top_item();
    if(!top) return false;
    const std::string& tag_name = top->tag_name();
    if(tag_name!=s_aida()) return false;

   {tools::xml::looper _for(*top);
    while(tools::xml::tree* _tree = _for.next_tree()) {

      const std::string& _tag_name = _tree->tag_name();
      reader rder = find_reader(_tag_name);
      if(!rder) {
        m_out << "toolx::raxml::load_file :"
              << " reader not found for " << tools::sout(_tag_name)
              << std::endl;
        return false;
      } else {
        tools::raxml_out ro = rder(*_tree,m_out,m_verbose,m_read_tag);
        if(ro.object()) m_objects.push_back(ro);
      }

    }}

    return true;
  }
protected:
  void* m_read_tag;
};

}

#endif
