﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Weredev.Yeren
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();

            calCertEffDate.DisplayDateStart = DateTime.Today;
            calCertEffDate.DisplayDate = DateTime.Today;
            calCertEffDate.SelectedDate = DateTime.Today;
            calCertExpDate.DisplayDateStart = DateTime.Today;
            calCertExpDate.DisplayDate = DateTime.Today.AddYears(2);
            calCertExpDate.SelectedDate = DateTime.Today.AddYears(2);
        }

        private void btnCertCreateCer_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CheckCertSubject();
                CheckCertDates();

                var cert = CertCrypto.CertHandler.CreateSelfSignedCert(txtCertSubject.Text, calCertEffDate.SelectedDate, calCertExpDate.SelectedDate);
                String certContent = CertCrypto.CertHandler.WriteX509Certificate(cert);

                if (AppHelper.SaveFile(AppHelper.FILE_CER, certContent, this))
                {
                    AppHelper.ShowInfoMessage("File saved successfully.", this);
                }
            }
            catch (Exception ex)
            {
                AppHelper.ShowErrorMessage(ex.Message, this);
            }
        }

        private void txtCertSubject_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txtCertSubject.Text))
            {
                txtCertSubject.BorderBrush = AppHelper.Border_Error;
                txtCertSubject.Background = AppHelper.Background_Error;
            }
            else
            {
                txtCertSubject.BorderBrush = AppHelper.Border_Ok;
                txtCertSubject.Background = AppHelper.Background_Ok;
            }
        }

        private void CheckCertSubject()
        {
            String subject = txtCertSubject.Text;
            txtCertSubject_TextChanged(null, null);
            if (String.IsNullOrWhiteSpace(subject))
            {
                throw new Exception("Please enter Subject / Issuer.");
            }
        }

        private void CheckCertDates()
        {
            try
            {
                CheckCertDates(calCertEffDate);
                CheckCertDates(calCertExpDate);

                DateTime? effDate = calCertEffDate.SelectedDate;
                DateTime? expDate = calCertExpDate.SelectedDate;

                if (effDate.Value > expDate.Value)
                {
                    calCertEffDate.BorderBrush = AppHelper.Border_Error;
                    calCertEffDate.Background = AppHelper.Background_Error;
                    calCertExpDate.BorderBrush = AppHelper.Border_Error;
                    calCertExpDate.Background = AppHelper.Background_Error;
                    throw new Exception("Effective Date can not be after Expiration Date.");
                }
            }
            catch (Exception ex)
            {
                AppHelper.ShowErrorMessage(ex.Message, this);
            }
        }

        private void CheckCertDates(DatePicker datePicker)
        {
            DateTime? selectedDate = datePicker.SelectedDate;
            if (selectedDate.HasValue)
            {
                datePicker.BorderBrush = AppHelper.Border_Ok;
                datePicker.Background = AppHelper.Background_Ok;
            }
            else
            {
                String datePickerName = datePicker.Name;
                String lblName = "lbl" + datePickerName.Substring(3);
                Label label = tabCertificates.FindName(lblName) as Label;

                datePicker.BorderBrush = AppHelper.Border_Error;
                datePicker.Background = AppHelper.Background_Error;

                if (label == null)
                {
                    throw new Exception("Please set value for " + datePickerName);
                }
                else
                {
                    throw new Exception("Please set value for " + label.Content);
                }
            }

        }

        

        private void btnCertCreatePfx_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CheckCertSubject();
                CheckCertDates();
                String password = AppHelper.AskForPassword(this);
                if (password == null)
                    return;

                var cert = CertCrypto.CertHandler.CreateSelfSignedCert(txtCertSubject.Text, calCertEffDate.SelectedDate, calCertExpDate.SelectedDate);
                Byte[] certContent = CertCrypto.CertHandler.WritePkcs12Certificate(cert, password);

                if (AppHelper.SaveFile(AppHelper.FILE_PFX, certContent, this))
                {
                    AppHelper.ShowInfoMessage("File saved successfully.", this);
                }

            }
            catch (Exception ex)
            {
                AppHelper.ShowErrorMessage(ex.Message, this);
            }
        }



        private void btnCertCreatePfxFromCer_Click(object sender, RoutedEventArgs e)
        {
            CreatePfxFromCer dialog = new CreatePfxFromCer();
            dialog.Owner = this;
            if (dialog.ShowDialog() == true)
            {
                AppHelper.ShowInfoMessage("File saved successfully.", this);
            }
        }

        private void btnCfgCert_Click(object sender, RoutedEventArgs e)
        {
            String filename = AppHelper.BrowseForFile(AppHelper.FILE_PFX, this);
            var collection = CertCrypto.CertHandler.LoadCertificate2(filename, "test1234");
        }
    }
}
