using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
using Compdfkit_Tools.Helper;
using ComPDFKit.DigitalSign;

namespace Compdfkit_Tools.PDFControl
{
    public partial class DigitalSignatureValiditySummaaryControl : UserControl, INotifyPropertyChanged
    {
        private string _validitySummaryString;
        public string ValiditySummaryString
        {
            get => _validitySummaryString;
            set => UpdateProper(ref _validitySummaryString, value);
        }
        public DigitalSignatureValiditySummaaryControl()
        {
            InitializeComponent();
            DataContext = this;
        }

        public void InitWithSignature(CPDFSignature signature)
        {
            bool isSignVerified = signature.SignerList.First().IsSignVerified;
            bool isCertTrusted = signature.SignerList.First().IsCertTrusted;
            bool isDocModified = signature.ModifyInfoList.Count > 0;
            bool isExpired = DateTime.Now >
            CommonHelper.GetDateTimeFromString(signature.SignerList.First().CertificateList.First().ValidityEnds);
            
            string validitySummaryString = "";
            if (isCertTrusted)
            {
                validitySummaryString += "The signer's identity is valid.\n\n";
            }
            else
            {
                validitySummaryString += "The signer's identity is invalid.\n\n";
            }

            if(isDocModified || (!isSignVerified && !isCertTrusted))
            {
                validitySummaryString += "The signature is invalid.\n\n";
            }
            else if (isSignVerified && isCertTrusted)
            {
                validitySummaryString += "The signature is valid.\n\n";
            }
            else if(isSignVerified && !isCertTrusted)
            {
                validitySummaryString += "Signature validity is unknown because it has not been included in your list of trusted certificates and none of its parent certificates are trusted certificates.\n\n";
            }
            
            if(isExpired)
            {
                validitySummaryString += "The file was signed with a certificate that has expired. If you acquired this file recently, it may not be authentic.\n\n";
            }

            if (!isDocModified)
            {
                validitySummaryString += "The document has not been modified since this signature was applied.\n";
            }
            else
            {
                validitySummaryString += "The document has been altered or corrupted since it was signed by the current user.\n";
            }
            ValiditySummaryString = validitySummaryString;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void UpdateProper<T>(ref T properValue,
            T newValue,
            [CallerMemberName] string properName = "")
        {
            if (object.Equals(properValue, newValue))
                return;

            properValue = newValue;
            OnPropertyChanged(properName);
        }

        protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}