using Autofac;
using GreenTree.Nachtragsmanagement.Core;
using GreenTree.Nachtragsmanagement.Core.Domain.User;
using GreenTree.Nachtragsmanagement.Services.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GreenTree.Nachtragsmanagement.Web.App_Start
{
    public class FunctionConfig
    {
        /// <summary>
        /// Registers all system functions provided by the base system
        /// </summary>
        public static void RegisterFunctions()
        {
            var userService = Singleton<IContainer>.Instance.Resolve<IUserService>();

            var systemFunctions = AllSystemFunctions();

            foreach (var function in systemFunctions)
            {
                var existingFunction = userService.GetFunctionByName(function.Name);

                if (existingFunction != null)
                {
                    var equals =
                        existingFunction.Description == function.Description &&
                        existingFunction.ImageUrl == function.ImageUrl &&
                        existingFunction.GroupName == function.GroupName &&
                        existingFunction.RouteName == function.RouteName &&
                        existingFunction.IsMenuMember == function.IsMenuMember &&
                        existingFunction.Plugin == function.Plugin &&
                        existingFunction.BaseWidth == function.BaseWidth &&
                        existingFunction.MinWidth == function.MinWidth &&
                        existingFunction.BaseHeight == function.BaseHeight &&
                        existingFunction.MinHeight == function.MinHeight &&
                        existingFunction.AllowMaximize == function.AllowMaximize;

                    if (!equals)
                    {
                        existingFunction.Description = function.Description;
                        existingFunction.ImageUrl = function.ImageUrl;
                        existingFunction.GroupName = function.GroupName;
                        existingFunction.RouteName = function.RouteName;
                        existingFunction.IsMenuMember = function.IsMenuMember;
                        existingFunction.Plugin = function.Plugin;
                        existingFunction.BaseWidth = function.BaseWidth;
                        existingFunction.MinWidth = function.MinWidth;
                        existingFunction.BaseHeight = function.BaseHeight;
                        existingFunction.MinHeight = function.MinHeight;
                        existingFunction.AllowMaximize = function.AllowMaximize;

                        userService.UpdateFunction(existingFunction);
                    }
                }
                else
                    userService.InsertFunction(function);
            }
        }

        private static Function[] AllSystemFunctions()
        {
            return new[]
            {
                new Function
                {
                    Name = "Administration",
                    Description = "Administration",
                    ImageUrl = "~/Content/Images/function-Administration-32.png",
                    IsMenuMember = true,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Administration-Users",
                    Description = "Benutzerverwaltung",
                    ImageUrl = "~/Content/Images/function-Administration-Users-32.png",
                    GroupName = "Administration",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Administration.Users",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 900,
                    MinWidth = 600,
                    BaseHeight = 600,
                    MinHeight = 400,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Administration-Users-Edit",
                    Description = "Benutzer editieren",
                    GroupName = "Administration-Users",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Administration-Roles",
                    Description = "Rollenverwaltung",
                    ImageUrl = "~/Content/Images/function-Administration-Roles-32.png",
                    GroupName = "Administration",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Administration.Roles",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 800,
                    MinWidth = 600,
                    BaseHeight = 500,
                    MinHeight = 400,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Administration-Roles-Edit",
                    Description = "Rollen editieren",
                    GroupName = "Administration-Roles",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Administration-Plugins",
                    Description = "Pluginverwaltung",
                    ImageUrl = "~/Content/Images/function-Administration-Plugins-32.png",
                    GroupName = "Administration",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Administration.Plugins",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 800,
                    MinWidth = 500,
                    BaseHeight = 450,
                    MinHeight = 300,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Administration-Plugins-Edit",
                    Description = "Plugins installieren / deinstallieren",
                    GroupName = "Administration-Plugins",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Administration-AppInfo",
                    Description = "Anwendungsinformationen",
                    ImageUrl = "~/Content/Images/function-Administration-AppInfo-32.png",
                    GroupName = "Administration",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Administration.AppInfo",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 1000,
                    MinWidth = 700,
                    BaseHeight = 600,
                    MinHeight = 400,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Administration-AppInfo-Update",
                    Description = "Updates installieren",
                    GroupName = "Administration-AppInfo",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Site",
                    Description = "Baustellen",
                    ImageUrl = "~/Content/Images/function-Site-32.png",
                    IsMenuMember = true,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Site-Sites",
                    Description = "Baustellenliste",
                    ImageUrl = "~/Content/Images/function-Site-Sites-32.png",
                    GroupName = "Site",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Site.Sites",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 1100,
                    MinWidth = 800,
                    BaseHeight = 650,
                    MinHeight = 500,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Site-Sites-Edit",
                    Description = "Baustellen editieren",
                    GroupName = "Site-Sites",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Site-Sites-Edit-Comment",
                    Description = "Baustellen Kommentar editieren",
                    GroupName = "Site-Sites",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Site-Sites-Delete",
                    Description = "Baustellen löschen",
                    GroupName = "Site-Sites",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Deviation",
                    Description = "Vertragsabweichungen",
                    ImageUrl = "~/Content/Images/function-Deviation-32.png",
                    IsMenuMember = true,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Deviation-Deviations",
                    Description = "Vertragsabweichungsliste",
                    ImageUrl = "~/Content/Images/function-Deviation-Deviations-32.png",
                    GroupName = "Deviation",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Deviation.Deviations",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 1000,
                    MinWidth = 800,
                    BaseHeight = 650,
                    MinHeight = 500,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Deviation-Deviations-Edit",
                    Description = "Vertragsabweichungen editieren",
                    GroupName = "Deviation-Deviations",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Deviation-Deviations-Edit-Comment",
                    Description = "Vertragsabweichungen Kommentar editieren",
                    GroupName = "Deviation-Deviations",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Deviation-Claims",
                    Description = "VA-Stammdaten",
                    ImageUrl = "~/Content/Images/function-Deviation-Claims-32.png",
                    GroupName = "Deviation",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Deviation.Claims",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 1000,
                    MinWidth = 600,
                    BaseHeight = 500,
                    MinHeight = 350,
                    AllowMaximize = true,
                    MaximizedOnStart = false
                },
                new Function
                {
                    Name = "Deviation-Claims-Edit",
                    Description = "VA-Stammdaten editieren",
                    GroupName = "Deviation-Claims",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Appendix",
                    Description = "Nachträge",
                    ImageUrl = "~/Content/Images/function-Appendix-32.png",
                    IsMenuMember = true,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Appendix-Appendices",
                    Description = "Nachtragsliste",
                    ImageUrl = "~/Content/Images/function-Appendix-Appendices-32.png",
                    GroupName = "Appendix",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Appendix.Appendices",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 1100,
                    MinWidth = 800,
                    BaseHeight = 650,
                    MinHeight = 500,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Appendix-Appendices-Edit",
                    Description = "Nachträge editieren",
                    GroupName = "Appendix-Appendices",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Appendix-Appendices-Edit-Comment",
                    Description = "Nachträge Kommentar editieren",
                    GroupName = "Appendix-Appendices",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Appendix-Claims",
                    Description = "NT-Stammdaten",
                    ImageUrl = "~/Content/Images/function-Appendix-Claims-32.png",
                    GroupName = "Appendix",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Appendix.Claims",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 800,
                    MinWidth = 600,
                    BaseHeight = 500,
                    MinHeight = 350,
                    AllowMaximize = true,
                    MaximizedOnStart = false
                },
                new Function
                {
                    Name = "Appendix-Claims-Edit",
                    Description = "NT-Stammdaten editieren",
                    GroupName = "Appendix-Claims",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Misc",
                    Description = "Sonstiges",
                    ImageUrl = "~/Content/Images/function-Misc-32.png",
                    IsMenuMember = true,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Misc-MailNotifications",
                    Description = "Benachrichtigungen",
                    ImageUrl = "~/Content/Images/function-Misc-MailNotifications-32.png",
                    GroupName = "Misc",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Misc.MailNotifications",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 900,
                    MinWidth = 700,
                    BaseHeight = 550,
                    MinHeight = 450,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Misc-MailNotifications-Edit",
                    Description = "Benachrichtigungen editieren",
                    GroupName = "Misc-MailNotifications",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Misc-HelpPages",
                    Description = "Hilfe",
                    ImageUrl = "~/Content/Images/function-Misc-HelpPages-32.png",
                    GroupName = "Misc",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Misc.HelpPages",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 1000,
                    MinWidth = 700,
                    BaseHeight = 600,
                    MinHeight = 450,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Misc-HelpPages-Edit",
                    Description = "Hilfe editieren",
                    GroupName = "Misc-HelpPages",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Misc-Logs",
                    Description = "Logs",
                    ImageUrl = "~/Content/Images/function-Misc-Logs-32.png",
                    GroupName = "Misc",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Misc.Logs",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 900,
                    MinWidth = 700,
                    BaseHeight = 550,
                    MinHeight = 450,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Misc-Logs-Delete",
                    Description = "Logs löschen",
                    GroupName = "Misc-Logs",
                    IsMenuMember = false,
                    Plugin = "System"
                },
                new Function
                {
                    Name = "Misc-ConfigItems",
                    Description = "Einstellungen",
                    ImageUrl = "~/Content/Images/function-Misc-ConfigItems-32.png",
                    GroupName = "Misc",
                    RouteName = "GreenTree.Nachtragsmanagement.Web.Misc.ConfigItems",
                    IsMenuMember = true,
                    Plugin = "System",
                    BaseWidth = 900,
                    MinWidth = 700,
                    BaseHeight = 550,
                    MinHeight = 450,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "Misc-ConfigItems-Edit",
                    Description = "Einstellungen ändern",
                    GroupName = "Misc-ConfigItems",
                    IsMenuMember = false,
                    Plugin = "System"
                }
            };
        }
    }
}