using System;
using System.Windows;
using MECF.Framework.Common.Account;
using MECF.Framework.Common.Account.Permissions;
using MECF.Framework.UI.Client.ClientBase;
using MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel.Params;

namespace MECF.Framework.UI.Client.RecipeEditorLib.DGExtension.CustomColumn
{
    public class EditorDataGridTemplateColumnBase: ExtendedGrid.Microsoft.Windows.Controls.DataGridTemplateColumn
    {
        #region Variables

        private MenuPermissionEnum _permission;

        #endregion

        public EditorDataGridTemplateColumnBase()
        {
            IsReadOnly = false;
            IsEnable = true;
            IsEditable = true;
            _permission = MenuPermissionEnum.MP_NONE;
            Visibility = Visibility.Visible;
            IsExpander = false;
        }
        public string ModuleName { get; set; }

        public string DisplayName { get; set; }

        public string UnitName { get; set; }

        public string ControlName { get; set; }

        public string Description { get; set; }

        public string StringCellTemplate { get; set; }

        public string StringCellEditingTemplate { get; set; }

        public string StringHeaderTemplate { get; set; }

        public bool  IsEnable { get; set; }

        public bool IsEditable { get; set; }

        public bool IgnoreGasFlowCalculate { get; set; }

        /// <summary>
        /// 返回当前列是否为列扩展器。
        /// </summary>
        public bool IsExpander { get; protected set; }

        public string Default { get; set; }

        public bool EnableConfig { get; set; }

        public bool EnableTolerance { get; set; }

        /// <summary>
        /// 设置当前列权限。
        /// </summary>
        public MenuPermissionEnum Permission
        {
            get => _permission;
            set
            {
                _permission = value;
                UpdateVisibility();
                UpdatePermission();
            }
        }

        public bool IsColumnSelected
        {
            get => (bool)GetValue(IsColumnSelectedProperty);
            set => SetValue(IsColumnSelectedProperty, value);
        }
        /// <summary>
        ///     The DependencyProperty that represents the IsReadOnly property.
        /// </summary>
        public static readonly DependencyProperty IsColumnSelectedProperty =
            DependencyProperty.Register("IsColumnSelected", typeof(bool), typeof(EditorDataGridTemplateColumnBase), new FrameworkPropertyMetadata(false, OnNotifyCellPropertyChanged));

        public Action<Param> PermissionChangedCallback { get; private set; }

        public Func<Param,bool> Check { get; set; }

        #region Methods

        /// <summary>
        /// 当前列的权限发生变化时执行的回调函数。
        /// </summary>
        /// <remarks>
        /// 该回调函数中，设置当前列下所有配方参数的访问权限。
        /// </remarks>
        private void UpdatePermission()
        {
            switch (Permission)
            {
                default:
                case MenuPermissionEnum.MP_NONE:
                    PermissionChangedCallback = (p) =>
                    {
                        p.ColumnPermission = Permission;
                    };
                    break;

                case MenuPermissionEnum.MP_READ:
                    PermissionChangedCallback = (p) =>
                    {
                        p.ColumnPermission = Permission;
                    };
                    break;

                case MenuPermissionEnum.MP_READ_WRITE:

                    PermissionChangedCallback = (p) =>
                    {
                        p.ColumnPermission = Permission;
                    };
                    break;
            }

        }

        /// <summary>
        /// 根据权限更新当前列的可见状态。
        /// </summary>
        public void UpdateVisibility()
        {
            if (!IsExpander)
            {
                Visibility = _permission == MenuPermissionEnum.MP_NONE
                    ? Visibility.Collapsed
                    : Visibility.Visible;
            }
        }

        #endregion
    }
}
