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 KryptonRadioButtonExtensions
    {
        public static ExFormSnapshotEntry GetExFormSnapshotEntry(this KryptonRadioButton kryptonRadioButton)
        {
            string bindDataName = null;

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

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

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

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

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

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

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


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

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

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

        public static bool GetValue(this KryptonRadioButton kryptonRadioButton)
        {
            return kryptonRadioButton.Checked;
        }


        public static void SetValue(this KryptonRadioButton kryptonRadioButton, object value)
        {
            if (value == null)
            {
                kryptonRadioButton.Checked = false;
            }
            else if (value is bool)
            {
                kryptonRadioButton.Checked = (bool)value;
            }
            else if (value is string)
            {
                kryptonRadioButton.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)
            {
                kryptonRadioButton.Checked = Convert.ToDecimal(value) > 0;
            }
        }

        public static BindingContextCustomAttribute GetCustomAttribute(this KryptonRadioButton kryptonRadioButton)
        {
            return GetProvider().GetCustomAttribute(kryptonRadioButton);
        }

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


