#include "Packet.hpp"

#include <stdexcept>

#include "SecurityMode.hpp"


namespace drp::packet::base {


/*
GenericPacketContent decryptPacketContentAes(const GenericPacket& packet) {
    GenericPacketContent decryptedPacketContent {};

    const auto& [key, iv] = keysAes[serverAddress];

    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    if (EVP_DecryptInit_ex(
        ctx,
        EVP_aes_256_cbc(),
        nullptr,
        key,
        iv
    ) != 1)
        throw std::runtime_error("[Client] Could not initialize the EVP_CIPHER_CTX.");

    int packetContentLength;

    if (EVP_DecryptUpdate(
        ctx,
        reinterpret_cast<std::uint8_t*>(&decryptedPacketContent),
        &packetContentLength,
        reinterpret_cast<const std::uint8_t*>(&packet.encryptedContent),
        sizeof(packet)
    ) != 1)
        throw std::runtime_error("[Client] Could not encrypt the plaintext.");

    if (EVP_DecryptFinal_ex(
        ctx,
        reinterpret_cast<std::uint8_t*>(&decryptedPacketContent + packetContentLength),
        &packetContentLength
    ) != 1)
        throw std::runtime_error("[Client] Could not decrypt the final plaintext.");

    EVP_CIPHER_CTX_free(ctx);

    return decryptedPacketContent;
}
*/


PacketContent Packet::getContent() const {
    // TODO(Faraphel): implement RSA and AES
    //   additional "context" argument to hold cryptographic keys ?

    switch (static_cast<SecurityMode>(this->securityMode)) {
        case SecurityMode::PLAIN:
            return this->_content;

        case SecurityMode::AES:
            // return decryptPacketContentAes(packet);
            throw std::runtime_error("Not implemented.");

        case SecurityMode::RSA:
            throw std::runtime_error("Not implemented.");

        default:
            throw std::runtime_error("Unsupported security mode.");
    }
}


}
