namespace PerpetuumSoft.Widgets.Licensing
{
    using System;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Globalization;
    using System.Reflection;

    public class PerpetuumSoftLicense : License
    {
        private NameValueCollection license;
        private string licenseKey = string.Empty;

        public PerpetuumSoftLicense(string licenseKey)
        {
            this.SetLicenseKey(licenseKey);
        }

        public bool CheckOption(string option)
        {
            string str = this[option];
            return ((str != null) && (str.Trim().Length > 0));
        }

        public override void Dispose()
        {
        }

        public bool IsExpire(Type type)
        {
            DateTime epirationDate = this.EpirationDate;
            object customAttribute = Attribute.GetCustomAttribute(type.Assembly, typeof(AssemblyDateAttribute));
            if (customAttribute != null)
            {
                return (((AssemblyDateAttribute) customAttribute).Date > epirationDate);
            }
            return true;
        }

        private void SetLicenseKey(string licenseKey)
        {
            this.license = LicensingUtilities.ParseLicenseKey(licenseKey);
            this.licenseKey = licenseKey;
            this.SetRegKey();
        }

        private void SetRegKey()
        {
        }

        public string BuyURL
        {
            get
            {
                return this["BuyURL"];
            }
        }

        public DateTime EpirationDate
        {
            get
            {
                DateTime minValue = DateTime.MinValue;
                try
                {
                    minValue = DateTime.Parse(this["ExpirationDate"], CultureInfo.InvariantCulture);
                }
                catch
                {
                }
                return minValue;
            }
        }

        public bool IsTrial
        {
            get
            {
                return (this.LicenseType == "TRIAL");
            }
        }

        public string this[string name]
        {
            get
            {
                string str = this.license[name];
                if (str != null)
                {
                    return str;
                }
                return string.Empty;
            }
        }

        public override string LicenseKey
        {
            get
            {
                return this.licenseKey;
            }
        }

        public string LicenseType
        {
            get
            {
                return this["LicenseType"];
            }
        }

        public string ProductName
        {
            get
            {
                return this["Product"];
            }
        }

        public string RegisteredTo
        {
            get
            {
                return this["Name"];
            }
        }

        public string RegKey
        {
            get
            {
                return LicensingUtilities.EncodeLicenseKey(this.RegisteredTo + "\n" + LicensingUtilities.HardwareID, false);
            }
        }
    }
}
