namespace MyOffice.Web.Infrastructure;

using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

public static class RouteHelperExtensions
{
    public static void Bind<T>(this IRouteBuilder routeBuilder, Expression<Func<T, ObjectResult>> expression)
    {
        System.Console.WriteLine("Bind");
        System.Console.WriteLine(expression.Name);
        System.Console.WriteLine(expression.Body.ToString());
        routeBuilder.MapRoute("SettingsAccountAccountsGet", "api/settings/accounts", new { controller = "SettingsAccount", action = "AccountsGet" });
    }
}

public class CustomRouter : IRouter
{
    private readonly IRouter _defaultRouter;
    private readonly string _controller;
    private readonly string _action;

    public CustomRouter(IRouter defaultRouter, string controller, string action)
    {
        _defaultRouter = defaultRouter;
        _controller = controller;
        _action = action;
    }

    public VirtualPathData? GetVirtualPath(VirtualPathContext context)
    {
        Console.WriteLine($"1:{context.RouteName}");
        return null;
    }

    public async Task RouteAsync(RouteContext context)
    {
        var headers = context.HttpContext.Request.Headers;
        var path = context.HttpContext.Request.Path.Value!.Split('/');

        Console.WriteLine($"CustomRouter:{context.HttpContext.Request.Path.Value}");

        context.RouteData.Values["controller"] = _controller;
        context.RouteData.Values["action"] = _action;

        await _defaultRouter.RouteAsync(context);
    }
}