using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace SuperCryptEX
{
    public enum BlockSize
    {
        Bit_128 = 0,    // AES
        Bit_192,        // AES
        Bit_224,        // SHA3
        Bit_256,        // AES & SHA3
        Bit_384,        // SHA3
        Bit_512         // SHA3
    };

    public class Class_General
    {

        // Set Ke False Jika Ingin Mematikan Console
        public bool enableDebug = true;
        public bool enableDebugFile = false;

        // Constructor
        public Class_General() { }

        // Function - Print Byte Array Value (Dalam Hex)
        public string printByteHexValue(byte[] byteInput)
        {
             string data = "";
            for (var i = 0; i < byteInput.Length; ++i)
                data += String.Format("{0:x2} ", byteInput[i]);
            return data;
        }

        // Function - Print Single Byte Value (Dalam Hex)
        public string printSingleByteHexValue(byte byteInput)
        {
            return String.Format("{0:x2}", byteInput);
        }

        // Function - Print Byte value
        public string printByteBaseValue(byte[] byteInput)
        {
            return string.Join(", ", byteInput);
        }

        // Function - Konversi Byte ke String
        public string byteToString(byte[] byteInput)
        {
            string stringOutput = Encoding.ASCII.GetString(byteInput);
            //printDebug("String Size \t: " + stringOutput.Length);
            return stringOutput;
        }

        // Function - Konversi String ke Byte
        public byte[] stringToByte(string strInput)
        {
            byte[] byteOutput = new byte[strInput.Length];
            byteOutput = Encoding.ASCII.GetBytes(strInput);
            //printDebug("Byte Size \t: " + byteOutput.Length);
            return byteOutput;
        }

        // Function - Cek Jika File Ada atau Tidak
        public bool fileIsExist(string inputPath)
        {
            if (File.Exists(inputPath))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        // Function - Rename File jika Nama File Sudah Ada
        public string renameIfNameAlreadyExists(string fileName)
        {
            string strFileName = fileName;
            string strExtensionName = "";
            int dotIndex = fileName.IndexOf(".");
            if (dotIndex > 0)
            {
                strFileName = fileName.Substring(0, dotIndex);
                strExtensionName = fileName.Substring(dotIndex);
            }
            strFileName += "_mx";
            strFileName += strExtensionName;
            return strFileName;
        }

        // Function - Tampilkan Debug Info
        public string printDebug(string debugMessage)
        {
            if (enableDebug)
            {
                debugMessage = "[Debug] " + debugMessage;
                Console.WriteLine(debugMessage);
                return debugMessage;
            }

            return string.Empty;
        }

        // Function - Membuat Debug File untuk Analisa Program Secara Manual
        public void createDebugFile(string inputFile, byte[] byteToWrite, string debugExtension)
        {
            if (enableDebugFile)
            {
                // Masukkan ke Sebuah File
                FileStream dummyStream = new FileStream(inputFile + "." + debugExtension, FileMode.Create);
                dummyStream.Write(byteToWrite);
                dummyStream.Close();
            }
        }

        // (Unused) Function - Get String Before a Specified Symbol
        public string GetStringBeforeSymbol(string text, string stopAt = "|")
        {
            if (!string.IsNullOrWhiteSpace(text))
            {
                int charLocation = text.IndexOf(stopAt, StringComparison.Ordinal);

                if (charLocation > 0)
                {
                    return text.Substring(0, charLocation);
                }
            }

            return String.Empty;
        }

    }
}
