//  
//   Rebex Sample Code License
// 
//   Copyright 2026, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Rebex.Security;

namespace Rebex.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            // set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key;


            try
            {
                // parse arguments
                Arguments arguments = new Arguments(args);

                // initialize FileEncryption object
                FileEncryption encryption = new FileEncryption();
                encryption.SetPassword(arguments.Password);
                encryption.OverwriteExistingFile = arguments.Overwrite;

                // perform specified operation
                if (arguments.Operation == Operation.Encrypt)
                {
                    encryption.Encrypt(arguments.SourceFile, arguments.DestinationFile);
                    Console.WriteLine("File successfully encrypted.");
                }
                else
                {
                    encryption.Decrypt(arguments.SourceFile, arguments.DestinationFile);
                    Console.WriteLine("File successfully decrypted.");
                }
            }
            catch (SampleException e)
            {
                // display an error reported by arguments parser class
                ShowHelp();
                Console.WriteLine();
                Console.WriteLine("Error: {0}", e.Message);
            }
            catch (IOException ex)
            {
                // display IO error
                Console.WriteLine(ex.Message);
            }
            catch (InvalidOperationException ex)
            {
                // display invalid operation error
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                // display all other errors
                Console.WriteLine("Error: {0}", ex);
            }
        }

        static void ShowHelp()
        {
            string encrypt = Operation.Encrypt.ToString().ToLower();
            string decrypt = Operation.Decrypt.ToString().ToLower();
            string applicationName = AppDomain.CurrentDomain.FriendlyName;

            Console.WriteLine("=====================================================================");
            Console.WriteLine(" Rebex {0} - Encrypts or decrypts files", applicationName);
            Console.WriteLine("=====================================================================");
            Console.WriteLine();
            Console.WriteLine("The program is a sample for Rebex Security for .NET component.");
            Console.WriteLine("For more info, see http://www.rebex.net/security/");
            Console.WriteLine();
            Console.WriteLine(@"Syntax: {0} {1}|{2} <source file> <destination file> password [overwrite]", applicationName, encrypt, decrypt);
            Console.WriteLine(@"Example: {0} {1} C:\file.txt C:\file.enc secret-password overwrite", applicationName, encrypt);
            Console.WriteLine(@"Example: {0} {1} C:\file.enc C:\file.txt secret-password", applicationName, decrypt);
        }
    }
}