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

#include <ls-std/core/ConditionalFunctionExecutor.hpp>
#include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
#include <ls-std/core/exception/IllegalArgumentException.hpp>
#include <ls-std/io/section-pair/evaluator/SectionPairIdentifierArgumentEvaluator.hpp>
#include <ls-std/io/section-pair/model/SectionPairRow.hpp>
#include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
#include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
#include <ls-std/io/section-pair/serialization/SerializableSectionPairRow.hpp>

using ls::standard::core::Class;
using ls::standard::core::ConditionalFunctionExecutor;
using ls::standard::core::EmptyStringArgumentEvaluator;
using ls::standard::core::IllegalArgumentException;
using ls::standard::core::type::byte_field;
using ls::standard::io::section_pair_identifier;
using ls::standard::io::section_pair_row_value;
using ls::standard::io::SectionPairIdentifierArgumentEvaluator;
using ls::standard::io::SectionPairRow;
using ls::standard::io::SectionPairRowEnumType;
using ls::standard::io::SectionPairRowListValue;
using ls::standard::io::SectionPairRowSingleValue;
using ls::standard::io::SectionPairRowValue;
using ls::standard::io::SerializableSectionPairParameter;
using ls::standard::io::SerializableSectionPairRow;
using std::make_shared;
using std::shared_ptr;
using std::string;
using std::string_view;

SectionPairRow::SectionPairRow(const section_pair_identifier &_key, const SectionPairRowEnumType &_type) : Class("SectionPairRow")
{
  this->_setKey(_key);
  this->_initValue(_type);
}

SectionPairRow::~SectionPairRow() noexcept = default;

section_pair_row_value SectionPairRow::getKey() const
{
  return this->key;
}

shared_ptr<SectionPairRowValue> SectionPairRow::getValue() const
{
  return this->value;
}

bool SectionPairRow::isList() const
{
  return this->value->getType() == SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE;
}

bool SectionPairRow::isSingleValue() const
{
  return this->value->getType() == SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE;
}

byte_field SectionPairRow::marshal()
{
  ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  return this->serializable->marshal();
}

void SectionPairRow::reserveNewLine(const string_view _reservedNewLine)
{
  this->reservedNewLine = _reservedNewLine;
}

void SectionPairRow::setKey(const section_pair_identifier &_key)
{
  this->_setKey(_key);
}

void SectionPairRow::unmarshal(const byte_field &_data)
{
  ConditionalFunctionExecutor{this->serializable == nullptr}.execute([this] { _createSerializable(); });
  this->serializable->unmarshal(_data);
}

void SectionPairRow::_createSerializable()
{
  SerializableSectionPairParameter parameter{};
  parameter.setValue(shared_from_this());

  if (!this->reservedNewLine.empty())
  {
    parameter.setNewLine(this->reservedNewLine);
  }

  this->serializable = make_shared<SerializableSectionPairRow>(parameter);
}

void SectionPairRow::_initValue(const SectionPairRowEnumType &_type)
{
  switch (_type)
  {
    case SectionPairRowEnumType::SECTION_PAIR_ROW_NOT_IMPLEMENTED:
    {
      throw IllegalArgumentException{this->getClassName() + ": default row enum type can not be set!"};
    }
    case SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE:
    {
      this->value = make_shared<SectionPairRowListValue>();
    }
    break;
    case SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE:
    {
      this->value = make_shared<SectionPairRowSingleValue>("empty");
    }
    break;
  }
}

void SectionPairRow::_setKey(const section_pair_identifier &_key)
{
  EmptyStringArgumentEvaluator{_key, this->getClassName() + ": passed key identifier is empty!"}.evaluate();
  SectionPairIdentifierArgumentEvaluator(_key).evaluate();
  this->key = _key;
}
