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

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

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

namespace
{
  class StorableFileTest : public Test
  {
    public:

      StorableFileTest() = default;
      ~StorableFileTest() override = default;

      string fileLocation = TestHelper::getResourcesFolderLocation() + "simple.txt";
  };

  TEST_F(StorableFileTest, getFile)
  {
    const StorableFile storableFile{this->fileLocation};
    ASSERT_STREQ(this->fileLocation.c_str(), storableFile.getFile()->getAbsoluteFilePath().c_str());
  }

  TEST_F(StorableFileTest, load)
  {
    StorableFile storableFile{this->fileLocation};
    const byte_field content = storableFile.load();

    const string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
    const string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();

    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  }

  TEST_F(StorableFileTest, reset)
  {
    StorableFile storableFile{this->fileLocation};
    byte_field content = storableFile.load();

    string expectedUnix = "Hello!" + NewLine::getUnixNewLine();
    string expectedWindows = "Hello!" + NewLine::getWindowsNewLine();

    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);

    // reset

    const string anotherFileLocation = TestHelper::getResourcesFolderLocation() + "list-test/bla.txt";
    storableFile.reset(anotherFileLocation);
    content = storableFile.load();

    expectedUnix = "nothing to say!" + NewLine::getUnixNewLine();
    expectedWindows = "nothing to say!" + NewLine::getWindowsNewLine();

    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
  }

  TEST_F(StorableFileTest, save)
  {
    const string path = TestHelper::getResourcesFolderLocation() + "tmp-storable-file.txt";
    const File file{path};
    file.createNewFile();

    StorableFile storableFile{path};
    const byte_field textUnix = "Testing save method!" + NewLine::getUnixNewLine();
    const byte_field textWindows = "Testing save method!" + NewLine::getWindowsNewLine();

    storableFile.save(textUnix);
    const byte_field content = storableFile.load();
    ASSERT_TRUE(content == textUnix || content == textWindows);

    file.remove();
  }
}
