using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

namespace GreenTree.Nachtragsmanagement.Web.Models.Admin.AppInfo
{
    public class AppInfoDataModel
    {
        public AssemblyDataModel[] Assemblies { get; set; }
        public string BaseDirectory { get; set; }
        public string AspNetVersion { get; set; }
        public string OperatingSystem { get; set; }
        public bool Is64BitOs { get; set; }
        public bool IsUpdate { get; set; }

        /// <summary>
        /// Gets information about every used assembly in local AppDomain
        /// </summary>
        public void GetAssemblies()
        {
            var assemblies = AppDomain.CurrentDomain.GetAssemblies()
                .Select(a => new AssemblyDataModel
                {
                    Name = a.GetName().Name,
                    Version = a.GetName().Version.ToString(),
                    Manufacturer =
                        a.GetCustomAttributes<AssemblyCompanyAttribute>().Any()
                            ? a.GetCustomAttributes<AssemblyCompanyAttribute>()
                                .FirstOrDefault().Company
                            : "Unbekannt"
                })
                .OrderBy(a => a.Manufacturer)
                .ThenBy(a => a.Name)
                .ToArray();

            Assemblies = assemblies;
        }
    }
}