using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;

namespace Hc900Crawler.Web;

/// <summary>
/// 지정된 컨트롤러 타입 이름을 제외하는 Feature Provider.
/// P&ID 컨트롤러를 조건부로 비활성화하는 데 사용.
/// </summary>
internal sealed class ExcludedControllersFeatureProvider : IApplicationFeatureProvider<ControllerFeature>
{
    private readonly IApplicationFeatureProvider<ControllerFeature> _inner;
    private readonly HashSet<string> _excludedNames;

    public ExcludedControllersFeatureProvider(
        IApplicationFeatureProvider<ControllerFeature> inner,
        IEnumerable<string> excludedNames)
    {
        _inner = inner;
        _excludedNames = new HashSet<string>(excludedNames);
    }

    public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
    {
        _inner.PopulateFeature(parts, feature);
        var excluded = feature.Controllers.Where(t => _excludedNames.Contains(t.Name)).ToList();
        foreach (var t in excluded)
        {
            feature.Controllers.Remove(t);
        }
    }
}
