using CloudBuilder;
using CloudBuilder.Gui;
using Microsoft.AspNetCore.Authorization;

namespace Krypton.Toolkit
{
    public static class KryptonButtonExtensions
    {
        public static void Set(this KryptonButton kryptonButton,
               string permissionName,
               Operate permissionMode)
        {
            GetProvider().SetCustomAttribute(kryptonButton, permissionName, permissionMode);
        }

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

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

        public static BindingContextCustomAttribute GetCustomAttribute(this KryptonButton kryptonButton)
        {
            return GetProvider().GetCustomAttribute(kryptonButton);
        }

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