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

namespace CloudBuilder.Gui
{
    public static class DataGridViewComboBoxColumnExtensions
    {
        public static void SetDataSource<T>(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn, T[] ds, string displayMember, string valueMember, bool blankOption)
        {
            dataGridViewComboBoxColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            dataGridViewComboBoxColumn.DataSource = null;
            DataTable dataTableSource = new DataTable();

            if (ds == null)
            {
                return;
            }
            //首先將entity轉為datatable
            dataTableSource = DataTableConvertor.ToDataTable<T>(ds, blankOption);

            dataGridViewComboBoxColumn.DataSource = dataTableSource;
            dataGridViewComboBoxColumn.DisplayMember = displayMember;
            dataGridViewComboBoxColumn.ValueMember = valueMember;

            dataTableSource.AcceptChanges();
            
        }

        public static void SetDataSource(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn, TableName<string>[] values, bool blankOption)
        {
            dataGridViewComboBoxColumn.SetDataSource<TableName<string>>(values, TableName<string>.DISPLAY_NAME, TableName<string>.VALUE, blankOption);
        }
        public static void SetDataSource<T>(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn, List<T> ds, string displayMember, string valueMember, bool blankOption)
        {
            dataGridViewComboBoxColumn.DataSource = null;
            DataTable dataTableSource = new DataTable();
            //DataTable deleteDataTableSource = new DataTable();

            if (ds == null)
            {
                return;
            }

            dataTableSource = DataTableConvertor.ToDataTable<T>(ds, blankOption);

            dataGridViewComboBoxColumn.DataSource = dataTableSource;

            dataGridViewComboBoxColumn.DisplayMember = displayMember;
            dataGridViewComboBoxColumn.ValueMember = valueMember;

            dataTableSource.AcceptChanges();

            //deleteDataTableSource = dataTableSource.Clone();
        }


        public static void Set(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn,
            BindingContextCustomAttribute bindingContext)
        {
            GetProvider().SetCustomAttribute(dataGridViewComboBoxColumn, bindingContext);
        }

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

        public static void Set(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn,
           string bindDataName,
           EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(dataGridViewComboBoxColumn, bindDataName, editModeColor);
        }

        public static void Set(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn,
            string bindDataName)
        {
            GetProvider().SetCustomAttribute(dataGridViewComboBoxColumn, bindDataName, EnumBindDirection.Both);
        }

        public static void Set(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn,
            string bindDataName,
            EnumBindDirection bindDirection)
        {
            GetProvider().SetCustomAttribute(dataGridViewComboBoxColumn, bindDataName, bindDirection);
        }


        public static void Set(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn,
            string bindDataName,
            EnumBindDirection bindDirection,
            EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(dataGridViewComboBoxColumn, bindDataName, bindDirection, editModeColor);
        }

        public static void Set(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn,
            string bindDataName,
            string bindFormat,
            EnumBindDirection bindDirection)
        {
            GetProvider().SetCustomAttribute(dataGridViewComboBoxColumn, bindDataName, bindFormat, bindDirection);
        }

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

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

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


        public static BindingContextCustomAttribute GetCustomAttribute(this DataGridViewComboBoxColumn dataGridViewComboBoxColumn)
        {
            return GetProvider().GetCustomAttribute(dataGridViewComboBoxColumn);
        }

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