#ifndef ARBUTILS___OPTIONALBORROWEDPTR_HPP
#define ARBUTILS___OPTIONALBORROWEDPTR_HPP

#include "../Assert.hpp"

namespace ArbUt {
    /// @brief An optional borrowed pointer is used to indicate a pointer that is not owned by its holder. This pointer
    /// can be null.
    /// @details A borrowed pointer is used to indicate a pointer that is not owned by an object, but instead borrowed
    /// from another owning object that is assumed to always be kept alive during the entire lifetime of the borrowing
    //  object.
    template <class T> class OptionalBorrowedPtr {
    private:
        T* _raw;

    public:
        /// @brief Initialise a BorrowedPtr with a nullptr.
        inline OptionalBorrowedPtr<T>() : _raw(nullptr) {}
        /// @brief Initialise a BorrowedPtr with a specific raw pointer.
        inline OptionalBorrowedPtr<T>(T* ptr) : _raw(ptr){};
        /// @brief Initialise a BorrowedPtr from a copy.
        inline OptionalBorrowedPtr<T>(const BorrowedPtr<T>& other) : _raw(other._raw){};
        /// @brief Initialise a BorrowedPtr with a std unique_ptr.
        inline OptionalBorrowedPtr<T>(const std::unique_ptr<T>& other) : _raw(other.get()){};

        ~OptionalBorrowedPtr() noexcept = default;

        /// @brief Copy operator.
        inline OptionalBorrowedPtr<T>& operator=(const OptionalBorrowedPtr<T>& rhs) {
            if (this == &rhs)
                return *this;
            _raw = rhs._raw;
            return *this;
        }
        /// @brief Assign operator with raw pointer.
        inline OptionalBorrowedPtr<T>& operator=(T* rhs) {
            if (_raw == &rhs)
                return *this;
            _raw = rhs;
            return *this;
        }

        /// @brief Return whether the pointer is null or not.
        [[nodiscard]] inline bool HasValue() const noexcept { return _raw == nullptr; }

        /// @brief Get the raw underlying pointer.
        inline T* GetValue() const noexcept { return _raw; }

        /// @brief Check equality of two BorrowedPtr objects
        inline bool operator==(const OptionalBorrowedPtr<T>& rhs) const noexcept { return _raw == rhs._raw; }
        /// @brief Check equality of pointers
        inline bool operator==(T* rhs) const noexcept { return _raw == rhs; }
        /// @brief Check equality of two BorrowedPtr objects
        inline bool operator!=(const OptionalBorrowedPtr<T>& rhs) const noexcept { return _raw != rhs._raw; }
        /// @brief Check equality of pointers
        inline bool operator!=(T* rhs) const noexcept { return _raw == rhs; }

        /// @brief Returns a const version of the underlying pointer.
        inline OptionalBorrowedPtr<const T> Const() const noexcept { return BorrowedPtr<const T>(_raw); }

        /// @brief Casts the underlying pointer to another type using dynamic_cast.
        template <class TCast> inline OptionalBorrowedPtr<TCast> As() const {
            auto cast = dynamic_cast<TCast*>(_raw);
            return BorrowedPtr<TCast>(cast);
        }

        /// @brief Try to cast the underlying pointer to another type using dynamic_cast.
        template <class TCast> inline bool TryAs(OptionalBorrowedPtr<TCast>& out) const noexcept {
            auto cast = dynamic_cast<TCast*>(_raw);
            if (cast == nullptr)
                return false;
            out = OptionalBorrowedPtr<TCast>(cast);
            return true;
        }

        /// @brief Force cast the underlying pointer to another type using reinterpret_cast.
        template <class TCast> inline OptionalBorrowedPtr<TCast> ForceAs() const noexcept {
            auto cast = reinterpret_cast<TCast*>(_raw);
            return BorrowedPtr<TCast>(cast);
        }

        /// @brief Implicit cast to retrieve raw pointer.
        inline operator T*() const noexcept { return _raw; }
    };
}

namespace std {
    /// @brief Helper class for allowing hashing of BorrowedPtr.
    template <class T> struct hash<ArbUt::OptionalBorrowedPtr<T>> {
        /// @brief Returns a hash of for a borrowed Pointer. Effectively just the raw memory address.
        /// @param k A borrowed pointer.
        /// @return The hash of the borrowed pointer.
        std::size_t operator()(const ArbUt::OptionalBorrowedPtr<T>& k) const { return (size_t)k.GetValue(); }
    };
}

#endif // ARBUTILS___OPTIONALBORROWEDPTR_HPP