namespace Pilz.Plugins.Advanced.UI.Extensions;

public static class PluginFeatureControllerExtensions
{
    public static IControlListener ApplyControlManager(this PluginFeatureController @this, string featureType, Control control)
    {
        return ApplyControlManager(@this, featureType, control, true, true);
    }

    public static IControlListener ApplyControlManager(this PluginFeatureController @this, string featureType, Control control, PluginFunctionParameter? parameter)
    {
        return ApplyControlManager(@this, featureType, control, true, true, parameter);
    }

    public static IControlListener ApplyControlManager(this PluginFeatureController @this, string featureType, Control control, bool recursive, bool autoTrack)
    {
        return ApplyControlManager(@this, featureType, control, recursive, autoTrack, null);
    }

    public static IControlListener ApplyControlManager(this PluginFeatureController @this, string featureType, Control control, bool recursive, bool autoTrack, PluginFunctionParameter? parameter)
    {
        var features = @this.Features.Get(featureType).OfType<ControlListenerFeature>();

        if (!features.Any())
            return new ControlListenerDummy([], control, recursive);

        var manager = new ControlListener(features, control, recursive);

        if (autoTrack)
            manager.Track();

        manager.Initialize(parameter);

        return manager;
    }
}
