using CloudBuilder.Core.DependencyInjection.Request;
using CloudBuilder.Core.Service;
using CloudBuilder.Gui;
using CloudBuilder.Gui.DependencyInjection;
using CloudBuilder.Request.Security;
using CloudBuilder.Security.Data;
using CloudBuilder.Security.Policy;
using Krypton.Navigator;
using Microsoft.Extensions.DependencyInjection;
using CloudBuilder.Core.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CloudBuilder.Security.Gui
{
    public partial class MainForm : AbstractForm
    {
        private IContext loginContext = null;
        private SecurityFunctionResultData securityMenuData = null;

        public MainForm()
        {
            InitializeComponent();
        }

        public void InitForm()
        {
            try
            {
                //初始化容器-------------------------------------
                loginContext = ServiceHelper.GetInterfaceService<IContext>()!;
                //设置登录信息
                securityUserNameLabel.Text = string.Format("[{0}] [{1}] {2}", loginContext.DefaultVersion, loginContext.Requestor, loginContext.LoginDate.GetFormattedDateString("yyyy-MM-dd HH:mm:ss"));
                copyLabel.Text = loginContext.DefaultCopyright;
                this.Text = loginContext.DefaultTitle;
                //不启用这两个功能
                navigatorLeft.Visible = false;
                toolStrip1.Visible = false;

                navigatorLeft.NavigatorMode = NavigatorMode.HeaderBarCheckButtonOnly;
                buttonLeft.TypeRestricted = PaletteNavButtonSpecStyle.ArrowRight;
                this.KryptonNavigator = mainKryptonNavigator;
                //设置居中对齐
                this.SetBounds((Screen.GetBounds(this).Width / 2) - (this.Width / 2),
    (Screen.GetBounds(this).Height / 2) - (this.Height / 2),
    this.Width, this.Height, BoundsSpecified.Location);
                //设置最大化
                this.WindowState = FormWindowState.Maximized;
                //清空菜单项
                this.mainMenu.Items.Clear();
                //初始化菜单数据-------------------------------------
                InitData();
                //设置菜单
                InitMenu();
                //设置欢迎页

                DisplayAbstractPage(typeof(WelcomePage).Name);
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void InitData()
        {
            SecurityUserRequest securityUserRequest = ServiceHelper.GetTypeService<SecurityUserRequest>();

            securityMenuData = securityUserRequest.FindMenuByUsername();
        }

        private void buttonLeft_Click(object sender, EventArgs e)
        {

            if (navigatorLeft.NavigatorMode == NavigatorMode.HeaderBarCheckButtonGroup)
            {
                navigatorLeft.NavigatorMode = NavigatorMode.HeaderBarCheckButtonOnly;
                buttonLeft.TypeRestricted = PaletteNavButtonSpecStyle.ArrowRight;
            }
            else
            {
                navigatorLeft.NavigatorMode = NavigatorMode.HeaderBarCheckButtonGroup;
                buttonLeft.TypeRestricted = PaletteNavButtonSpecStyle.ArrowLeft;
            }
        }


        private void mainKryptonNavigator_Deselecting(object sender, KryptonPageCancelEventArgs e)
        {
            if (mainKryptonNavigator.Pages.Count == 0) return;

            if (mainKryptonNavigator.SelectedPage == null) return;

            AbstractPage page = GetAbstractPage(mainKryptonNavigator.SelectedPage);

            if (page == null) return;

            if (!page.FunctionDeactivating()) e.Cancel = true;
        }

        public AbstractPage GetAbstractPage(KryptonPage page)
        {
            foreach (Control c in page.Controls)
            {
                if (c is AbstractPage)
                {
                    return (AbstractPage)c;
                }
            }
            return null;
        }

        private void mainKryptonNavigator_Selecting(object sender, KryptonPageCancelEventArgs e)
        {

        }

        private void mainKryptonNavigator_Deselected(object sender, KryptonPageEventArgs e)
        {

        }

        private void mainKryptonNavigator_Selected(object sender, KryptonPageEventArgs e)
        {
            if (mainKryptonNavigator.Pages.Count == 0) return;

            if (mainKryptonNavigator.SelectedPage == null) return;

            AbstractPage page = GetAbstractPage(mainKryptonNavigator.SelectedPage);

            if (page == null) return;

            page.FunctionSelected();
        }
    }
}
