using Swashbuckle.Swagger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Description;

namespace LHDN.CRM.WebAPI.Filters
{
    public class EnableSwaggerFilter : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            foreach (var apiDescription in apiExplorer.ApiDescriptions)
            {
                if (!apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<EnableSwaggerAttribute>().Any())
                {
                    var route = apiDescription.RelativePath.Split('?')[0];
                    swaggerDoc.paths.Remove("/" + route);
                }
            }
        }
    }
}