using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Muscularity.Core.Shared;

namespace Muscularity.Web.Infrastructure.ExceptionHandling
{
    /// <summary>
    /// An exception filter that will respond with a 404 'Not Found' if the 
    /// Exception is a ResourceNotFoundException.
    /// </summary>
    public class ResourceNotFoundExceptionFilter : ExceptionFilterBase
    {
        public ResourceNotFoundExceptionFilter(
            IWebHostEnvironment hostingEnvironment,
            ILogger<ResourceNotFoundExceptionFilter> logger)
            : base(hostingEnvironment, logger)
        {
        }

        protected override bool CanHandle(ExceptionContext context)
        {
            return IsInstanceOf<ResourceNotFoundException>(context.Exception);
        }

        protected override void PopulateProblemDetails(ValidationProblemDetails model, ExceptionContext context)
        {
            model.Status = 404;
            model.Title = "Not Found";
        }
    }
}

