using CloudBuilder.Core.DependencyInjection.Form;
using CloudBuilder.Core.DependencyInjection.Request;
using CloudBuilder.Gui.DependencyInjection;
using CloudBuilder.Security.Policy;
using CloudBuilder.Security.Utility;
using Krypton.Toolkit;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CloudBuilder.Gui
{
    public class GuiOperateHelper
    {
        private LoginContextPermission loginContextPermission;
        public GuiOperateHelper()
        {
            this.loginContextPermission = new LoginContextPermission();
        }

        public void EnableControls(bool enable, IComponent c)
        {
            if ((c is Control))
                ((Control)c).SuspendLayout();
            Enable(enable, c);
            if ((c is Control))
                ((Control)c).ResumeLayout();

        }

        public void EnableControls(bool enable, Control.ControlCollection cc)
        {
            IEnumerator enumerator = cc.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Control current = (Control)enumerator.Current;
                EnableControls(enable, current);
            }
        }

        public void EnableControls(bool enable, params IComponent[] cc)
        {
            foreach (IComponent c in cc)
            {
                EnableControls(enable, c);
            }
        }

        public void EnableControls(bool enable, List<IComponent> cc)
        {
            foreach (IComponent c in cc)
            {
                EnableControls(enable, c);
            }
        }

        public bool CheckValidate(Control.ControlCollection cc)
        {
            IEnumerator enumerator = cc.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Control current = (Control)enumerator.Current;
                if (!CheckValidate(current)) return false;
            }
            return true;
        }

        public bool CheckValidate(params IComponent[] cc)
        {
            foreach (IComponent c in cc)
            {
                if (!CheckValidate(c)) return false;
            }
            return true;
        }

        public bool CheckValidate(List<IComponent> cc)
        {
            foreach (IComponent c in cc)
            {
                if (!CheckValidate(c)) return false;
            }
            return true;
        }

        public void ResetControls(params IComponent[] cc)
        {
            foreach (IComponent c in cc)
            {
                ResetControls(c);
            }
        }

        public void ResetControls(List<IComponent> cc)
        {
            ResetControls(cc.ToArray());
        }

        public void ResetControls(IComponent c)
        {
            if (!(c is Control)) return;
            ((Control)c).SuspendLayout();
            Reset(((Control)c));
            ((Control)c).ResumeLayout();
        }

        public void ResetControls(Control.ControlCollection cc)
        {
            IEnumerator enumerator = cc.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Control current = (Control)enumerator.Current;
                ResetControls(current);
            }
        }

        public void SetControlsText(params IComponent[] cc)
        {
            foreach (IComponent c in cc)
            {
                SetControlsText(c);
            }
        }


        public static void SetControlsText(IComponent c, ResourceManager rs)
        {
            if ((c is Control))
                ((Control)c).SuspendLayout();
            SetText(c, rs);
            if ((c is Control))
                ((Control)c).ResumeLayout();
        }

        public static void SetControlsText(Control.ControlCollection cc, ResourceManager rs)
        {
            IEnumerator enumerator = cc.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Control current = (Control)enumerator.Current;
                SetControlsText(current, rs);
            }
        }

        public static void SetControlsText(ResourceManager rs, params IComponent[] cc)
        {
            foreach (IComponent c in cc)
            {
                SetControlsText(c, rs);
            }
        }

        public static void SetControlsText(List<IComponent> cc, ResourceManager rs)
        {
            foreach (Control c in cc)
            {
                SetControlsText(c, rs);
            }
        }

        private void Reset(IComponent c)
        {
            if (!(c is Control)) return;

            if (c is GridViewPagination)
            {
                ((GridViewPagination)c).Reset();
            }
            else if (c is KryptonTextBox)
            {
                ((KryptonTextBox)c).Text = string.Empty;
            }
            else if (c is KryptonDateTimePicker)
            {
                ((KryptonDateTimePicker)c).Checked = false;
            }
            else if (c is KryptonMaskedTextBox)
            {
                ((KryptonMaskedTextBox)c).Text = string.Empty;
            }
            else if (c is KryptonNumericUpDown)
            {
                ((KryptonNumericUpDown)c).Text = "0";
            }
            else if (c is KryptonComboBox)
            {
                ((KryptonComboBox)c).SelectedIndex = -1;
            }
            else if (c is KryptonCheckBox)
            {
                ((KryptonCheckBox)c).Checked = false;
            }
            else if (c is KryptonDataGridView)
            {
                ((KryptonDataGridView)c).Clear();
            }
            else if (c is KryptonCheckedListBox)
            {
                ((KryptonCheckedListBox)c).Reset();
            }
            else if (c is KryptonListBox)
            {
                ((KryptonListBox)c).Items.Clear();
            }
            else if (c is KryptonRadioButton)
            {
                ((KryptonRadioButton)c).Checked = false;
            }
            else
            {
                if (((Control)c).HasChildren) ResetControls(((Control)c).Controls);
            }
        }


        private static void SetText(IComponent c, ResourceManager rs)
        {
            if (c is Label)
            {
                var text = rs.GetString(((Label)c).Name);

                if (text != null) ((Label)c).Text = (string)text;
            }
            if (c is KryptonLabel)
            {
                var text = rs.GetString(((KryptonLabel)c).Name);

                if (text != null) ((KryptonLabel)c).Text = (string)text;
            }
            else if (c is Button)
            {
                var text = rs.GetString(((Button)c).Name);

                if (text != null) ((Button)c).Text = (string)text;
            }
            else if (c is KryptonButton)
            {
                var text = rs.GetString(((KryptonButton)c).Name);

                if (text != null) ((KryptonButton)c).Text = (string)text;
            }
            else if (c is DataGridView)
            {
                SetText(((DataGridView)c).Columns, rs);
            }
            else if (c is KryptonDataGridView)
            {
                SetText(((KryptonDataGridView)c).Columns, rs);
            }
            else if (c is SplitContainer)
            {
                if (((Control)c).HasChildren) SetControlsText(((SplitContainer)c).Panel1, rs);
                if (((Control)c).HasChildren) SetControlsText(((SplitContainer)c).Panel2, rs);
            }
            else if (c is SplitterPanel)
            {
                if (((Control)c).HasChildren) SetControlsText(((SplitterPanel)c).Controls, rs);
            }
            else if (c is Panel)
            {
                if (((Control)c).HasChildren) SetControlsText(((Panel)c).Controls, rs);
            }
            else if (c is TableLayoutPanel)
            {
                if (((Control)c).HasChildren) SetControlsText(((TableLayoutPanel)c).Controls, rs);
            }
            else if (c is AbstractForm)
            {
                if (((Control)c).HasChildren) SetControlsText(((AbstractForm)c).Controls, rs);
            }

        }

        private static void SetText(DataGridViewColumnCollection c, ResourceManager rs)
        {
            foreach (DataGridViewColumn dgvc in c)
            {
                var text = rs.GetString(((DataGridViewColumn)dgvc).Name);
                if (text != null) dgvc.HeaderText = (string)text;

            }
        }

        private void Enable(bool enable, IComponent c)
        {
            IContext loginContext = null!;
            loginContext = ServiceHelper.GetInterfaceService<IContext>()!;

            if (c is KryptonTextBox)
            {
                ((KryptonTextBox)c).ReadOnly = !enable;

                foreach (ButtonSpecAny but in ((KryptonTextBox)c).ButtonSpecs)
                {
                    but.Enabled = enable ? ButtonEnabled.True : ButtonEnabled.False;
                }

                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonTextBox)c);

                if (enable)
                {
                    if (guiBindingContext == null)
                    {
                        //沒有設置時，默认白色
                        ((KryptonTextBox)c).StateCommon.Back.Color1 = Color.White;
                    }
                    else if (guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                    {
                        ((KryptonTextBox)c).StateCommon.Border.Color1 = Color.Peru;
                        ((KryptonTextBox)c).StateActive.Border.DrawBorders = ((Krypton.Toolkit.PaletteDrawBorders)(((
                            (Krypton.Toolkit.PaletteDrawBorders.Top | Krypton.Toolkit.PaletteDrawBorders.Bottom) |
                             Krypton.Toolkit.PaletteDrawBorders.Left) | Krypton.Toolkit.PaletteDrawBorders.Right)));
                        ((KryptonTextBox)c).StateCommon.Back.Color1 = Color.White;
                    }
                    else if (guiBindingContext.EditModeColor == EnumEditModeColor.ReadOnly)
                    {
                        ((KryptonTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                        ((KryptonTextBox)c).ReadOnly = true;
                    }
                    else
                    {
                        ((KryptonTextBox)c).StateCommon.Back.Color1 = Color.White;
                    }
                }
                else
                {
                    ((KryptonTextBox)c).StateCommon.Border.Color1 = Color.LightGray;
                    ((KryptonTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                }

                if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonTextBox)c).Text = null!;
                        ((KryptonTextBox)c).ReadOnly = true;
                        ((KryptonTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Update))
                    {
                        ((KryptonTextBox)c).ReadOnly = true;
                        ((KryptonTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                }
                else if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && !string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonTextBox)c).Text = null!;
                        ((KryptonTextBox)c).ReadOnly = true;
                        ((KryptonTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Update))
                    {
                        ((KryptonTextBox)c).ReadOnly = true;
                        ((KryptonTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                }

            }
            else if (c is KryptonComboBox)
            {
                ((KryptonComboBox)c).Enabled = enable;

                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonComboBox)c);

                if (enable)
                {
                    ((KryptonComboBox)c).StateCommon.ComboBox.Border.Width = 1;

                    if (guiBindingContext == null)
                    {
                        //沒有設置時，默认白色
                        ((KryptonComboBox)c).StateCommon.ComboBox.Back.Color1 = Color.White;
                        ((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.FromArgb(215, 229, 243);
                    }
                    else if (guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                    {
                        ((KryptonComboBox)c).StateCommon.ComboBox.Back.Color1 = Color.White;
                        ((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.Peru;
                        ((KryptonComboBox)c).StateActive.ComboBox.Border.DrawBorders = ((Krypton.Toolkit.PaletteDrawBorders)(((
                            (Krypton.Toolkit.PaletteDrawBorders.Top | Krypton.Toolkit.PaletteDrawBorders.Bottom) |
                             Krypton.Toolkit.PaletteDrawBorders.Left) | Krypton.Toolkit.PaletteDrawBorders.Right)));
                    }
                    else if (guiBindingContext.EditModeColor == EnumEditModeColor.ReadOnly)
                    {
                        ((KryptonComboBox)c).StateCommon.ComboBox.Back.Color1 = Color.FromArgb(234, 242, 251);
                        ((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.LightGray;
                        ((KryptonComboBox)c).Enabled = false;
                    }
                    else
                    {
                        ((KryptonComboBox)c).StateCommon.ComboBox.Back.Color1 = Color.White;
                        ((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.FromArgb(215, 229, 243);
                    }
                }
                else
                {
                    ((KryptonComboBox)c).StateDisabled.ComboBox.Back.Color1 = Color.FromArgb(234, 242, 251);
                    ((KryptonComboBox)c).StateDisabled.ComboBox.Border.Color1 = Color.LightGray;
                    ((KryptonComboBox)c).StateDisabled.ComboBox.Content.Color1 = Color.Black;
                }

                if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonComboBox)c).SelectedIndex = -1;
                        ((KryptonComboBox)c).Enabled = false;
                        ((KryptonComboBox)c).StateCommon.ComboBox.Back.Color1 = Color.FromArgb(234, 242, 251);
                        ((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.LightGray;
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Update))
                    {
                        ((KryptonComboBox)c).Enabled = false;
                        ((KryptonComboBox)c).StateCommon.ComboBox.Back.Color1 = Color.FromArgb(234, 242, 251);
                        ((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.LightGray;
                    }
                }
                else if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && !string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonComboBox)c).SelectedIndex = -1;
                        ((KryptonComboBox)c).Enabled = false;
                        ((KryptonComboBox)c).StateCommon.ComboBox.Back.Color1 = Color.FromArgb(234, 242, 251);
                        ((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.LightGray;
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Update))
                    {
                        ((KryptonComboBox)c).Enabled = false;
                        ((KryptonComboBox)c).StateCommon.ComboBox.Back.Color1 = Color.FromArgb(234, 242, 251);
                        ((KryptonComboBox)c).StateCommon.ComboBox.Border.Color1 = Color.LightGray;
                    }
                }
            }
            else if (c is KryptonCheckBox)
            {
                ((KryptonCheckBox)c).Enabled = enable;

                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonCheckBox)c);

                if (enable)
                {
                    ((KryptonCheckBox)c).StateCommon.ShortText.Color1 = Color.Black;
                    ((KryptonCheckBox)c).StateCommon.LongText.Color1 = Color.Black;

                    if (guiBindingContext != null && guiBindingContext.EditModeColor == EnumEditModeColor.ReadOnly)
                    {
                        ((KryptonCheckBox)c).Enabled = false;
                    }
                }
                else
                {
                    ((KryptonCheckBox)c).StateCommon.ShortText.Color1 = Color.DimGray;
                    ((KryptonCheckBox)c).StateCommon.LongText.Color1 = Color.DimGray;
                }

                if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonCheckBox)c).Visible = false;
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Update))
                    {
                        ((KryptonCheckBox)c).Enabled = false;
                    }
                }
                else if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && !string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonCheckBox)c).Visible = false;
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Update))
                    {
                        ((KryptonCheckBox)c).Enabled = false;
                    }
                }
            }
            else if (c is KryptonDateTimePicker)
            {
                ((KryptonDateTimePicker)c).Enabled = enable;

                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonDateTimePicker)c);

                if (enable)
                {
                    ((KryptonDateTimePicker)c).StateCommon.Border.Width = 1;

                    if (guiBindingContext == null)
                    {
                        //沒有設置時，默认白色
                        ((KryptonDateTimePicker)c).StateCommon.Border.Color1 = Color.FromArgb(215, 229, 243);
                        ((KryptonDateTimePicker)c).StateCommon.Back.Color1 = Color.White;
                    }
                    else if (guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                    {
                        ((KryptonDateTimePicker)c).StateCommon.Back.Color1 = Color.White;
                        ((KryptonDateTimePicker)c).StateCommon.Border.Color1 = Color.Peru;
                        ((KryptonDateTimePicker)c).StateActive.Border.DrawBorders = ((Krypton.Toolkit.PaletteDrawBorders)(((
                            (Krypton.Toolkit.PaletteDrawBorders.Top | Krypton.Toolkit.PaletteDrawBorders.Bottom) |
                             Krypton.Toolkit.PaletteDrawBorders.Left) | Krypton.Toolkit.PaletteDrawBorders.Right)));
                    }
                    else if (guiBindingContext.EditModeColor == EnumEditModeColor.ReadOnly)
                    {
                        ((KryptonDateTimePicker)c).StateCommon.Border.Color1 = Color.LightGray;
                        ((KryptonDateTimePicker)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                        ((KryptonDateTimePicker)c).Enabled = false;
                    }
                    else
                    {
                        ((KryptonDateTimePicker)c).StateCommon.Back.Color1 = Color.White;
                        ((KryptonDateTimePicker)c).StateCommon.Border.Color1 = Color.FromArgb(215, 229, 243);
                    }

                }
                else
                {
                    ((KryptonDateTimePicker)c).StateDisabled.Border.Color1 = Color.LightGray;
                    ((KryptonDateTimePicker)c).StateDisabled.Back.Color1 = Color.FromArgb(234, 242, 251);
                    ((KryptonDateTimePicker)c).StateDisabled.Content.Color1 = Color.Black;
                }

                if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonDateTimePicker)c).Checked = false;
                        ((KryptonDateTimePicker)c).Enabled = false;
                        ((KryptonDateTimePicker)c).StateCommon.Border.Color1 = Color.LightGray;
                        ((KryptonDateTimePicker)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Update))
                    {
                        ((KryptonDateTimePicker)c).Enabled = false;
                        ((KryptonDateTimePicker)c).StateCommon.Border.Color1 = Color.LightGray;
                        ((KryptonDateTimePicker)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                }
                else if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && !string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonDateTimePicker)c).Checked = false;
                        ((KryptonDateTimePicker)c).Enabled = false;
                        ((KryptonDateTimePicker)c).StateCommon.Border.Color1 = Color.LightGray;
                        ((KryptonDateTimePicker)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Update))
                    {
                        ((KryptonDateTimePicker)c).Enabled = false;
                        ((KryptonDateTimePicker)c).StateCommon.Border.Color1 = Color.LightGray;
                        ((KryptonDateTimePicker)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                }
            }
            else if (c is KryptonMaskedTextBox)
            {
                ((KryptonMaskedTextBox)c).ReadOnly = !enable;

                foreach (ButtonSpecAny but in ((KryptonMaskedTextBox)c).ButtonSpecs)
                {
                    but.Enabled = enable ? ButtonEnabled.True : ButtonEnabled.False;
                }

                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonMaskedTextBox)c);

                if (enable)
                {
                    if (guiBindingContext == null)
                    {
                        //沒有設置時，默认白色
                        ((KryptonMaskedTextBox)c).StateCommon.Back.Color1 = Color.White;
                    }
                    else if (guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                    {
                        ((KryptonMaskedTextBox)c).StateCommon.Border.Color1 = Color.Peru;
                        ((KryptonMaskedTextBox)c).StateActive.Border.DrawBorders = ((Krypton.Toolkit.PaletteDrawBorders)(((
                            (Krypton.Toolkit.PaletteDrawBorders.Top | Krypton.Toolkit.PaletteDrawBorders.Bottom) |
                             Krypton.Toolkit.PaletteDrawBorders.Left) | Krypton.Toolkit.PaletteDrawBorders.Right)));
                        ((KryptonMaskedTextBox)c).StateCommon.Back.Color1 = Color.White;
                    }
                    else if (guiBindingContext.EditModeColor == EnumEditModeColor.ReadOnly)
                    {
                        ((KryptonMaskedTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonMaskedTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                        ((KryptonMaskedTextBox)c).ReadOnly = true;
                    }
                    else
                    {
                        ((KryptonMaskedTextBox)c).StateCommon.Back.Color1 = Color.White;
                    }

                }
                else
                {
                    ((KryptonMaskedTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                    ((KryptonMaskedTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    ((KryptonMaskedTextBox)c).StateCommon.Content.Color1 = Color.Black;
                }


                if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonMaskedTextBox)c).Text = null!;
                        ((KryptonMaskedTextBox)c).ReadOnly = true;
                        ((KryptonMaskedTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonMaskedTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Update))
                    {
                        ((KryptonMaskedTextBox)c).ReadOnly = true;
                        ((KryptonMaskedTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonMaskedTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                }
                else if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && !string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                {
                    if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Find))
                    {
                        guiBindingContext.BindDataName = null!;
                        ((KryptonMaskedTextBox)c).Text = null!;
                        ((KryptonMaskedTextBox)c).ReadOnly = true;
                        ((KryptonMaskedTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonMaskedTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                    if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Update))
                    {
                        ((KryptonMaskedTextBox)c).ReadOnly = true;
                        ((KryptonMaskedTextBox)c).StateCommon.Border.Color1 = Color.Gainsboro;
                        ((KryptonMaskedTextBox)c).StateCommon.Back.Color1 = Color.FromArgb(234, 242, 251);
                    }
                }
            }
            else if (c is KryptonDataGridView)
            {
                foreach (DataGridViewColumn column in ((KryptonDataGridView)c).Columns)
                {
                    column.ReadOnly = !enable;

                    BindingContextCustomAttribute guiBindingContext = column.GetCustomAttribute();
                    if (enable)
                    {
                        if (guiBindingContext == null)
                        {
                            //沒有設置時，默认白色
                            column.DefaultCellStyle.BackColor = Color.White;
                        }
                        else if (guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                        {
                            column.DefaultCellStyle.BackColor = Color.LightYellow;
                        }
                        else if (guiBindingContext.EditModeColor == EnumEditModeColor.ReadOnly)
                        {
                            column.DefaultCellStyle.BackColor = Color.Gainsboro;
                            column.ReadOnly = true;
                        }
                        else
                        {
                            column.DefaultCellStyle.BackColor = Color.White;
                        }


                    }
                    else
                    {
                        column.DefaultCellStyle.BackColor = Color.FromArgb(234, 242, 251);
                    }

                    if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                    {
                        if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Find))
                        {
                            column.DataPropertyName = null;
                            column.ReadOnly = true;
                            column.DefaultCellStyle.BackColor = Color.FromArgb(234, 242, 251);
                        }
                        if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, Operate.Update))
                        {
                            column.ReadOnly = true;
                            column.DefaultCellStyle.BackColor = Color.FromArgb(234, 242, 251);
                        }
                    }
                    else if (guiBindingContext != null && !string.IsNullOrEmpty(guiBindingContext.PermissionName) && !string.IsNullOrEmpty(guiBindingContext.DependentDetailName))
                    {
                        if (guiBindingContext.PermissionMode == Operate.Find && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Find))
                        {
                            column.DataPropertyName = null;
                            column.ReadOnly = true;
                            column.DefaultCellStyle.BackColor = Color.FromArgb(234, 242, 251);
                        }
                        if (guiBindingContext.PermissionMode == Operate.Update && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, Operate.Update))
                        {
                            column.ReadOnly = true;
                            column.DefaultCellStyle.BackColor = Color.FromArgb(234, 242, 251);
                        }
                    }
                }
            }
            if (c is KryptonCheckedListBox)
            {
                ((KryptonCheckedListBox)c).Enabled = enable;
            }
            else if (c is KryptonListBox)
            {
                ((KryptonListBox)c).Enabled = enable;
            }
            else if (c is KryptonRadioButton)
            {
                ((KryptonRadioButton)c).Enabled = enable;
            }
            else if (c is KryptonButton)
            {
                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonButton)c);

                ((KryptonButton)c).Enabled = enable;

                if (enable && guiBindingContext != null
                    && !string.IsNullOrEmpty(guiBindingContext.PermissionName)
                    && loginContext != null)
                {
                    if (string.IsNullOrEmpty(guiBindingContext.DependentDetailName) && !loginContext.Assert(loginContextPermission.Assert, guiBindingContext.PermissionName, guiBindingContext.PermissionMode))
                    {
                        ((KryptonButton)c).Enabled = false;
                    }
                    else if (!string.IsNullOrEmpty(guiBindingContext.DependentDetailName) && !loginContext.AssertDependents(loginContextPermission.AssertDependents, guiBindingContext.PermissionName, guiBindingContext.DependentDetailName, guiBindingContext.PermissionMode))
                    {
                        ((KryptonButton)c).Enabled = false;
                    }
                }
            }
            else
            {
                if (((Control)c).HasChildren) EnableControls(enable, ((Control)c).Controls);
            }

        }

        private bool CheckValidate(IComponent c)
        {
            IContext loginContext = null!;
            loginContext = ServiceHelper.GetInterfaceService<IContext>()!;
            SecurityMessagePolicy securityMessagePolicy = ServiceHelper.GetTypeService<SecurityMessagePolicy>()!;
            bool isCheck = true;

            if (c is KryptonTextBox)
            {
                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonTextBox)c);

                if (!((KryptonTextBox)c).ReadOnly && guiBindingContext != null && guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                {
                    //沒有設置時，默认白色
                    if (string.IsNullOrEmpty(((KryptonTextBox)c).GetValue()))
                    {
                        ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_VALID_INPUT, GuiOperateHelper.GetLabelText(guiBindingContext.BindDisplayName)));
                        ((KryptonTextBox)c).Focus();
                        return false;
                    }
                }
            }
            else if (c is KryptonComboBox)
            {
                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonComboBox)c);

                if (((KryptonComboBox)c).Enabled && guiBindingContext != null && guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                {
                    //沒有設置時，默认白色
                    if (((KryptonComboBox)c).SelectedIndex <= 0)
                    {
                        ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_VALID_SELECT, GuiOperateHelper.GetLabelText(guiBindingContext.BindDisplayName)));
                        ((KryptonComboBox)c).Focus();
                        return false;
                    }
                }
            }
            else if (c is KryptonDateTimePicker)
            {
                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonDateTimePicker)c);

                if (((KryptonDateTimePicker)c).Enabled && guiBindingContext != null && guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                {
                    //沒有設置時，默认白色
                    if (!((KryptonDateTimePicker)c).Checked)
                    {
                        ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_VALID_INPUT, GuiOperateHelper.GetLabelText(guiBindingContext.BindDisplayName)));
                        ((KryptonDateTimePicker)c).Focus();
                        return false;
                    }
                }
            }
            else if (c is KryptonMaskedTextBox)
            {
                BindingContextCustomAttribute guiBindingContext = ServiceHelper.GetService<BindingContextAttributeProvider>()!.GetCustomAttribute((KryptonMaskedTextBox)c);

                if (!((KryptonMaskedTextBox)c).ReadOnly && guiBindingContext != null && guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                {
                    //沒有設置時，默认白色
                    if (string.IsNullOrEmpty(((KryptonMaskedTextBox)c).GetValue()))
                    {
                        ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_VALID_INPUT, GuiOperateHelper.GetLabelText(guiBindingContext.BindDisplayName)));
                        ((KryptonMaskedTextBox)c).Focus();
                        return false;
                    }
                }
            }
            else if (c is KryptonDataGridView)
            {
                if (!((KryptonDataGridView)c).Enabled) return isCheck;
                foreach (DataGridViewRow row in ((KryptonDataGridView)c).Rows)
                {
                    foreach (DataGridViewColumn column in ((KryptonDataGridView)c).Columns)
                    {
                        if (column.ReadOnly) continue;

                        BindingContextCustomAttribute guiBindingContext = column.GetCustomAttribute();
                        if (guiBindingContext != null && guiBindingContext.EditModeColor == EnumEditModeColor.Required)
                        {
                            if (column is DataGridViewComboBoxColumn || column is KryptonDataGridViewTextBoxColumn)
                            {
                                if (string.IsNullOrEmpty(row.Cells[column.Name].Value.ToString()))
                                {
                                    ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_VALID_INPUT, column.HeaderText));
                                    ((KryptonDataGridView)c).CurrentCell = ((KryptonDataGridView)c).Rows[row.Index].Cells[column.Index];
                                    ((KryptonDataGridView)c).FirstDisplayedScrollingRowIndex = row.Index;
                                    return false;
                                }
                            }
                            else if (column is KryptonDataGridViewDateTimePickerColumn)
                            {
                                if (!((KryptonDataGridViewDateTimePickerCell)row.Cells[column.Name]).Checked)
                                {
                                    ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_VALID_INPUT, column.HeaderText));
                                    ((KryptonDataGridView)c).CurrentCell = ((KryptonDataGridView)c).Rows[row.Index].Cells[column.Index];
                                    ((KryptonDataGridView)c).FirstDisplayedScrollingRowIndex = row.Index;
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                if (((Control)c).HasChildren) isCheck = CheckValidate(((Control)c).Controls);
            }
            return isCheck;
        }

        public static string GetLabelText(string text)
        {
            if (string.IsNullOrEmpty(text)) return null;

            return text.Substring(0, text.Length - 1);
        }

        public void ComposeMessage(string groupName, params Control[] cc)
        {
            foreach (Control c in cc)
            {
                c.Text = ApplicationServiceContext.localeMessageManager.ComposeMessage(groupName, c.Name);
            }
        }

        public void ComposeMessage(string groupName, Control.ControlCollection cc)
        {
            foreach (Control c in cc)
            {
                c.Text = ApplicationServiceContext.localeMessageManager.ComposeMessage(groupName, c.Name);
            }
        }
    }
}
