// 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(const SimpleEncryption& encryptor, const std::string& message) {
    std::cout << "\n=== Caesar Cipher Demo ===" << std::endl;
    std::cout << "Original message: " << message << std::endl;
    std::cout << "Shift value: " << encryptor.GetShift() << std::endl;
    
    std::string encrypted = encryptor.EncryptCaesar(message);
    std::cout << "Encrypted: " << encrypted << std::endl;
    
    std::string decrypted = encryptor.DecryptCaesar(encrypted);
    std::cout << "Decrypted: " << decrypted << std::endl;
    
    std::cout << "Match original: " << (message == decrypted ? "YES" : "NO") << std::endl;
}

void DemonstrateXorEncryption(const SimpleEncryption& encryptor, const std::string& message, const std::string& key) {
    std::cout << "\n=== XOR Encryption Demo ===" << std::endl;
    std::cout << "Original message: " << message << std::endl;
    std::cout << "Key: " << key << std::endl;
    
    std::string encrypted = encryptor.EncryptXor(message, key);
    std::cout << "Encrypted (hex): ";
    for (unsigned char ch : encrypted) {
        printf("%02x ", ch);
    }
    std::cout << std::endl;
    
    std::string decrypted = encryptor.DecryptXor(encrypted, key);
    std::cout << "Decrypted: " << decrypted << std::endl;
    
    std::cout << "Match original: " << (message == decrypted ? "YES" : "NO") << std::endl;
}

void DemonstrateVigenereCipher(const SimpleEncryption& encryptor, const std::string& message, const std::string& key) {
    std::cout << "\n=== Vigenere Cipher Demo ===" << std::endl;
    std::cout << "Original message: " << message << std::endl;
    std::cout << "Key: " << key << std::endl;
    
    std::string encrypted = encryptor.EncryptVigenere(message, key);
    std::cout << "Encrypted: " << encrypted << std::endl;
    
    std::string decrypted = encryptor.DecryptVigenere(encrypted, key);
    std::cout << "Decrypted: " << decrypted << std::endl;
    
    std::cout << "Match original: " << (message == decrypted ? "YES" : "NO") << std::endl;
}

void InteractiveMode() {
    SimpleEncryption encryptor;
    std::string message, key;
    int choice, shift;
    
    std::cout << "\n=== Interactive Encryption Mode ===" << std::endl;
    std::cout << "Enter your message: ";
    std::getline(std::cin, message);
    
    std::cout << "\nChoose encryption method:" << std::endl;
    std::cout << "1. Caesar Cipher" << std::endl;
    std::cout << "2. XOR Encryption" << std::endl;
    std::cout << "3. Vigenere Cipher" << std::endl;
    std::cout << "Enter choice (1-3): ";
    std::cin >> choice;
    std::cin.ignore(); // Clear the newline
    
    switch (choice) {
        case 1:
            std::cout << "Enter shift value (0-25): ";
            std::cin >> shift;
            encryptor.SetShift(shift);
            DemonstrateCaesarCipher(encryptor, message);
            break;
            
        case 2:
            std::cout << "Enter encryption key: ";
            std::getline(std::cin, key);
            DemonstrateXorEncryption(encryptor, message, key);
            break;
            
        case 3:
            std::cout << "Enter encryption key: ";
            std::getline(std::cin, key);
            DemonstrateVigenereCipher(encryptor, message, key);
            break;
            
        default:
            std::cout << "Invalid choice!" << std::endl;
            break;
    }
}

int main() {
    std::cout << "Simple Encryption Program" << std::endl;
    std::cout << "=========================" << std::endl;
    
    // Create encryptor with default shift
    SimpleEncryption encryptor(5);
    
    // Test message
    std::string test_message = "Hello, World! This is a secret message.";
    std::string xor_key = "MySecretKey";
    std::string vigenere_key = "CIPHER";
    
    // Demonstrate all encryption methods
    DemonstrateCaesarCipher(encryptor, test_message);
    DemonstrateXorEncryption(encryptor, test_message, xor_key);
    DemonstrateVigenereCipher(encryptor, test_message, vigenere_key);
    
    // Show random key generation
    std::cout << "\n=== Random Key Generation ===" << std::endl;
    std::string random_key = encryptor.GenerateRandomKey(16);
    std::cout << "Generated 16-character random key: " << random_key << std::endl;
    
    // Interactive mode
    char continue_choice;
    std::cout << "\nWould you like to try interactive mode? (y/n): ";
    std::cin >> continue_choice;
    std::cin.ignore(); // Clear the newline
    
    if (continue_choice == 'y' || continue_choice == 'Y') {
        InteractiveMode();
    }
    
    std::cout << "\nThank you for using Simple Encryption!" << std::endl;
    return 0;
}