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

namespace SuperCryptEX
{
    public partial class SHA3Form : Form
    {
        private const BlockSize SHA3BlockSize = BlockSize.Bit_512;

        private Class_General g = new Class_General();
        private string inputPath;

        public SHA3Form()
        {
            InitializeComponent();
        }

        // ------------------------------------------------------------
        // SHA3 Test Area - Functions
        // ------------------------------------------------------------
        #region SHA3 Test Area - Functions

        private HEX SHA3_Hash(FileStream fs)
        {
            return Class_SHA3.SHA3(fs, SHA3BlockSize);
        }

        private void sha_resetFields()
        {
            text_shatest_input.Text =
                text_shatest_hash_result.Text =
                inputPath =
                string.Empty;
        }

        #endregion

        // ------------------------------------------------------------
        // SHA3 Test Area - Components
        // ------------------------------------------------------------
        #region SHA3 Test Area - Components

        private void button_get_hash_Click(object sender, EventArgs e)
        {
            if (g.fileIsExist(inputPath))
            {
                try
                {
                    using (FileStream fs = File.OpenRead(inputPath))
                    {
                        text_shatest_hash_result.Text = SHA3_Hash(fs);
                    }
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("The File Does Not Exist!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void button_shatest_browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileBrowser = new OpenFileDialog();
            fileBrowser.Title = "SuperCryptEX - Browse File";
            fileBrowser.Filter = "All File|*.*";

            if (fileBrowser.ShowDialog() == DialogResult.OK)
            {
                inputPath = fileBrowser.FileName;
                text_shatest_input.Text = inputPath;

                // Cek Jika File > 10240 KB (10 MB)
                FileInfo fileInfo = new FileInfo(inputPath);
                // File Size Default : Bytes
                double fileLength_KB = fileInfo.Length / Math.Pow(1024, 1);
                if (fileLength_KB > 10240)
                {
                    MessageBox.Show("The File That You Choose is Too Large and It will Take a Long Time to Process! (This is Just a Warning)\n" +
                        "File Size : " + String.Format("{0:n}", fileLength_KB) + " KB\n" +
                        "(Expected Time : " + Math.Truncate(fileLength_KB * 0.0004).ToString() + "s)",
                        "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private void button_shatest_reset_Click(object sender, EventArgs e)
        {
            sha_resetFields();
        }

        #endregion

    }
}
