using System;
using System.Diagnostics;
using System.Reflection;
using System.IO;

using C4IT.FASD.Licensing;
using C4IT.DataHistoryProvider;

namespace F4SDwebService
{
    static public class cInfo
    {
        static public readonly FileVersionInfo fileVersionInfo;
        static public readonly string FileName;
        static public readonly string minClientVersion = String.Empty;

        static cInfo()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);

            string codeBase = assembly.CodeBase;
            var uri = new Uri(codeBase);
            string path = uri.LocalPath;
            FileName = Path.GetFileName(path);

            var arrMinClientVersion = assembly.GetCustomAttributes(typeof(AssemblyMinClientVersion), false);
            if (arrMinClientVersion != null && arrMinClientVersion.Length >= 1)
            {
                var attrMinClientVersion = (AssemblyMinClientVersion)(arrMinClientVersion[0]);
                minClientVersion = attrMinClientVersion.minClientVersion;
            }
        }

        static public string Title()
        {
            return fileVersionInfo.FileDescription;
        }

        static public string Copyright()
        {
            return fileVersionInfo.LegalCopyright;
        }

        static public string Version()
        {
            return fileVersionInfo.FileVersion;
        }

        static public string ProductVersion()
        {
            return fileVersionInfo.ProductVersion;
        }

        static public string AppTitle()
        {
            return fileVersionInfo.ProductName;
        }

        static public string Connectors
        {
            get
            {
                if (WebApiApplication.Collector == null)
                    return "- no connectors available -";

                var s = "";

                foreach (var _C in WebApiApplication.Collector.GetConnectors())
                {
                    if (s != "")
                        s += ", ";
                    s += _C.Name;
                }
                return s;
            }
        }

        static public string LicenseInfo
        {
            get
            {
                if (cF4SDLicense.Instance?.IsValid == true)
                    return "License is valid, licensed modules:";
                else
                    return "*** invalid license ***";
            }
        }

        static public string LicenseConnectors
        {
            get
            {
                if (cF4SDLicense.Instance == null || !cF4SDLicense.Instance.IsValid)
                    return "";

                var s = "";
                foreach (var Entry in cF4SDLicense.Instance.Modules.Values)
                {
                    if (s != "")
                        s += ", ";
                    s += Entry.Name;
                }
                return s;
            }
        }

        static public string LicenseDetails
        {
            get
            {
                if (cF4SDLicense.Instance == null || cF4SDLicense.Instance.IsIntrinsic)
                    return "";
                var strLic = $"License count: {cF4SDLicense.Instance.LicenseMetrics.Count}";
                if (cF4SDLicense.Instance.EndDate != null)
                {
                    var strEndDate = ((DateTime)cF4SDLicense.Instance.EndDate).Date.ToString("D");
                    strLic += $", end date: {strEndDate}";
                    if (cF4SDLicense.Instance.IsExpired)
                        strLic += "license expired.";
                }
                return strLic;
            }
        }

        static public string LicenseInfoVisibility
        {
            get
            {
                if (cF4SDLicense.Instance == null || cF4SDLicense.Instance.IsIntrinsic)
                    return "hidden";
                else
                    return "";
            }
        }

        static public string MinClientVersion()
        {
            return minClientVersion;
        }

    }
}