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.Plugin.MonitionList.App_Start
{
    public class FunctionConfig
    {
        /// <summary>
        /// Registers all plugin functions provided by the plugin system
        /// </summary>
        public static void RegisterFunctions()
        {
            var userService = Singleton<IContainer>.Instance.Resolve<IUserService>();

            var pluginFunctions = AllPluginFunctions();

            foreach (var function in pluginFunctions)
            {
                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[] AllPluginFunctions()
        {
            return new[]
            {
                new Function
                {
                    Name = "MonotionList",
                    Description = "Mahnlisten",
                    ImageUrl = "~/Plugins/Misc.MonotionList/Content/Images/function-MonotionList-32.png",
                    IsMenuMember = true,
                    Plugin = "GreenTree.Nachtragsmanagement.Plugin.MonitionList"
                },
                new Function
                {
                    Name = "MonotionList-Deviations",
                    Description = "VA-Mahnliste",
                    ImageUrl = "~/Plugins/Misc.MonotionList/Content/Images/function-MonitionList-Deviations-32.png",
                    GroupName = "MonotionList",
                    RouteName = "GreenTree.Nachtragsmanagement.Plugin.MonitionList.Deviations",
                    IsMenuMember = true,
                    Plugin = "GreenTree.Nachtragsmanagement.Plugin.MonitionList",
                    BaseWidth = 900,
                    MinWidth = 600,
                    BaseHeight = 600,
                    MinHeight = 400,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                },
                new Function
                {
                    Name = "MonotionList-Appendices",
                    Description = "NT-Mahnliste",
                    ImageUrl = "~/Plugins/Misc.MonotionList/Content/Images/function-MonitionList-Appendices-32.png",
                    GroupName = "MonotionList",
                    RouteName = "GreenTree.Nachtragsmanagement.Plugin.MonitionList.Appendices",
                    IsMenuMember = true,
                    Plugin = "GreenTree.Nachtragsmanagement.Plugin.MonitionList",
                    BaseWidth = 900,
                    MinWidth = 600,
                    BaseHeight = 600,
                    MinHeight = 400,
                    AllowMaximize = true,
                    MaximizedOnStart = true
                }
            };
        }
    }
}