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

#include <classes/TestHelper.hpp>
#include <gtest/gtest.h>
#include <ls-std/ls-std-core.hpp>
#include <ls-std/ls-std-io.hpp>

using ls::standard::core::FileNotFoundException;
using ls::standard::core::FileOperationException;
using ls::standard::core::type::byte_field;
using ls::standard::io::File;
using ls::standard::io::FileReader;
using ls::standard::io::NewLine;
using ls::standard::test::TestHelper;
using std::string;
using testing::Test;

namespace
{
  class FileReaderTest : public Test
  {
    public:

      FileReaderTest() = default;
      ~FileReaderTest() override = default;
  };

  TEST_F(FileReaderTest, constructor_file_does_not_exist)
  {
    const File file{TestHelper::getResourcesFolderLocation() + "does-not-exist.txt"};

    EXPECT_THROW(
        {
          try
          {
            FileReader reader{file};
          }
          catch (const FileNotFoundException &_exception)
          {
            throw;
          }
        },
        FileNotFoundException);
  }

  TEST_F(FileReaderTest, read)
  {
    const File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
    FileReader reader{file};
    const string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
    const string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();

    const byte_field content = reader.read();
    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  }

  TEST_F(FileReaderTest, read_file_gets_lost_in_between)
  {
    const File file{TestHelper::getResourcesFolderLocation() + "lost-readable-file.txt"};
    file.createNewFile();
    FileReader reader{file};
    file.remove();

    EXPECT_THROW(
        {
          try
          {
            byte_field content = reader.read();
          }
          catch (const FileOperationException &_exception)
          {
            throw;
          }
        },
        FileOperationException);
  }

  TEST_F(FileReaderTest, reset)
  {
    File file{TestHelper::getResourcesFolderLocation() + "simple.txt"};
    FileReader reader{file};
    string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
    string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();

    byte_field content = reader.read();
    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);

    File anotherFile{TestHelper::getResourcesFolderLocation() + "list-test/bla.txt"};
    reader.reset(anotherFile);
    expectedUnix = "nothing to say!" + NewLine::getUnixNewLine();
    expectedWindows = "nothing to say!" + NewLine::getWindowsNewLine();

    content = reader.read();
    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  }
}
