using System.Text.RegularExpressions;

namespace Pilz.Plugins.Advanced.UI;

internal class ControlListener(IEnumerable<ControlListenerFeature> features, Control control, bool recursive) : IControlListener
{
    protected bool isTracking;

    public virtual bool Recursive => recursive;

    public virtual Control Control => control;

    public virtual IEnumerable<ControlListenerFeature> Features => features;

    internal virtual void Track()
    {
        if (isTracking)
            return;
        Track(control);
        isTracking = true;
    }

    protected virtual void Track(Control control)
    {
        control.Disposed += Control_Disposed;
        control.ControlAdded += Control_ControlAdded;
        control.ControlRemoved += Control_ControlRemoved;

        if (!recursive)
            return;

        foreach (var child in control.Controls)
        {
            if (child is Control childControl)
                Track(childControl);
        }
    }

    protected virtual void Control_Disposed(object? sender, EventArgs e)
    {
        if (sender is not Control control)
            return;

        Execute(control, ControlExecuteReason.Dispose, null, false);

        control.Disposed -= Control_Disposed;
        control.ControlAdded -= Control_ControlAdded;
        control.ControlRemoved -= Control_ControlRemoved;
    }

    protected virtual void Control_ControlAdded(object? sender, ControlEventArgs e)
    {
        if (sender is not Control control)
            return;

        if (e.Control is not null)
            Track(e.Control);

        Execute(control, ControlExecuteReason.Add, new ControlParameters(e.Control), false);
    }

    protected virtual void Control_ControlRemoved(object? sender, ControlEventArgs e)
    {
        if (sender is not Control control)
            return;

        Execute(control, ControlExecuteReason.Remove, new ControlParameters(e.Control), false);
    }

    protected internal virtual void Initialize(PluginFunctionParameter? parameters)
    {
        Execute(ControlExecuteReason.Init, parameters);
    }

    public virtual void Execute(PluginFunctionParameter? parameters)
    {
        Execute(ControlExecuteReason.None, parameters);
    }

    protected virtual void Execute(ControlExecuteReason reason, PluginFunctionParameter? parameter)
    {
        Execute(control, reason, parameter, recursive);
    }

    protected virtual void Execute(Control control, ControlExecuteReason reason, PluginFunctionParameter? parameter, bool recursive)
    {
        execute(control);
        void execute(Control control)
        {
            if (control.Name is string name)
            {
                foreach (var feature in features)
                {
                    if (feature.ControlName is null || feature.ControlNameRegEx && Regex.IsMatch(control.Name, feature.ControlName) || feature.ControlName == control.Name)
                        feature.Execute(control, reason, parameter);
                }
            }

            if (!recursive)
                return;

            foreach (var child in control.Controls)
            {
                if (child is Control childControl)
                    execute(childControl);
            }
        }
    }
}
