/*
 * This file is part of VoidArchiveTool.
 *
 * Copyright (C) 2025 Yanczi
 *
 * Void Archive Toolis free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "ViewCargo.h"

ViewCargo::ViewCargo()
{}

//-----------------------------------------------------------------------------
//   Wywoływanie
//-----------------------------------------------------------------------------
bool ViewCargo::View(const std::string& path)
{
	//Sprawdź czy plik istnieje
	if (!std::filesystem::exists(path))
	{
		std::cerr << "Error: The given file is not exist!" << std::endl;
		return false;
	}

	//Sprawdź czy plik jets plikiem
	if (!std::filesystem::is_regular_file(path))
	{
		std::cerr << "Error: The given file is a directory!" << std::endl;
		return false;
	}

	//Sprawdź czy kontener jest prawidłowy
	if (!ViewFiles(path))
	{
		std::cerr << "Nie prawidlowa struktura kontenera Void" << std::endl;
		return false;
	}

	return true;
}

//-----------------------------------------------------------------------------
//  Sprawdzenie poprawności kontenera
//-----------------------------------------------------------------------------
bool ViewCargo::ViewFiles(const std::string& path)
{
	uint64_t tabPos = 0;
	uint32_t tabSize = 0;

	const std::string signature = SIGNATURE;
	std::vector<char> magic(signature.length());
	int8_t cargoVer = 0;

	std::ifstream cargo(path, std::ios::binary);

	if (!cargo.is_open())
	{
		std::cerr << "Error: Failed to open container" << std::endl;
		return false;
	}

	//---------------------------------------------------------------
	// Odczytywanie pierwszych 16 bajtów nagłówka pliku
	// 4 Sygnatura kontenera XPAK
	// 8 Offset tablicy plików
	// 4 Rozmiar tablicy plików
	//---------------------------------------------------------------
	cargo.read(magic.data(), magic.size());
	cargo.read(reinterpret_cast<char*>(&tabPos), sizeof(tabPos));
	cargo.read(reinterpret_cast<char*>(&tabSize), sizeof(tabSize));

	//Sprawdź czy kontener ma poprawną sygnature
	if (std::string(magic.begin(), magic.end()) != signature)
	{
		std::cerr << "Error: Corrupted Cargo" << std::endl;
		cargo.close();
		return false;
	}

	std::cout << "ZIP" << " " << "ENC" << " " << "Path" << std::endl;

	// Przeskocz do tablicy plików
	cargo.seekg(tabPos);

	// Załaduj dane o plikach
	for (uint32_t i = 0; i < tabSize; ++i)
	{
		FilesTable fhTmp;
		cargo.read(reinterpret_cast<char*>(&fhTmp.nameLen), sizeof(fhTmp.nameLen));

		std::vector<char> nameBuffor(fhTmp.nameLen);
		cargo.read(nameBuffor.data(), fhTmp.nameLen);
		fhTmp.nameFile = std::string(nameBuffor.begin(), nameBuffor.end());

		cargo.read(reinterpret_cast<char*>(&fhTmp.offset), sizeof(fhTmp.offset));
		cargo.read(reinterpret_cast<char*>(&fhTmp.size), sizeof(fhTmp.size));
		cargo.read(reinterpret_cast<char*>(&fhTmp.crc), sizeof(fhTmp.crc));
		cargo.read(reinterpret_cast<char*>(&fhTmp.flag), sizeof(fhTmp.flag));

		//Tworzenie wierszy tabeli
		ShowFile(fhTmp.nameFile, fhTmp.flag);
	}

	cargo.close();

	return true;
}

//-----------------------------------------------------------------------------
//  Generowanie wierszy do tabeli
//-----------------------------------------------------------------------------
void ViewCargo::ShowFile(const std::string& file, const uint8_t& flag)
{
	std::string compresedCheck = "[ ]";
	std::string encryptedCheck = "[ ]";

	// Ustawianie checkboxów
	switch (flag)
	{
	case FILE_FLAG_COMPRESS:
		compresedCheck = "[x]";
		break;

	case FILE_FLAG_ENCRYPT:
		encryptedCheck = "[x]";
		break;

	case FILE_FLAG_ZIPENC:
		compresedCheck = "[x]";
		encryptedCheck = "[x]";
		break;

	default:
		compresedCheck = "[ ]";
		encryptedCheck = "[ ]";
		break;
	}

	// Wyświetlanie
	std::cout << compresedCheck << " " << encryptedCheck << " " << file << std::endl;
}
