using DiplomovaPrace.Web.Server.Infrastructure.ModelValidation;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace DiplomovaPrace.Web.Server.Infrastructure.ConfigurationExtensions
{
	public static class MvcConfig
	{
		public static void AddCustomizedMvc(this IServiceCollection services, IConfiguration configuration)
		{
			IMvcBuilder mvcBuilder = services
				.AddMvc(options =>
				{
					//options.Conventions.Add(new ApiExplorerGroupPerApiTypeConvention());

					//var defaultPolicy = new AuthorizationPolicyBuilder(AuthenticationConfig.GetAuthenticationSchemes(configuration))
					//	.RequireAuthenticatedUser()
					//	.Build();

					//options.Filters.Add(new AuthorizeFilter(defaultPolicy));
					options.Filters.Add(new ValidateModelAttribute { ResultSelector = ValidationErrorModel.FromModelState() });

					options.EnableEndpointRouting = false;
				})
				.SetCompatibilityVersion(CompatibilityVersion.Latest)

				.AddDataAnnotationsLocalization()
				.AddNewtonsoftJson();
		}

		public static void UseCustomizedMvc(this IApplicationBuilder app)
		{
			app.UseMvc(routes =>
			{
				routes.MapRoute(
					name: "default",
					template: "{controller}/{action=Index}/{id?}");
			});
		}
	}
}
