using CloudBuilder.Core.Service;
using CloudBuilder.Gui;
using CloudBuilder.Request.Security;
using CloudBuilder.Security.Data;
using CloudBuilder.Security.Entity;
using CloudBuilder.Security.Linkup;
using CloudBuilder.Security.Policy;
using CloudBuilder.Security.SearchCriteria;
using Krypton.Toolkit;
using Microsoft.AspNetCore.Authorization;
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 SecurityGroupOperationPage : AbstractPage
    {
        private SecurityMessagePolicy securityMessagePolicy;
        private SecurityGroupRequest securityGroupRequest;
        private SecurityGroupEntity securityGroupEntity;
        private SecurityGroupData data;
        public SecurityGroupOperationPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 第一次加载时执行，只执行一次
        /// </summary>
        public override void FunctionLoaded()
        {
            try
            {
                base.FunctionLoaded();
                //初始化容器-------------------------------------
                securityMessagePolicy = ServiceHelper.GetTypeService<SecurityMessagePolicy>()!;
                securityGroupRequest = ServiceHelper.GetTypeService<SecurityGroupRequest>();

                //界面初始化
                InitGui();
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void InitGui()
        {
            InitBindingContext();

            InitDisplayMode();

            this.ParentAbstractForm.AcceptButton = this.searchBut;
            this.ParentAbstractForm.CancelButton = this.resetBut;
        }

        /// <summary>
        /// 有特殊要求的UI控件初始化设置,Init与Add配对后，使用guiOperateHelper管理
        /// </summary>
        private void InitBindingContext()
        {
            //需要绑定数据
            guiBindingContext = new GuiBindingContext();

            InitPermission();

            groupNameTxt.Set(SecurityGroupEntity.GROUP_NAME, groupNameLabel.Text, EnumEditModeColor.Required);
            groupEngNameTxt.Set(SecurityGroupEntity.GROUP_ENG_NAME, groupLocalNameLabel.Text, EnumEditModeColor.Required);
            groupLocalNameTxt.Set(SecurityGroupEntity.GROUP_LOCAL_NAME, groupEngNameLabel.Text, EnumEditModeColor.Required);

            dataGv.InitColumn(false, true);
            dataDsGv.InitColumn(false, true);
            actorGv.InitColumn(false, true);

            guiOperateHelper = new GuiOperateHelper();
        }

        private void InitPermission()
        {
            createBut.Set(SecurityPermissionPolicy.SecurityGroup, Operate.Insert);
            amendBut.Set(SecurityPermissionPolicy.SecurityGroup, Operate.Update);
            deleteBut.Set(SecurityPermissionPolicy.SecurityGroup, Operate.Delete);
        }

        /// <summary>
        /// 每次从别的界面进入时执行，发生在FunctionLoaded之后
        /// </summary>
        /// <param name="param"></param>
        public override void FunctionActivated(object param)
        {
            try
            {
                if (param == null)
                {
                    groupNameSearchTxt.Focus();
                    return;
                }

                InitDisplayMode();

                SecurityGroupOperationLinkup linkup = null;

                if (param is SecurityGroupOperationLinkup)
                {
                    linkup = (SecurityGroupOperationLinkup)param;
                }

                this.DisplayMode = (GuiDisplayMode)linkup.DisplayMode;

                groupNameSearchTxt.Text = linkup.GroupName;

                if (this.DisplayMode == GuiDisplayMode.View)
                {
                    Search();
                }
                else if (this.DisplayMode == GuiDisplayMode.Create)
                {
                    Create();
                }
                else if (this.DisplayMode == GuiDisplayMode.Amend)
                {
                    Search();
                    Amend();
                }

                groupNameSearchTxt.Focus();
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        public override void FunctionSelected()
        {
            this.ParentAbstractForm.AcceptButton = this.searchBut;
            if (!IsDialog)
                this.ParentAbstractForm.CancelButton = this.resetBut;
            else
                this.ParentAbstractForm.CancelButton = this.cancelBut;

            groupNameSearchTxt.Focus();
        }

        public override bool FunctionDeactivating()
        {
            if (this.DisplayMode == GuiDisplayMode.View)
            {
                return true;
            }
            else
            {
                return CancelEditing();
            }
        }

        private bool CancelEditing()
        {
            DialogResult result = ShowMessageBox.ShowOption(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_CONFIRM_CANCEL), securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_CONFIRM));

            //从外部跳转(非mainKryptonNavigator)，result第一次不生效？，所以再执行一次生效
            if (result == DialogResult.Cancel)
            {
                result = ShowMessageBox.ShowOption(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_CONFIRM_CANCEL), securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_CONFIRM));
            }

            if (result == DialogResult.No)
            {
                return false;
            }

            Cancel();

            return true;
        }

        private void InitDisplayMode()
        {
            this.DisplayMode = GuiDisplayMode.View;

            SetDisplayMode();
        }

        private void SetDisplayMode()
        {
            if (this.DisplayMode == GuiDisplayMode.View)
            {
                savePanel.Visible = false;
                operationPanel.Visible = true;

                guiOperateHelper.EnableControls(true, topPanel);
                guiOperateHelper.EnableControls(false, contentPanel);
                //查詢有記錄時,修改等控件有效
                if (securityGroupEntity != null)
                    guiOperateHelper.EnableControls(true, bottomPanel);
                else
                    guiOperateHelper.EnableControls(false, bottomPanel);
            }
            else if (this.DisplayMode == GuiDisplayMode.Amend || this.DisplayMode == GuiDisplayMode.Create)
            {
                savePanel.Visible = true;
                operationPanel.Visible = false;

                guiOperateHelper.EnableControls(false, topPanel);
                guiOperateHelper.EnableControls(true, bottomPanel, contentPanel);

                if (this.DisplayMode == GuiDisplayMode.Amend) guiOperateHelper.EnableControls(false, groupNameTxt);


                permissionNameCol.ReadOnly = true;
                permissionNameCol.DefaultCellStyle.BackColor = System.Drawing.Color.Silver;
                permissionDescriptionCol.ReadOnly = true;
                permissionDescriptionCol.DefaultCellStyle.BackColor = System.Drawing.Color.Silver;
                dependentCol.ReadOnly = true;
                dependentCol.DefaultCellStyle.BackColor = System.Drawing.Color.Silver;

                dsDependentCol.ReadOnly = true;
                dsDependentCol.DefaultCellStyle.BackColor = System.Drawing.Color.Silver;
                dsDependentDetailNameCol.ReadOnly = true;
                dsDependentDetailNameCol.DefaultCellStyle.BackColor = System.Drawing.Color.Silver;
                dsDependentDetailDescriptionCol.ReadOnly = true;
                dsDependentDetailDescriptionCol.DefaultCellStyle.BackColor = System.Drawing.Color.Silver;

                actorGv.DefaultCellStyle.BackColor = System.Drawing.Color.Silver;
            }
        }

        private void PackUIToEntity()
        {
            guiBindingContext.DataPack();

            SecurityGroupPermissionViewDisplayData[] securityGroupPermissions = dataGv.GetAllRows<SecurityGroupPermissionViewDisplayData>();

            if (securityGroupPermissions == null || securityGroupPermissions.Length == 0)
                data.SecurityGroupPermissionViews = null;
            else
                data.SecurityGroupPermissionViews = securityGroupPermissions.Select(x => new SecurityGroupPermissionViewDisplayData()
                {
                    PermissionName = x.PermissionName,
                    GroupName = groupNameTxt.Text,
                    ViewIndc = x.ViewIndc,
                    AmendIndc = x.AmendIndc,
                    CreateIndc = x.CreateIndc,
                    DeleteIndc = x.DeleteIndc,
                    Dependent = x.Dependent,
                    IsView = x.ViewIndc == YesNoPolicy.YES,
                    IsAmend = x.AmendIndc == YesNoPolicy.YES,
                    IsCreate = x.CreateIndc == YesNoPolicy.YES,
                    IsDelete = x.DeleteIndc == YesNoPolicy.YES,
                    PermissionDescription = x.PermissionDescription
                }).ToArray();

            SecurityGroupPermissionDependentViewDisplayData[] dependents = dataDsGv.GetAllRows<SecurityGroupPermissionDependentViewDisplayData>();

            if (dependents == null || dependents.Length == 0)
                data.SecurityGroupPermissionDependentViews = null;
            else
                data.SecurityGroupPermissionDependentViews = dependents.Select(x => new SecurityGroupPermissionDependentViewDisplayData()
                {
                    PermissionName = x.PermissionName,
                    GroupName = groupNameTxt.Text,
                    ViewIndc = x.ViewIndc,
                    AmendIndc = x.AmendIndc,
                    CreateIndc = x.CreateIndc,
                    DeleteIndc = x.DeleteIndc,
                    Dependent = x.Dependent,
                    IsView = x.ViewIndc == YesNoPolicy.YES,
                    IsAmend = x.AmendIndc == YesNoPolicy.YES,
                    IsCreate = x.CreateIndc == YesNoPolicy.YES,
                    IsDelete = x.DeleteIndc == YesNoPolicy.YES,
                    PermissionDescription = x.PermissionDescription,
                    DependentDetailDescription = x.DependentDetailDescription,
                    DependentDetailName = x.DependentDetailName,
                }).ToArray();

            SecurityActorGroupViewEntity[] actors = actorGv.GetAllRows<SecurityActorGroupViewEntity>();

            if (actors == null || actors.Length == 0)
                data.SecurityActorGroups = null;
            else
                data.SecurityActorGroups = actors.Select(x => new SecurityActorGroupViewEntity()
                {
                    ActorName = x.ActorName,
                    GroupName = groupNameTxt.Text,
                    ActorLocalName = x.ActorLocalName,
                }).ToArray();
        }

        private bool SaveValidate()
        {
            if (!guiOperateHelper.CheckValidate(this.mainPanel)) return false;

            return true;
        }

        private void Search()
        {
            try
            {
                if (string.IsNullOrEmpty(groupNameSearchTxt.Text)) return;

                Reset();

                data = securityGroupRequest.FindByKey(groupNameSearchTxt.Text);

                if (data == null || data.SecurityGroup == null)
                {
                    ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_WS_RECORD_NOT_FOUND));
                }
                else
                {
                    securityGroupEntity = data.SecurityGroup;

                    PackEntityToUI();

                    SetDisplayMode();
                }
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void Create()
        {
            this.DisplayMode = GuiDisplayMode.Create;

            guiOperateHelper.ResetControls(this.mainPanel);

            SetDisplayMode();

            data = new SecurityGroupData();
            securityGroupEntity = new SecurityGroupEntity()
            {
                CreatedBy = string.Empty,
                UpdatedBy = string.Empty,
                CreatedDatetime = DateTime.Now,
                UpdatedDatetime = DateTime.Now,
            };
            data.SecurityGroup = securityGroupEntity;

            PackEntityToUI();

            groupNameTxt.Focus();
        }

        private void Amend()
        {
            this.DisplayMode = GuiDisplayMode.Amend;

            SetDisplayMode();
        }

        private void PackEntityToUI()
        {
            guiBindingContext.DataBind(data.SecurityGroup, this);

            dataGv.SetDataSource(data.SecurityGroupPermissionViews);
            dataDsGv.SetDataSource(data.SecurityGroupPermissionDependentViews);
            actorGv.SetDataSource(data.SecurityActorGroups);
        }

        private void Reset()
        {
            this.DisplayMode = GuiDisplayMode.View;

            securityGroupEntity = null;

            guiOperateHelper.ResetControls(this.contentPanel);

            SetDisplayMode();

            groupNameSearchTxt.Focus();
        }

        public void Cancel()
        {
            if (!string.IsNullOrEmpty(groupNameSearchTxt.Text))
                Search();
            else
                Reset();

        }


        private void groupNameSearchBut_Click(object sender, EventArgs e)
        {
            try
            {
                AbstractForm dialog = GetAbstractDialog(typeof(SecurityGroupEnquiryPage).Name);

                if (DialogResult.OK != dialog.ShowDialog()) return;

                groupNameSearchTxt.Text = ((SecurityGroupEntity[])dialog.OutputData)[0].GroupName;

                Search();
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void searchBut_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(groupNameSearchTxt.Text))
                {
                    ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_VALID_INPUT, GuiOperateHelper.GetLabelText(groupNameSearchLabel.Text)));
                    this.groupNameSearchTxt.Focus();
                    return;
                }

                this.DisplayMode = GuiDisplayMode.View;

                Search();

            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void resetBut_Click(object sender, EventArgs e)
        {
            try
            {
                guiOperateHelper.ResetControls(this.topPanel);
                Reset();
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void createBut_Click(object sender, EventArgs e)
        {
            try
            {
                Create();
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void amendBut_Click(object sender, EventArgs e)
        {
            try
            {
                Amend();
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void deleteBut_Click(object sender, EventArgs e)
        {
            try
            {
                if (securityGroupEntity == null)
                {
                    ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_WS_RECORD_NOT_FOUND));
                    return;
                }

                if (DialogResult.Yes != ShowMessageBox.ShowOption(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_CONFIRM_DEL_NULL), securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_DELETE))) return;

                securityGroupRequest.Delete(securityGroupEntity.GroupName);

                ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_SUCCEED_DELETED_NULL));

                Reset();
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void saveBut_Click(object sender, EventArgs e)
        {
            try
            {
                if (!SaveValidate()) return;

                PackUIToEntity();


                if (this.DisplayMode == GuiDisplayMode.Create)
                    securityGroupRequest.Insert(data);
                else
                    securityGroupRequest.Update(data);


                ShowMessageBox.ShowInfo((this.DisplayMode == GuiDisplayMode.Create) ?
                    securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_SUCCEED_CREATED_NULL) :
                    securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_SUCCEED_UPDATED_NULL));

                groupNameSearchTxt.Text = securityGroupEntity.GroupName;
                Search();
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void cancelBut_Click(object sender, EventArgs e)
        {
            try
            {
                CancelEditing();
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void addBut_Click(object sender, EventArgs e)
        {
            try
            {
                AbstractForm dialog = GetAbstractDialog(typeof(SecurityPermissionEnquiryPage).Name);

                if (DialogResult.OK != dialog.ShowDialog()) return;

                SecurityPermissionEntity[] securityPermissions = (SecurityPermissionEntity[])dialog.OutputData;
                List<string> permissionNames = new List<string>();
                if (dataGv.Rows.Count > 0)
                {
                    permissionNames.AddRange(dataGv.GetAllRows<SecurityGroupPermissionViewDisplayData>().Select(x => x.PermissionName));
                }

                SecurityGroupPermissionViewDisplayData d;

                foreach (SecurityPermissionEntity securityPermission in securityPermissions)
                {
                    if (permissionNames.Contains(securityPermission.PermissionName)) continue;
                    permissionNames.Add(securityPermission.PermissionName);

                    d = new SecurityGroupPermissionViewDisplayData()
                    {
                        PermissionName = securityPermission.PermissionName,
                        PermissionDescription = securityPermission.Description,
                        Dependent = securityPermission.Dependent,
                        ViewIndc = YesNoPolicy.YES,
                        AmendIndc = YesNoPolicy.YES,
                        CreateIndc = YesNoPolicy.YES,
                        DeleteIndc = YesNoPolicy.YES,
                        IsView = true,
                        IsAmend = true,
                        IsCreate = true,
                        IsDelete = true,
                    };

                    dataGv.AddRow(d);
                }
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void delBut_Click(object sender, EventArgs e)
        {
            if (dataGv.RowCount <= 0 || dataGv.SelectedCells.Count <= 0)
            {
                ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_WS_RECORD_NOT_FOUND));
                return;
            }

            List<string> dependents = dataGv.GetAllRows<SecurityGroupPermissionViewDisplayData>().Select(x => x.Dependent).ToList();

            dataGv.DeleteRows(dataGv.SelectedCells);

            SecurityGroupPermissionDependentViewDisplayData[] ds = dataDsGv.GetAllRows<SecurityGroupPermissionDependentViewDisplayData>();

            if (ds != null && ds.Length > 0)
            {
                dataDsGv.SetDataSource(ds.Where(x => !dependents.Contains(x.Dependent)).ToList());
            }
        }

        private void addDsBut_Click(object sender, EventArgs e)
        {
            //AddDs(null);
        }

        private void AddDs(SecurityGroupPermissionViewDisplayData displayData)
        {
            try
            {
                AbstractForm dialog = GetAbstractDialog(typeof(SecurityPermissionDependentEnquiryPage).Name);

                SecurityPermissionSearchCriteria criteria = new SecurityPermissionSearchCriteria();
                criteria.PermissionName = displayData.PermissionName;
                criteria.Dependent = displayData.Dependent;
                criteria.DependentEnable = false;
                if (DialogResult.OK != dialog.ShowViewDialog(null, criteria)) return;

                SecurityPermissionDependentEntity[] securityPermissions = (SecurityPermissionDependentEntity[])dialog.OutputData;
                List<string> dependentDetailNames = new List<string>();
                if (dataDsGv.Rows.Count > 0)
                {
                    dependentDetailNames.AddRange(dataDsGv.GetAllRows<SecurityPermissionDependentEntity>().Select(x => x.PermissionName + "_" + x.DependentDetailName));
                }

                SecurityGroupPermissionDependentViewDisplayData d;

                foreach (SecurityPermissionDependentEntity securityPermission in securityPermissions)
                {
                    if (dependentDetailNames.Contains(securityPermission.PermissionName + "_" + securityPermission.DependentDetailName)) continue;
                    dependentDetailNames.Add(securityPermission.PermissionName + "_" + securityPermission.DependentDetailName);

                    d = new SecurityGroupPermissionDependentViewDisplayData()
                    {
                        DependentDetailDescription = securityPermission.DependentDetailDescription,
                        DependentDetailName = securityPermission.DependentDetailName,
                        PermissionName = securityPermission.PermissionName,
                        Dependent = securityPermission.Dependent,
                        ViewIndc = YesNoPolicy.YES,
                        AmendIndc = YesNoPolicy.YES,
                        CreateIndc = YesNoPolicy.YES,
                        DeleteIndc = YesNoPolicy.YES,
                        IsView = true,
                        IsAmend = true,
                        IsCreate = true,
                        IsDelete = true,
                    };

                    dataDsGv.AddRow(d);
                }
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void delDsBut_Click(object sender, EventArgs e)
        {
            if (dataDsGv.RowCount <= 0 || dataDsGv.SelectedCells.Count <= 0)
            {
                ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_WS_RECORD_NOT_FOUND));
                return;
            }

            dataDsGv.DeleteRows(dataDsGv.SelectedCells);
        }

        private void addActorBut_Click(object sender, EventArgs e)
        {
            try
            {
                AbstractForm dialog = GetAbstractDialog(typeof(SecurityActorEnquiryPage).Name);

                if (DialogResult.OK != dialog.ShowDialog()) return;

                SecurityActorEntity[] securityActors = (SecurityActorEntity[])dialog.OutputData;
                List<string> actorNames = new List<string>();
                if (actorGv.Rows.Count > 0)
                {
                    actorNames.AddRange(actorGv.GetAllRows<SecurityActorEntity>().Select(x => x.ActorName));
                }

                SecurityActorGroupViewEntity d;

                foreach (SecurityActorEntity securityActor in securityActors)
                {
                    if (actorNames.Contains(securityActor.ActorName)) continue;
                    actorNames.Add(securityActor.ActorName);

                    d = new SecurityActorGroupViewEntity()
                    {
                        ActorName = securityActor.ActorName,
                        ActorLocalName = securityActor.ActorLocalName
                    };

                    actorGv.AddRow(d);
                }
            }
            catch (ValidatedException ex)
            {
                ShowMessageBox.ShowInfo(ex.Message);
            }
            catch (ServiceErrorException ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
            catch (Exception ex)
            {
                ShowMessageBox.ShowError(ex.Message);
            }
        }

        private void delActorBut_Click(object sender, EventArgs e)
        {
            if (actorGv.RowCount <= 0 || actorGv.SelectedCells.Count <= 0)
            {
                ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_WS_RECORD_NOT_FOUND));
                return;
            }

            actorGv.DeleteRows(actorGv.SelectedCells);
        }

        private void dataGv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.DisplayMode == GuiDisplayMode.View) return;

            if (e.RowIndex < 0 || e.ColumnIndex != dependentCol.Index) return;
            SecurityGroupPermissionViewDisplayData d = dataGv.GetRowData<SecurityGroupPermissionViewDisplayData>(e.RowIndex);

            if (string.IsNullOrEmpty(d.Dependent)) return;

            AddDs(d);
        }

        private void dataGv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGv.RowCount <= 0 || dataGv.SelectedCells.Count <= 0)
            {
                ShowMessageBox.ShowInfo(securityMessagePolicy.ComposeMessage(SecurityMessagePolicy.GUI_WS_RECORD_NOT_FOUND));
                return;
            }

            SecurityGroupPermissionViewDisplayData data = dataGv.GetRowData<SecurityGroupPermissionViewDisplayData>(dataGv.SelectedCells[0].RowIndex);

            DisplayPage(GuiDisplayMode.View, data.PermissionName);
        }

        private void DisplayPage(GuiDisplayMode mode, string permissionName)
        {
            SecurityPermissionOperationLinkup linkupData = new SecurityPermissionOperationLinkup();
            linkupData.DisplayMode = Convert.ToInt32(mode);
            linkupData.PermissionName = permissionName;

            this.DisplayAbstractPage(typeof(SecurityPermissionOperationPage).Name, linkupData);
        }
    }
}
