// SPDX-License-Identifier: Apache-2.0
// Copyright 2025 Simple Encryption Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "SimpleEncryption.h"
#include <iostream>
#include <string>

void DemonstrateCaesarCipher() {
    std::cout << "\n=== Caesar Cipher Demonstration ===" << std::endl;
    
    SimpleEncryption encryptor(5); // Default shift of 5
    
    std::string original_message = "Hello World! This is a secret message.";
    std::cout << "Original message: " << original_message << std::endl;
    
    // Encrypt with default key
    std::string encrypted_message = encryptor.EncryptCaesar(original_message);
    std::cout << "Encrypted message: " << encrypted_message << std::endl;
    
    // Decrypt the message
    std::string decrypted_message = encryptor.DecryptCaesar(encrypted_message);
    std::cout << "Decrypted message: " << decrypted_message << std::endl;
    
    // Encrypt with custom shift
    std::string custom_encrypted = encryptor.EncryptCaesar(original_message, 13);
    std::cout << "Custom encrypted (shift 13): " << custom_encrypted << std::endl;
    
    std::string custom_decrypted = encryptor.DecryptCaesar(custom_encrypted, 13);
    std::cout << "Custom decrypted: " << custom_decrypted << std::endl;
}

void DemonstrateXorEncryption() {
    std::cout << "\n=== XOR Encryption Demonstration ===" << std::endl;
    
    SimpleEncryption encryptor;
    
    std::string original_message = "This is a confidential document!";
    std::string encryption_key = "MySecretKey123";
    
    std::cout << "Original message: " << original_message << std::endl;
    std::cout << "Encryption key: " << encryption_key << std::endl;
    
    // Encrypt the message
    std::string encrypted_message = encryptor.EncryptXor(original_message, encryption_key);
    std::cout << "Encrypted message (hex): ";
    for (unsigned char byte : encrypted_message) {
        printf("%02x ", byte);
    }
    std::cout << std::endl;
    
    // Decrypt the message
    std::string decrypted_message = encryptor.DecryptXor(encrypted_message, encryption_key);
    std::cout << "Decrypted message: " << decrypted_message << std::endl;
}

void DemonstrateRandomKeyGeneration() {
    std::cout << "\n=== Random Key Generation Demonstration ===" << std::endl;
    
    SimpleEncryption encryptor;
    
    std::cout << "Generated random keys:" << std::endl;
    for (int i = 1; i <= 5; i++) {
        std::string random_key = encryptor.GenerateRandomKey(8);
        std::cout << "Key " << i << ": " << random_key << std::endl;
    }
}

void InteractiveMode() {
    std::cout << "\n=== Interactive Encryption Mode ===" << std::endl;
    
    SimpleEncryption encryptor;
    std::string user_input;
    int choice;
    
    std::cout << "Enter your message: ";
    std::getline(std::cin, user_input);
    
    if (!encryptor.IsValidText(user_input)) {
        std::cout << "Invalid input!" << std::endl;
        return;
    }
    
    std::cout << "\nChoose encryption method:" << std::endl;
    std::cout << "1. Caesar Cipher" << std::endl;
    std::cout << "2. XOR Encryption" << std::endl;
    std::cout << "Enter choice (1 or 2): ";
    std::cin >> choice;
    
    if (choice == 1) {
        int shift;
        std::cout << "Enter Caesar shift value: ";
        std::cin >> shift;
        
        std::string encrypted = encryptor.EncryptCaesar(user_input, shift);
        std::cout << "Encrypted: " << encrypted << std::endl;
        
        std::string decrypted = encryptor.DecryptCaesar(encrypted, shift);
        std::cout << "Decrypted: " << decrypted << std::endl;
        
    } else if (choice == 2) {
        std::string key;
        std::cout << "Enter XOR key: ";
        std::cin >> key;
        
        std::string encrypted = encryptor.EncryptXor(user_input, key);
        std::cout << "Encrypted (hex): ";
        for (unsigned char byte : encrypted) {
            printf("%02x ", byte);
        }
        std::cout << std::endl;
        
        std::string decrypted = encryptor.DecryptXor(encrypted, key);
        std::cout << "Decrypted: " << decrypted << std::endl;
        
    } else {
        std::cout << "Invalid choice!" << std::endl;
    }
}

int main() {
    std::cout << "Simple Encryption Demonstration Program" << std::endl;
    std::cout << "=======================================" << std::endl;
    
    // Run demonstrations
    DemonstrateCaesarCipher();
    DemonstrateXorEncryption();
    DemonstrateRandomKeyGeneration();
    
    // Interactive mode
    std::cout << "\nPress Enter to continue to interactive mode...";
    std::cin.ignore();
    std::cin.get();
    
    InteractiveMode();
    
    std::cout << "\nThank you for using Simple Encryption!" << std::endl;
    
    return 0;
}