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;

namespace Krypton.Toolkit
{
    public static class KryptonCheckBoxExtensions
    {

        public static ExFormSnapshotEntry GetExFormSnapshotEntry(this KryptonCheckBox kryptonCheckBox)
        {
            string bindDataName = null;

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

            return new ExFormSnapshotEntry(
                kryptonCheckBox.Name,
                bindDataName,
                kryptonCheckBox.Checked,
                kryptonCheckBox.Text);
        }

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

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

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

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

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


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

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

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

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

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

        public static bool GetValue(this KryptonCheckBox kryptonCheckBox)
        {
            return kryptonCheckBox.Checked;
        }
        

        public static void SetValue(this KryptonCheckBox kryptonCheckBox, object value)
        {
            if (value is bool)
            {
                kryptonCheckBox.Checked = (bool)value;
            }
            else if (value is string)
            {
                kryptonCheckBox.Checked = value.ToString().ToUpper() == "Y" ? true : false;
            }
            else if (value is decimal || value is int || value is Int32 || value is Int64 || value is Int16 || value is double || value is float)
            {
                kryptonCheckBox.Checked = Convert.ToDecimal(value) > 0;
            }
        }
        public static BindingContextCustomAttribute GetCustomAttribute(this KryptonCheckBox kryptonCheckBox)
        {
            return GetProvider().GetCustomAttribute(kryptonCheckBox);
        }

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


