/*
 * Author:          Patrick-Christopher Mattulat
 * Co-Author:       Claude Sonnet 4.6 (LLM)
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2020-10-10
 * Changed:         2026-06-23
 *
 * */

#include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
#include <ls-std/core/exception/IllegalArgumentException.hpp>
#include <ls-std/io/FileReader.hpp>
#include <ls-std/io/xml/XmlParser.hpp>
#include <ls-std/io/xml/XmlReader.hpp>

using ls::standard::core::Class;
using ls::standard::core::IllegalArgumentException;
using ls::standard::core::NullPointerArgumentEvaluator;
using ls::standard::core::type::byte_field;
using ls::standard::io::File;
using ls::standard::io::FileReader;
using ls::standard::io::XmlDocument;
using ls::standard::io::XmlParser;
using ls::standard::io::XmlReader;
using std::shared_ptr;
using std::string;

XmlReader::XmlReader(const shared_ptr<XmlDocument> &_document, const string &_absolutePath) : Class("XmlReader")
{
  this->_assignDocument(_document);
  this->_assignFile(File{_absolutePath});
}

XmlReader::~XmlReader() noexcept = default;

byte_field XmlReader::read()
{
  byte_field data = FileReader{this->xmlFile}.read();
  XmlParser{this->document}.parse(data);

  return data;
}

shared_ptr<XmlDocument> XmlReader::getDocument() const
{
  return this->document;
}

void XmlReader::setDocument(const shared_ptr<XmlDocument> &_document)
{
  this->_assignDocument(_document);
}

void XmlReader::setFile(const File &_xmlFile)
{
  this->_assignFile(_xmlFile);
}

void XmlReader::_assignDocument(const shared_ptr<XmlDocument> &_document)
{
  NullPointerArgumentEvaluator{_document, "xml document reference is null!"}.evaluate();
  this->document = _document;
}

void XmlReader::_assignFile(const File &_xmlFile)
{
  if (!_xmlFile.exists())
  {
    throw IllegalArgumentException{"file does not exist: " + _xmlFile.getAbsoluteFilePath()};
  }

  this->xmlFile = _xmlFile;
}
