#include "aes.hpp"
#include "decrypt.hpp"

int main(int argc, char* argv[]) {
    if(argc != 3) {
        std::cerr << "An input file is missing.\n ./Decrypt.ex fileToDecrypt AeskeyForDecrypt\n";
        return 1;
    }
    
    size_t fileLength;
    unsigned char* fileEncrypted;
    unsigned char* decryptedFile;

    aes* O2 = new decrypt(argv[1]);
    O2->importKey(argv[2]);

    fileLength = O2->readFile(&fileEncrypted);
    std::cerr << "byte to be decrypted : " << fileLength << std::endl;

    int decryptedFileLength = (dynamic_cast<decrypt*>(O2))->aesDecrypt(fileEncrypted, fileLength, &decryptedFile);
    if( decryptedFileLength == -1) {
        fprintf(stderr, "Decryption failed\n");
        return 1;
    }
    
    printf("%d bytes decrypted\n", (int)decryptedFileLength);
    std::cerr << O2->PrintAesKey() << std::endl;

    // Write the decrypted file to its own file
    std::string decryptedFilename = O2->writeFile(decryptedFile, decryptedFileLength);
    printf("Decrypted file written to \"%s\"\n", decryptedFilename.c_str());

    free(decryptedFile);
    free(fileEncrypted);
    delete O2;

    return 0;
}