using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Xml;
using static Shafer;

namespace ShaferExample
{
    public static class AppUtils
    {
        public static void ErrorHandler(object sender, UnhandledExceptionEventArgs args)
        {
            Exception e = (Exception)args.ExceptionObject;

            MessageBox.Show(e.Message, "Application exception", MessageBoxButton.OK);
        }

        public static void CheckShaferLibrary()
        {
            if (!File.Exists("ShaferFilechck.dll"))
            {
                throw new Exception("Filechk Missing, please reinstall application");
            }
        }

        public static void CheckPassiveLibrary()
        {
            if (!File.Exists("PassiveFilechck.dll"))
            {
                throw new Exception("Filechk Missing, please reinstall application");
            }
        }

        public static string GetNodeValue(XmlNode node)
        {
            return node.FirstChild != null ? node.FirstChild.Value : string.Empty;
        }

        public static string GetBuildPlatform()
        {
            string result = "";
            if (IntPtr.Size == 8)
            {
                result = "x64";
            }
            else
            {
                result = "x86";
            }
            return result;
        }

        public static string ExpiresIn(double seconds)
        {
            string result = "Expired";
            if (seconds >= 0)
            {
                TimeSpan time = TimeSpan.FromSeconds(seconds);
                result = $"{time.Days} days {time.Hours} hours {time.Minutes} minutes {time.Seconds} seconds";
            }
            return result;
        }

        public static string ExpiresIn(DateTime date)
        {
            TimeSpan time = date - DateTime.Now;
            return $"{time.Days} days {time.Hours} hours {time.Minutes} minutes {time.Seconds} seconds";
        }

        public static long ToUnixTime(DateTime date)
        {
            return (int)(date - new DateTime(1970, 1, 1)).TotalSeconds;
        }

        public static bool LicenseAcquired(LicenseStatus licenseStatus)
        {
            return _licenseAquiredList.Contains(licenseStatus);
        }

        public static string GetEnumDescription(Enum value)
        {
            FieldInfo info = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attr =
                (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attr != null && attr.Length > 0)
            {
                return attr[0].Description;
            }
            else
            {
                return value.ToString();
            }
        }

        public static string ConstructXMLVariableTag(Dictionary<string, string> ValuePair, string TagName)
        {
            // Create the xml document 
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("data");
            doc.AppendChild(root);

            XmlElement Lower = doc.CreateElement(TagName);
            root.AppendChild(Lower);

            foreach (KeyValuePair<String, String> entry in ValuePair)
            {
                XmlElement xe = doc.CreateElement(entry.Key.ToString());
                try
                {
                    xe.InnerText = entry.Value.ToString();
                    Lower.AppendChild(xe);
                }
                catch (Exception ex)
                {
                    return ex.ToString();
                }
            }

            return doc.OuterXml;
        }

        public static UInt32 CalculateSecurityOffSet(UInt32 SecurityRandom, UInt32 X, UInt32 Y, UInt32 Z)
        {
            return X + (SecurityRandom * Y) % Z;
        }

        public static string GetLanguage()
        {
            CultureInfo ci = CultureInfo.InstalledUICulture;
            return DefAppSettings("appLanguage", ci.Name);
        }

        public static string GetVersion()
        {
            Assembly thisAssembly = Application.ResourceAssembly;
            return DefAppSettings("version", thisAssembly.GetName().Version.Major.ToString());
        }

        public static string GetBuild()
        {
            Assembly thisAssembly = Application.ResourceAssembly;
            return DefAppSettings("build", thisAssembly.GetName().Version.Minor.ToString());
        }

        public static string GetEdition()
        {
            return DefAppSettings("edition", "ENT");
        }

        public static string DefAppSettings(string key, string defValue)
        {
            var value = ConfigurationManager.AppSettings[key];
            return string.IsNullOrWhiteSpace(value) ? defValue : value;
        }

        public static byte[] StringToBytes(string value)
        {
            return Encoding.UTF8.GetBytes(value ?? "");
        }

        private static List<LicenseStatus> _licenseAquiredList = new List<LicenseStatus> {LicenseStatus.PROD_AUTHORIZED , LicenseStatus.PROD_CONCURRENT ,
                LicenseStatus.PROD_NETWORK , LicenseStatus.PROD_NETWORK_LTCO ,LicenseStatus.PROD_ABL , LicenseStatus.PROD_ABL_CONCURRENT ,
                LicenseStatus.PROD_LTCO_AUTHORIZED , LicenseStatus.PROD_LTCO_CONCURRENT, LicenseStatus.PROD_LTCO_ACCOUNT, LicenseStatus.PROD_LTCO_ACCOUNT_CONCURRENT };
    }
}
