// =====================================================================================================================
//  fennec, a free and open source game engine
//  Copyright © 2025  Medusa Slockbower
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU 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 General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================

#ifndef FENNEC_LANGPROC_IO_PATH_H
#define FENNEC_LANGPROC_IO_PATH_H

#include <fennec/langproc/strings/string.h>

namespace fennec
{

///
/// \brief struct for handling file paths
///
/// \details This structure makes no guarantees about the validity of a path.
///          Operations do not examine the system's file structure.
struct path
{
public:
// Static Functions ====================================================================================================

	/// \brief Get the current working directory
	/// \returns a path containing the absolute path to the working directory
	static path current();

	/// \brief Set the current working directory
	/// \param path the path to the new working directory
	/// \returns a path containing the absolute path to the working directory
	static path current(const path& path);


// Constructors ========================================================================================================

	///
	/// \brief Default Constructor, returns the root of the current working directory
	path() : _str("/") { }

	///
	/// \brief C-String Conversion Constructor
	/// \param str the cstring to convert
	path(const cstring& str)
		: _str(str) {
		while (not _str.empty() && _str[_str.size() - 1] == '/') {
			_str = _str.substring(0, str.size() - 1);
		}
	}

	///
	/// \brief String Conversion Constructor
	/// \param str the string to convert
	path(const string& str)
		: _str(str) {
		while (_str[_str.size() - 1] == '/') {
			_str = _str.substring(0, str.size() - 1);
		}
	}

	///
	/// \brief Path Copy Constructor
	/// \param p the path to copy
	path(const path& p)
		: _str(p._str) {
	}

	///
	/// \brief Path Move Constructor
	/// \param p the path to take ownership of
	path(path&& p) noexcept : _str(move(p._str)) { }


// Assignment Operators ================================================================================================

	///
	/// \brief C-String Assignment Operator
	/// \param p the cstring to assign
	/// \returns a reference to `this` after assigning `p`
	path& operator=(const cstring& p) {
		_str = p;
		return *this;
	}

	///
	/// \brief String Assignment Operator
	/// \param p the cstring to assign
	/// \returns a reference to `this` after assigning `p`
	path& operator=(const string& p) {
		_str = p;
		return *this;
	}

	///
	/// \brief Path Copy Assignment Operator
	/// \param p the path to copy
	/// \returns a reference to `this` after copying `p`
	path& operator=(const path& p) {
		_str = p._str;
		return *this;
	}

	///
	/// \brief Path Move Assignment Operator
	/// \param p the path to take ownership of
	/// \returns a reference to `this` after taking ownership of `p`
	path& operator=(path&& p) noexcept {
		_str = move(p._str);
		return *this;
	}


// Append Operators ====================================================================================================

	///
	/// \brief
	/// \param str
	/// \return
	path operator/(const cstring& str) const {
		return path(_str + '/' + str);
	}

	path operator/(const string& str) const {
		return path(_str + '/' + str);
	}

	path operator/(const path& p) const {
		return path(_str + '/' + p._str);
	}

	bool operator==(const path& p) const {
		return _str == p._str;
	}

	const string& str() const { return _str; }

	bool empty() {
		size_t size = _str.size();
		if (size == 0) return true;
#if FENNEC_PLATFORM_WINDOWS
		return (_str[1] == ':' && size == 3);
#else
		return (_str[0] == '/' && size == 1);
#endif
	}

	path parent() const {
#ifdef FENNEC_PLATFORM_WINDOWS
		size_t start = _str.size() - 1;
		start = _str[start] == '/' || _str[start] == '\\' ? start - 1 : start;

		size_t r = _str.rfind('/', start);
		size_t l = _str.rfind('\\', start);
		if (r == _str.size()) {
			start = l;
		}
		else if (l == _str.size()) {
			start = r;
		}
		else {
			start = max(r, l);
		}
		return _str.substring(0, start);
#else
		size_t start = _str.size() - 1;
		start = _str[start] == '/' ? start - 1 : start;
		return _str.substring(0, _str.rfind('/', start));
#endif
	}

	path absolute() const {
		path parse = *this;
		path working; working._str.resize(0);

// Check if this is a rooted path;
#ifdef FENNEC_PLATFORM_WINDOWS
		if (_str[1] != ':') {
#else
		if (_str[0] != '/') {
#endif
			working = current();
		}

		while (not parse.empty()) {
			// Handle dots
			while (parse._str[0] == '.') {
				// Check for ".."
				if (parse._str[1] == '.') {
					// ".."
					if (parse._str.size() == 2) {
						parse = path();
						working = working.parent();
					}
					// "../"
					else if (parse._str[2] == '/') {
						working = working.parent();
						parse._str = parse._str.substring(3);
					}
				}
				// "./"
				else if (parse._str[1] == '/') {
					parse._str = parse._str.substring(2);
				}
			}

			if (parse.empty()) break;

			// Push the path
			const size_t loc = parse._str.find('/');
			working._str += '/';
			working._str += parse._str.substring(0, loc);
			parse._str = parse._str.substring(loc + 1);
		}

		return working;
	}

private:
	string _str;
};

}

#endif // FENNEC_LANGPROC_IO_PATH_H
