using CloudBuilder.Core.Utility;
using Krypton.Toolkit;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CloudBuilder.Gui
{
    public static partial class DataGridViewColumnExtensions
    {
        public static void Set(this DataGridViewColumn dataGridViewColumn,
            BindingContextCustomAttribute bindingContext)
        {
            if (bindingContext != null && !string.IsNullOrEmpty(bindingContext.BindDataName)) dataGridViewColumn.DataPropertyName = bindingContext.BindDataName;
            GetProvider().SetCustomAttribute(dataGridViewColumn, bindingContext);
        }

        public static void Set(this DataGridViewColumn dataGridViewColumn,
            EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(dataGridViewColumn, editModeColor);
        }

        public static void Set(this DataGridViewColumn dataGridViewColumn,
           string bindDataName,
           EnumEditModeColor editModeColor)
        {
            if (!string.IsNullOrEmpty(bindDataName)) dataGridViewColumn.DataPropertyName = bindDataName;
            GetProvider().SetCustomAttribute(dataGridViewColumn, bindDataName, editModeColor);
        }

        public static void Set(this DataGridViewColumn dataGridViewColumn,
            string bindDataName)
        {
            if (!string.IsNullOrEmpty(bindDataName)) dataGridViewColumn.DataPropertyName = bindDataName;
            //GetProvider().SetCustomAttribute(dataGridViewColumn, bindDataName, EnumBindDirection.Both);
        }

        public static void Set(this DataGridViewColumn dataGridViewColumn,
            string bindDataName,
            EnumBindDirection bindDirection)
        {
            if (!string.IsNullOrEmpty(bindDataName)) dataGridViewColumn.DataPropertyName = bindDataName;
            GetProvider().SetCustomAttribute(dataGridViewColumn, bindDataName, bindDirection);
        }


        public static void Set(this DataGridViewColumn dataGridViewColumn,
            string bindDataName,
            EnumBindDirection bindDirection,
            EnumEditModeColor editModeColor)
        {
            if (!string.IsNullOrEmpty(bindDataName)) dataGridViewColumn.DataPropertyName = bindDataName;
            GetProvider().SetCustomAttribute(dataGridViewColumn, bindDataName, bindDirection, editModeColor);
        }

        public static void Set(this DataGridViewColumn dataGridViewColumn,
            string bindDataName,
            string bindFormat,
            EnumBindDirection bindDirection)
        {
            if (!string.IsNullOrEmpty(bindDataName)) dataGridViewColumn.DataPropertyName = bindDataName;
            GetProvider().SetCustomAttribute(dataGridViewColumn, bindDataName, bindFormat, bindDirection);
        }

        public static void Set(this DataGridViewColumn dataGridViewColumn,
           string permissionName,
           Operate permissionMode)
        {
            GetProvider().SetCustomAttribute(dataGridViewColumn, permissionName, permissionMode);
        }

        public static void Set(this DataGridViewColumn dataGridViewColumn, string permissionName, string dependentDetailName, Operate permissionMode)
        {
            GetProvider().SetCustomAttribute(dataGridViewColumn, permissionName, dependentDetailName, permissionMode);
        }

        public static void Set(this DataGridViewColumn dataGridViewColumn, string permissionName, string dependentDetailName)
        {
            GetProvider().SetCustomAttribute(dataGridViewColumn, permissionName, dependentDetailName);
        }

        public static BindingContextCustomAttribute GetCustomAttribute(this DataGridViewColumn dataGridViewColumn)
        {
            return GetProvider().GetCustomAttribute(dataGridViewColumn);
        }

        private static BindingContextAttributeProvider GetProvider()
        {
            return ServiceHelper.GetService<BindingContextAttributeProvider>() ??
        throw new InvalidOperationException("BindingContextAttributeProvider is not registered in the service GetProvider().");
        }
    }
}
