using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SuperCryptEX
{
    public partial class ElGamalForm : Form
    {
        // Parameter - Options (Encrypt/Decrypt)
        private enum Options
        {
            Encrypt = 0,
            Decrypt,
        };

        private Class_ElGamal eg = new Class_ElGamal();
        private Options optEGEnDe = Options.Encrypt;

        public ElGamalForm()
        {
            InitializeComponent();
            text_egtest_output.Enabled = false;
        }

        // ------------------------------------------------------------
        // ElGamal Test Area - Functions
        // ------------------------------------------------------------
        #region ElGamal Test Area - Functions

        private bool eg_checkPublicKey()
        {
            // Cek p, g, y, k
            if (text_egtest_p2.Text != string.Empty &&
                text_egtest_g.Text != string.Empty &&
                text_egtest_y.Text != string.Empty &&
                text_egtest_k.Text != string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool eg_checkPrivateKey()
        {
            // Cek p, x, a
            if (text_egtest_p1.Text != string.Empty &&
                text_egtest_x.Text != string.Empty &&
                text_egtest_a.Text != string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void eg_resetFields()
        {
            text_egtest_p1.Text =
                text_egtest_p2.Text =
                text_egtest_x.Text =
                text_egtest_g.Text =
                text_egtest_y.Text =
                text_egtest_k.Text =
                text_egtest_a.Text =
                text_egtest_input.Text =
                text_egtest_output.Text =
                string.Empty;
        }

        #endregion

        // ------------------------------------------------------------
        // ElGamal Test Area - Components
        // ------------------------------------------------------------
        #region ElGamal Test Area - Components

        private void button_egtest_generate_keypair_Click(object sender, EventArgs e)
        {
            // Inisialisasi Ulang Parameter agar Tidak Terjadi Crash
            eg_resetFields();
            eg.General_ClearVariables();

            // Buat Public Key dan Private Key Secara Acak
            eg.Go_GenerateRandomKey();

            // Tampilkan pada TextBox
            text_egtest_p1.Text = eg.var_p.ToString();
            text_egtest_p2.Text = eg.var_p.ToString();
            text_egtest_x.Text = eg.var_x.ToString();
            text_egtest_g.Text = eg.var_g.ToString();
            text_egtest_y.Text = eg.var_y.ToString();
            text_egtest_k.Text = eg.var_k.ToString();
            text_egtest_a.Text = eg.var_a.ToString();

            // Cek Ulang Semua Parameter
            if (false == eg.General_CheckKeyValidity(Convert.ToInt32(text_egtest_p1.Text), Convert.ToInt32(text_egtest_x.Text), Convert.ToInt32(text_egtest_g.Text), Convert.ToInt32(text_egtest_y.Text), Convert.ToInt32(text_egtest_k.Text), Convert.ToInt32(text_egtest_a.Text)))
            {
                eg_resetFields();
                eg.General_ClearVariables();
                MessageBox.Show("Parameters Not Valid, Re-Check All Parameters!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                //MessageBox.Show("Parameters Valid!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            button_egtest_generate_keypair.Text = "Generate New Keypair";
        }

        private void radio_egtest_encrypt_CheckedChanged(object sender, EventArgs e)
        {
            //eg_resetFields();
            eg.General_ClearVariables();
            optEGEnDe = Options.Encrypt;
        }

        private void radio_egtest_decrypt_CheckedChanged(object sender, EventArgs e)
        {
            //eg_resetFields();
            eg.General_ClearVariables();
            optEGEnDe = Options.Decrypt;
        }

        private void button_egtest_start_Click(object sender, EventArgs e)
        {
            // If Encrypt
            if (optEGEnDe == Options.Encrypt)
            {
                if (eg_checkPublicKey() == false)
                {
                    MessageBox.Show("Please Enter All Public Key Parameters!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (text_egtest_input.Text == string.Empty)
                    {
                        MessageBox.Show("Please Enter Some Text to be Encrypted!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        text_egtest_output.Text = string.Empty;

                        int p = int.Parse(text_egtest_p2.Text);
                        int g = int.Parse(text_egtest_g.Text);
                        int y = int.Parse(text_egtest_y.Text);
                        int k = int.Parse(text_egtest_k.Text);

                        string eg_encryptedText = eg.Go_Encrypt(text_egtest_input.Text, p, g, y, k);
                        Console.WriteLine("Encrypted Text : " + eg_encryptedText);
                        text_egtest_output.Text = eg_encryptedText;
                    }
                }
            }
            // If Decrypt
            else
            {
                if (eg_checkPrivateKey() == false)
                {
                    MessageBox.Show("Please Enter All Private Key Parameters!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (text_egtest_input.Text == string.Empty)
                    {
                        MessageBox.Show("Please Enter Some Text to be Decrypted!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        text_egtest_output.Text = string.Empty;

                        int p = int.Parse(text_egtest_p1.Text);
                        int x = int.Parse(text_egtest_x.Text);
                        int a = int.Parse(text_egtest_a.Text);

                        try
                        {
                            text_egtest_output.Text = eg.Go_Decrypt(text_egtest_input.Text, p, x, a);
                        }
                        catch (Exception exc)
                        {
                            MessageBox.Show(exc.ToString());
                        }

                    }
                }
            }
        }

        private void button_egtest_reset_Click(object sender, EventArgs e)
        {
            eg_resetFields();
        }

        private void button_egtest_copy_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(text_egtest_output.Text);
                MessageBox.Show("Result Copied to Clipboard!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

    }
}
