using CloudBuilder.Core.DependencyInjection.Request;
using CloudBuilder.Core.Extensions;
using CloudBuilder.Core.Policy;
using CloudBuilder.Core.Service;
using CloudBuilder.Gui;
using CloudBuilder.Security.Data;
using CloudBuilder.Security.Entity;
using CloudBuilder.Security.Policy;
using DocumentFormat.OpenXml.Math;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CloudBuilder.Security.Gui
{
    public partial class MainForm
    {
        private Dictionary<string, ToolStripMenuItem> menuItems = new Dictionary<string, ToolStripMenuItem>();

        private void InitMenu()
        {
            this.mainMenu.Items.Clear();

            if (securityMenuData == null || securityMenuData.SecurityFunctionDisplayDatas == null || securityMenuData.SecurityFunctionDisplayDatas.Count() == 0 || securityMenuData.SecurityFunctionMaps == null || securityMenuData.SecurityFunctionMaps.Count() == 0)
            {
                return;
            }

            Dictionary<string, ToolStripMenuItem> menuItems = new Dictionary<string, ToolStripMenuItem>();

            SetMenuItem();
        }



        public void SetMenuItem()
        {
            SecurityUserPermissionInfo permissionInfo = loginContext.GetPermissionInfo();

            string menuDisplayName = null;

            foreach (SecurityFunctionDisplayData functionDisplayData in securityMenuData.SecurityFunctionDisplayDatas.OrderBy(x => x.Orderby))
            {
                SecurityFunctionMapEntity[] maps = securityMenuData.SecurityFunctionMaps.Where(x => x.FunctionNameSon == functionDisplayData.FunctionName).OrderBy(x => x.Orderby).ToArray();

                if (maps == null || maps.Count() == 0) return;

                if (functionDisplayData.FunctionIndc != YesNoPolicy.YES) continue;

                menuDisplayName = CulturePolicy.IsZhcn(loginContext.GetCulture()) ? functionDisplayData.FunctionLocalName : functionDisplayData.FunctionEngName;

                foreach (SecurityFunctionMapEntity map in maps)
                {
                    if (functionDisplayData.FunctionNameParent != map.FunctionNameParent) continue;

                    if (menuDisplayName == "-" || menuDisplayName == "divider")
                    {
                        AddToolStripSeparator(map);
                        continue;
                    }

                    AddMenuItem(functionDisplayData, map.FunctionNameParent);
                }
            }
        }

        private void AddToolStripSeparator(SecurityFunctionMapEntity map)
        {
            ToolStripSeparator sp = new ToolStripSeparator();
            sp.Name = map.FunctionNameParent + map.Orderby + "_" + menuItems[map.FunctionNameParent].DropDownItems.Count;
            menuItems[map.FunctionNameParent].DropDownItems.Add(sp);

        }

        private void AddMenuItem(SecurityFunctionDisplayData function, string functionNameParent)
        {
            //Only Permission whithout function
            if (string.IsNullOrEmpty(function.FunctionName)) return;
            if (string.IsNullOrEmpty(function.PageName) && function.FunctionIndc == YesNoPolicy.YES) return;

            string functionName = function.FunctionName;

            if (menuItems.ContainsKey(functionName))
            {
                functionName = functionName + "_" + functionNameParent;
            }

            ToolStripMenuItem menuItem = new ToolStripMenuItem();
            menuItem.Name = !string.IsNullOrEmpty(function.PageName) ? function.PageName : function.FunctionName;
            menuItem.Size = new System.Drawing.Size(124, 22);
            menuItem.Text = CulturePolicy.IsZhcn(loginContext.GetCulture()) ? function.FunctionLocalName : function.FunctionEngName;

            if (function.FunctionIndc == YesNoPolicy.YES)
                menuItem.Click += new System.EventHandler(MenuItem_Click);

            if (!menuItems.ContainsKey(menuItem.Name))
            {
                menuItems.Add(menuItem.Name, menuItem);
            }
            else
            {
                menuItems.Add(functionName, menuItem);
            }

            CreateParentMenuItem(function.FunctionName, menuItem, functionNameParent);
        }

        private void CreateParentMenuItem(string functionName, ToolStripMenuItem menuItem, string functionNameParent)
        {
            SecurityFunctionMapEntity[] maps = securityMenuData.SecurityFunctionMaps.Where(x => x.FunctionNameSon == functionName).OrderBy(x => x.Orderby).ToArray();

            //如果不存在上級節點返回
            if (maps == null || maps.Count() == 0) return;

            SecurityFunctionMapEntity[] tmaps;
            SecurityFunctionDisplayData functionDisplayData;
            foreach (SecurityFunctionMapEntity map in maps)
            {
                if (!menuItems.ContainsKey(map.FunctionNameParent))
                {
                    functionDisplayData = securityMenuData.SecurityFunctionDisplayDatas.Where(x => x.FunctionName == map.FunctionNameParent).FirstOrDefault()!;

                    if (functionDisplayData == null)
                    {
                        mainMenu.Items.Add(menuItem);
                        return;
                    }

                    tmaps = securityMenuData.SecurityFunctionMaps.OrderBy(x => x.Orderby).Where(x => x.FunctionNameSon == functionDisplayData.FunctionName).ToArray();

                    if (tmaps == null || tmaps.Length == 0)
                    {
                        mainMenu.Items.Add(menuItem);
                        return;
                    }


                    AddMenuItem(functionDisplayData, tmaps[0].FunctionNameParent);
                }

                if (map.FunctionNameParent != functionNameParent) continue;

                menuItems[map.FunctionNameParent].DropDownItems.Add(menuItem);
            }
        }

        private void MenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                DisplayAbstractPage(menuItems[((ToolStripMenuItem)sender).Name].Name, menuItems[((ToolStripMenuItem)sender).Name].Text, null, this.KryptonNavigator);
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

    }
}
