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

namespace Volpe.Cafe.Utils
{
    /// <summary>
    /// Provides basic information about the current version of the CAFE Model.
    /// </summary>
    [Serializable]
    public static class ApplicationInfo
    {
        static ApplicationInfo()
        {
            var exeAssembly  = Assembly.GetEntryAssembly();
            var versionInfo  = FileVersionInfo.GetVersionInfo(exeAssembly.Location);
            ProductName      = versionInfo.ProductName;
            ProductVersion   = versionInfo.ProductVersion;
            ProductBuildVersion = versionInfo.FileVersion;
            ProductBuildDate = (new FileInfo(exeAssembly.Location)).LastWriteTime.ToString("M/dd/yy H:mm:ss");
            ApplicationPath  = Path.GetDirectoryName(exeAssembly.Location);

            // **A note on setting "CAFELibBuildDate" below.
            // (Note: This whole approach may be changed, expanded, or removed.)
            // When the application is packaged and released using embedded assemblies, trying to
            // obtain the build date from a file's write-time attribute throws an exception, since
            // a physical file path does not actually exist on disk. So instead, we need to check
            // if a file exists (i.e., the actual DLL is in the same place with the executable,
            // and not just embedded), and if it doesn't, default to using the "ProductBuildDate".
            var libAssembly  = typeof(ApplicationInfo).Assembly;
            CAFELibVersion   = libAssembly.GetName().Version.ToString(4);
            CAFELibBuildDate = File.Exists(libAssembly.Location)
                             ? (new FileInfo(libAssembly.Location)).LastWriteTime.ToString("M/dd/yy H:mm:ss")
                             : ProductBuildDate;

            UserResourcesPath  = Path.Combine(
                                    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                                    "Volpe Center",
                                    "CAFE Model");
            LogPath            = Path.Combine(UserResourcesPath, "logs");
            DefaultOutputPath  = Path.Combine(UserResourcesPath, "output");
            SettingsFilePath   = Path.Combine(UserResourcesPath, "settings.bin");
            RecentListFilePath = Path.Combine(UserResourcesPath, "recent.list");

            Directory.CreateDirectory(LogPath);
            Directory.CreateDirectory(DefaultOutputPath);
        }

        /// <summary>
        /// Specifies the path for the executable file that started the application, not including the executable name.
        /// </summary>
        public static readonly string ApplicationPath;

        /// <summary>Specifies the product name of the application.</summary>
        public static readonly string ProductName;
        /// <summary>Specifies the product version of the application.</summary>
        public static readonly string ProductVersion;
        /// <summary>Specifies the build version of the executable of the application.</summary>
        public static readonly string ProductBuildVersion;
        /// <summary>Specifies the date and time the executable for the application was built.</summary>
        public static readonly string ProductBuildDate;

        /// <summary>Specifies the version of the Volpe.Cafe.dll assembly (the CAFE Model library).</summary>
        public static readonly string CAFELibVersion;
        /// <summary>
        /// Specifies the date and time the Volpe.Cafe.dll assembly (the CAFE Model library) was built.
        /// </summary>
        public static readonly string CAFELibBuildDate;

        /// <summary>Specifies the path where additional user-specific application resources are located.</summary>
        public static readonly string UserResourcesPath;
        /// <summary>Specifies the path where the model log files are stored.</summary>
        public static readonly string LogPath;
        /// <summary>Specifies the default path where the output files are stored.</summary>
        public static readonly string DefaultOutputPath;
        /// <summary>Specifies the location of the settings.bin file.</summary>
        public static readonly string SettingsFilePath;
        /// <summary>Specifies the location of the recent.list file.</summary>
        public static readonly string RecentListFilePath;

    }
}
