using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

namespace FHT
{
    public partial class FormConfiguration : Form
    {
        public string Tftp_IP { get; set; }
        public string Sftp_IP { get; set; }
        public string Sftp_Username { get; set; }
        public string Sftp_Password { get; set; }
        public bool bEncryption { get; set; }
        public string sTasklistPath { get; set; }
        public string sProfilePath { get; set; }

        public Aes aes { get; set; }

        public static ILog log;

        public FormConfiguration(string tftp_IP, string sftpIP, string sftp_Username, string sftp_Password, bool bencryption
            , Aes aaes, string sprofilepath, ILog Log)
        {
            InitializeComponent();
            this.textBoxTftpIP.Text = Tftp_IP = tftp_IP;
            this.textBoxSftpIP.Text = Sftp_IP = sftpIP;
            this.textBoxSftpUsername.Text = Sftp_Username = sftp_Username;
            this.textBoxSftpPassword.Text = Sftp_Password = sftp_Password;
            buttonProfilePath.Text = sProfilePath = sprofilepath;
            aes = aaes;
            log = Log;
            
            this.Update();
            
        }

        public void AdminDisable()
        {
            buttonTasklistEnc.Enabled = false;
            buttonTasklistDec.Enabled = false;
            
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            Tftp_IP = textBoxTftpIP.Text;
            Sftp_IP = textBoxSftpIP.Text;
            Sftp_Username = textBoxSftpUsername.Text;
            Sftp_Password = textBoxSftpPassword.Text;
            this.Close();
        }

        private void FormConfiguration_Load(object sender, EventArgs e)
        {

        }


        private void buttonTasklistEnc_Click(object sender, EventArgs e)
        {
            Utilities utilities = new Utilities(log);
            //Enable selecting multiple files
            openFileDialog.Multiselect = true; 


            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] Jsonfilenames = openFileDialog.FileNames;
                foreach (string Jsonfilename in Jsonfilenames)
                {
                    if (File.Exists(Jsonfilename))
                    {
                        try
                        {
                            //read tasklist data from file
                            string sData = File.ReadAllText(Jsonfilename);
                            log.Info(string.Format("Read {0} to String", Jsonfilename));
                            //encrypt data
                            byte[] bData = Utilities.EncryptStringAES(sData, aes.Key);

                            //write encrypted data to file
                            File.WriteAllBytes(Jsonfilename, bData);

                        }
                        catch (Exception ex)
                        {
                            //could not read tasklist.json properly
                            log.Error(string.Format("Could not load {0} for encrypting", Jsonfilename));
                            log.Error(ex);
                        }
                    }
                }
                
            }
            //disable multiselect
            openFileDialog.Multiselect = false;
        }

        private void buttonTasklistDec_Click(object sender, EventArgs e)
        {
            Utilities utilities = new Utilities(log);
            //Enable selecting multiple files
            openFileDialog.Multiselect = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string[] Jsonfilenames = openFileDialog.FileNames;
                foreach (string JsonFileName in Jsonfilenames)
                {
                    if (File.Exists(JsonFileName))
                    {
                        try
                        {
                            //read json data from file
                            byte[] bData = File.ReadAllBytes(JsonFileName);

                            //decrypt data and save to file

                            File.WriteAllText(JsonFileName, Utilities.DecryptStringFromBytes(bData, aes.Key));

                        }
                        catch (Exception ex)
                        {
                            //could not read tasklist.json properly
                            log.Error(string.Format("Could not load {0} for encrypting", JsonFileName));
                            log.Error(ex);
                        }


                    }
                }
            }
        }

        private void buttonProfilePath_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                sProfilePath = openFileDialog.FileName;
                buttonProfilePath.Text = openFileDialog.SafeFileName;
            }
        }
    }
}
