using CloudBuilder;
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.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Krypton.Toolkit
{
    public static class kryptonCheckedListBoxExtensions
    {

        public static ExFormSnapshotEntry GetExFormSnapshotEntry(this KryptonCheckedListBox kryptonCheckedListBox)
        {
            string bindDataName = null;

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

            return new ExFormSnapshotEntry(
                kryptonCheckedListBox.Name,
                bindDataName,
                kryptonCheckedListBox.GetCheckedItemValue<string>(),
                kryptonCheckedListBox.Text);
        }

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

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

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

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

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


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

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

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

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

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

        public static string[] GetCheckedItemText(this KryptonCheckedListBox kryptonCheckedListBox)
        {
            List<string> l = new List<string>();
            for (int i = 0; i < kryptonCheckedListBox.Items.Count; i++)
            {
                if (kryptonCheckedListBox.GetItemCheckState(i) == CheckState.Checked)
                {
                    l.Add(((KryptonListItem)kryptonCheckedListBox.Items[i]).ShortText);
                }
            }
            return l.ToArray();
        }

        public static string[] GetUnCheckedItemText(this KryptonCheckedListBox kryptonCheckedListBox)
        {
            List<string> l = new List<string>();
            for (int i = 0; i < kryptonCheckedListBox.Items.Count; i++)
            {
                if (kryptonCheckedListBox.GetItemCheckState(i) == CheckState.Unchecked)
                {
                    l.Add(((KryptonListItem)kryptonCheckedListBox.Items[i]).ShortText);
                }
            }
            return l.ToArray();
        }

        public static T[] GetCheckedItemValue<T>(this KryptonCheckedListBox kryptonCheckedListBox)
        {
            List<T> l = new List<T>();
            for (int i = 0; i < kryptonCheckedListBox.Items.Count; i++)
            {
                if (kryptonCheckedListBox.GetItemCheckState(i) == CheckState.Checked)
                {
                    l.Add((T)((Dictionary<int, object>)((KryptonListItem)kryptonCheckedListBox.Items[i]).Tag)[i]);
                }
            }
            return l.ToArray();
        }

        public static T[] GetUnCheckedItemValue<T>(this KryptonCheckedListBox kryptonCheckedListBox)
        {
            List<T> l = new List<T>();
            for (int i = 0; i < kryptonCheckedListBox.Items.Count; i++)
            {
                if (kryptonCheckedListBox.GetItemCheckState(i) == CheckState.Unchecked)
                {
                    l.Add((T)((Dictionary<int, object>)((KryptonListItem)kryptonCheckedListBox.Items[i]).Tag)[i]);
                }
            }
            return l.ToArray();
        }

        public static void SetValue(this KryptonCheckedListBox kryptonCheckedListBox, string[] ts)
        {
            SetCheckedValue(kryptonCheckedListBox, ts);
        }

        public static void Reset(this KryptonCheckedListBox kryptonCheckedListBox)
        {
            for (int i = 0; i < kryptonCheckedListBox.Items.Count; i++)
            {
                kryptonCheckedListBox.SetItemCheckState(i, CheckState.Unchecked);
            }
        }

        public static void SetCheckedValue(this KryptonCheckedListBox kryptonCheckedListBox, object[] ts)
        {

            for (int i = 0; i < kryptonCheckedListBox.Items.Count; i++)
            {
                kryptonCheckedListBox.SetItemCheckState(i, CheckState.Unchecked);
                if (ts == null || ts.Length == 0) continue;

                foreach (object t in ts)
                {
                    if (((Dictionary<int, object>)((KryptonListItem)kryptonCheckedListBox.Items[i]).Tag)[i].ToString() == t.ToString())
                    {
                        kryptonCheckedListBox.SetItemCheckState(i, CheckState.Checked);
                    }
                }
            }
        }

        public static void SetDataSource<T>(this KryptonCheckedListBox kryptonCheckedListBox, T[] dataSource, string displayName, string valueName)
        {
            if (dataSource == null || dataSource.Length == 0) return;
            KryptonListItem[] items = KryptonListItemConvertor.ToKryptonListItem<T>(dataSource, displayName, valueName);
            kryptonCheckedListBox.Items.AddRange(items);
        }

        public static BindingContextCustomAttribute GetCustomAttribute(this KryptonCheckedListBox kryptonCheckedListBox)
        {
            return GetProvider().GetCustomAttribute(kryptonCheckedListBox);
        }

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


