/*
 * 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/>.
 */

 //============================================================================
 // Name        : Void Archive Tool
 // Author      : Yanczi
 // Version     : 1.1
 // Copyright   : Yanczi 19-06-2025
 // Description : Void Archive Tool. Packing and extract files
 //============================================================================

#include <iostream>
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp>

#include "CreateCargo.h"
#include "ExtractCargo.h"
#include "ViewCargo.h"
#include "Interface.h"

void RenderHelp()
{
	const std::string HelpTitle = "< Manual >";
	const std::string HelpInstruction =
		"pakcmd <parametr> <catalog>                                                                 \n"
		"                                                                                            \n"
		"  \-c  Compressing                                                                          \n"
		"  \-r  Raw files                                                                            \n"
		"  \-e  Encrypted                                                                            \n"
		"  \-s  Compressing and Encrypted                                                            \n"
		"  \-f  Pack the files according to the guidelines given in the \<directory\>.json           \n"
		"                                                                                            \n"
		"Extracting:                                                                                 \n"
		"  -x  Extract files from the specified container                                            \n"
		"                                                                                            \n"
		"Others:                                                                                     \n"
		"  -ls List files stored in a container                                                      \n"
		"                                                                                            \n"
		"<catalog>.json                                                                              \n"
		"                                                                                            \n"
		"Keys:                                                                                       \n"
		"                                                                                            \n"
		" {compress}      - Compressing files                                                        \n"
		" {crypt}         - Encrypted files                                                          \n"
		" {ignore}        - Ignoring concrete files                                                  \n"
		"                                                                                            \n"
		" /path/to/file.ext - Concrete file                                                          \n"
		" *.ext             - All files with concrete extension                                      \n"
		" *.*               - All files !NOT WORKING WITH {ignore} KEY!                              \n";

	//Interface tui;
	//tui.TextBorder(HelpTitle, HelpInstruction);
}

static bool EmptyPath(std::string path)
{
	if (path == "")
	{
		std::cerr << "Error: Nie podano sciezki!" << std::endl;
		return false;
	}

	return true;
}

int main(int argc, char* argv[]) {
	std::string path = "";

	std::cout <<
		"                  8888888b.     d8888 888    d8P \n"
		"                  888   Y88b   d88888 888   d8P  \n"
		"                  888    888  d88P888 888  d8P   \n"
		" .d88b.  888  888 888   d88P d88P 888 888d88K    \n"
		"d8P  Y8b `Y8bd8P' 8888888P\" d88P  888 8888888b   \n" 
		"88888888   X88K   888      d88P   888 888  Y88b  \n"
		"Y8b.     .d8\"\"8b. 888     d8888888888 888   Y88b \n"
		" \"Y8888  888  888 888    d88P     888 888    Y88b\n"
	<< std::endl;
	std::cout << "\n" << PROGRAM_VERSION << " Release " << PROGRAM_COMPILING << std::endl;
	std::cout << "Author: " << PROGRAM_AUTHOR << std::endl;
	std::cout << "License: " << PROGRAM_LICENSE << "\n" << std::endl;

	CreateCargo cargo;
	ExtractCargo extract;
	ViewCargo viewCargo;

	for (int i = 0; i < argc; ++i)
	{
		std::string arg = argv[i];

		if (arg == "-c" && i + 1 < argc)
		{
			path = argv[i + 1];

			if (!EmptyPath(path)) { return 1; }

			if (!cargo.Create(path, 1))
			{
				return 1;
			}
			i++;
		}

		if (arg == "-r" && i + 1 < argc)
		{
			path = argv[i + 1];
			if (!cargo.Create(path, 0))
			{
				return 1;
			}
			i++;
		}

		if (arg == "-e" && i + 1 < argc)
		{
			path = argv[i + 1];
			if (!cargo.Create(path, 2))
			{
				return 1;
			}
			i++;
		}

		if (arg == "-s" && i + 1 < argc)
		{
			path = argv[i + 1];
			if (!cargo.Create(path, 3))
			{
				return 1;
			}
			i++;
		}

		if (arg == "-f" && i + 1 < argc)
		{
			path = argv[i + 1];
			if (!EmptyPath(path)) { return 1; }
			if (!cargo.Create(path, -1))
			{
				return 1;
			}
			i++;
		}

		if (arg == "-x" && i + 1 < argc)
		{
			path = argv[i + 1];
			if (!EmptyPath(path)) { return 1; }
			if (!extract.Extract(path))
			{
				return 1;
			}
			i++;
		}

		if (arg == "-ls" && i + 1 < argc)
		{
			path = argv[i + 1];
			if (!EmptyPath(path)) { return 1; }
			if (!viewCargo.View(path))
			{
				return 1;
			}
			i++;
		}

		if (arg == "-help")
		{
			//std::cout << helpText << std::endl;
			RenderHelp();
			return 0;
			i++;
		}
	}

	return 0;
}