#include "aes.hpp"
#include "crypt.hpp"

int main(int argc, char* argv[]) {
    if(argc != 2) {
        std::cerr << "No file argument supplied.\n";
        return 1;
    }

    char* filename = argv[1];
    aes* O = new crypt(filename);

    // Read the file to encrypt
    unsigned char* file;
    // readFile fait l'aloccation mémoire !!! pensé au free
    size_t fileLength = O->readFile(&file);
    printf("%d bytes to be encrypted\n", (int)fileLength);
    
    // Encrypt the file
    unsigned char *encryptedFile;
    int encryptedFileLength;
    
    if((encryptedFileLength = (dynamic_cast<crypt*>(O))->aesEncrypt((const unsigned char*)file, fileLength, &encryptedFile) ) == -1) {
        fprintf(stderr, "Encryption failed\n");
        return 1;
    }

    printf("%d bytes encrypted\n", encryptedFileLength);
    std::cerr << "mainCrypt.ex : " << O->PrintAesKey() << std::endl;

    std::string keyFilename = O->exportKey();
    // std::string keyFilename = O->writeFile(O->getAesKey(), (O->PrintAesKey()).length(), "aesKey.key");
    printf("Encrypt key written to \"%s\"\n", keyFilename.c_str());

    // Write the encrypted file to its own file
    std::string encryptedFilename = O->writeFile(encryptedFile, encryptedFileLength);
    printf("Encrypted message written to \"%s\"\n", encryptedFilename.c_str());

    free(encryptedFile);
    free(file);
    delete O;

    return 0;
}