using CloudBuilder;
using CloudBuilder.Gui;
using CloudBuilder.Gui.DependencyInjection;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Krypton.Toolkit
{
    public static class KryptonListBoxExtensions
    {

        public static ExFormSnapshotEntry GetExFormSnapshotEntry(this KryptonListBox kryptonListBox)
        {
            string bindDataName = null;

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

            return new ExFormSnapshotEntry(
                kryptonListBox.Name,
                bindDataName,
                kryptonListBox.GetAllItemValue<string>(),
                kryptonListBox.Text);
        }

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

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

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

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

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


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

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

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

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

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

        public static string[] GetAllItemText(this KryptonListBox kryptonListBox)
        {
            List<string> l = new List<string>();
            for (int i = 0; i < kryptonListBox.Items.Count; i++)
            {
                l.Add(((KryptonListItem)kryptonListBox.Items[i]).ShortText);
            }
            return l.ToArray();
        }


        public static T[] GetAllItemValue<T>(this KryptonListBox kryptonListBox)
        {
            List<T> l = new List<T>();
            for (int i = 0; i < kryptonListBox.Items.Count; i++)
            {
                l.Add((T)((Dictionary<int, object>)((KryptonListItem)kryptonListBox.Items[i]).Tag)[i]);
            }
            return l.ToArray();
        }

        public static void SetValue(this KryptonListBox kryptonListBox, string[] ts)
        {
            kryptonListBox.Items.Clear();

            if (ts == null || ts.Length == 0) return;

            List<KryptonListItem> l = new List<KryptonListItem>();
            KryptonListItem kryptonListItem;

            int i = 0;
            Dictionary<int, object> keyValues = new Dictionary<int, object>();
            foreach (string item in ts)
            {
                kryptonListItem = new KryptonListItem();
                
                kryptonListItem.ShortText = item;
                l.Add(kryptonListItem);

                keyValues.Add(i, item);
                kryptonListItem.Tag = keyValues;
                i++;
            }

            kryptonListBox.Items.AddRange(l.ToArray());
        }


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

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


