using CloudBuilder;
using CloudBuilder.Core.Utility;
using CloudBuilder.Gui;
using CloudBuilder.Gui.DependencyInjection;
using Krypton.Toolkit;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Krypton.Toolkit
{
    public static class KryptonComboBoxExtensions
    {
        public static ExFormSnapshotEntry GetExFormSnapshotEntry(this KryptonComboBox kryptonComboBox)
        {
            string bindDataName = null;

            if (GetProvider().GetCustomAttribute(kryptonComboBox) != null)
                bindDataName = GetProvider().GetCustomAttribute(kryptonComboBox).BindDataName;

            return new ExFormSnapshotEntry(
                kryptonComboBox.Name,
                bindDataName,
                kryptonComboBox.SelectedValue,
                kryptonComboBox.Text);
        }

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

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

        public static void Set(this KryptonComboBox kryptonComboBox,
    EnumEditModeColor editModeColor, string bindDisplayName)
        {
            GetProvider().SetCustomAttribute(kryptonComboBox, editModeColor, bindDisplayName);
        }

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

        public static void Set(this KryptonComboBox kryptonComboBox, string bindDataName, string bindDisplayName, EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(kryptonComboBox, bindDataName, bindDisplayName, editModeColor);
        }

        public static void Set(this KryptonComboBox kryptonComboBox, string bindDataName, string bindFormat, string bindDisplayName, EnumEditModeColor editModeColor)
        {
            GetProvider().SetCustomAttribute(kryptonComboBox, bindDataName, bindFormat, bindDisplayName, editModeColor);
        }

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

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


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

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

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

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

        public static void Set(this KryptonComboBox kryptonComboBox, string permissionName, string dependentDetailName)
        {
            GetProvider().SetCustomAttribute(kryptonComboBox, permissionName, dependentDetailName);
        }
        public static object GetValue(this KryptonComboBox kryptonComboBox)
        {
            if (kryptonComboBox.SelectedIndex < 0) return null;
            if (kryptonComboBox.SelectedValue == DBNull.Value) return null;
            if (kryptonComboBox.SelectedValue is DataRowView) return null;
            return kryptonComboBox.SelectedValue;
        }

        public static void SetValue(this KryptonComboBox kryptonComboBox, object value)
        {
            if (value == null) kryptonComboBox.SelectedIndex = -1;
            else
                kryptonComboBox.SelectedValue = value;

        }


        //private const string DATA_TABLE_ROW_GUID_ID = "DATA_TABLE_ROW_GUID_ID";

        public static void SetDataSource<T>(this KryptonComboBox kryptonComboBox, List<T> ds, string displayMember, string valueMember, bool blankOption)
        {
            kryptonComboBox.DataSource = null;
            DataTable dataTableSource = new DataTable();
            //DataTable deleteDataTableSource = new DataTable();

            if (ds == null)
            {
                return;
            }

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

            kryptonComboBox.DataSource = dataTableSource;

            kryptonComboBox.DisplayMember = displayMember;
            kryptonComboBox.ValueMember = valueMember;

            dataTableSource.AcceptChanges();

            //deleteDataTableSource = dataTableSource.Clone();
        }

        public static void Clear(this KryptonComboBox kryptonComboBox)
        {
            kryptonComboBox.DataSource = null;
        }

        public static void SetDataSource<T>(this KryptonComboBox kryptonComboBox, T[] ds, string displayMember, string valueMember, bool blankOption)
        {
            kryptonComboBox.DataSource = null;
            DataTable dataTableSource = new DataTable();
            //DataTable deleteDataTableSource = new DataTable();

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

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

            dataTableSource.AcceptChanges();

            //deleteDataTableSource = dataTableSource.Clone();
        }

        public static void SetDataSource(this KryptonComboBox kryptonComboBox, TableName<string>[] values, bool blankOption)
        {
            kryptonComboBox.SetDataSource<TableName<string>>(values, TableName<string>.DISPLAY_NAME, TableName<string>.VALUE, blankOption);
        }

        public static BindingContextCustomAttribute GetCustomAttribute(this KryptonComboBox kryptonComboBox)
        {
            return GetProvider().GetCustomAttribute(kryptonComboBox);
        }

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