// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Teams.Api.Activities;
using Microsoft.Teams.Api.Activities.Invokes;
using Microsoft.Teams.Apps.Routing;

namespace Microsoft.Teams.Apps.Activities.Invokes;

[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class TokenExchangeAttribute() : InvokeAttribute(Api.Activities.Invokes.Name.SignIn.TokenExchange, typeof(SignIn.TokenExchangeActivity))
{
    public override object Coerce(IContext<IActivity> context) => context.ToActivityType<SignIn.TokenExchangeActivity>();
}

public static partial class AppInvokeActivityExtensions
{
    [Obsolete("Use the handler with the cancellation token")]
    public static App OnTokenExchange(this App app, Func<IContext<SignIn.TokenExchangeActivity>, Task> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.SignIn.TokenExchange]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context =>
            {
                await handler(context.ToActivityType<SignIn.TokenExchangeActivity>()).ConfigureAwait(false);
                return null;
            },
            Selector = activity => activity is SignIn.TokenExchangeActivity
        });

        return app;
    }

    [Obsolete("Use the handler with the cancellation token")]
    public static App OnTokenExchange(this App app, Func<IContext<SignIn.TokenExchangeActivity>, Task<Response<Api.TokenExchange.InvokeResponse>>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.SignIn.TokenExchange]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SignIn.TokenExchangeActivity>()).ConfigureAwait(false),
            Selector = activity => activity is SignIn.TokenExchangeActivity
        });

        return app;
    }

    [Obsolete("Use the handler with the cancellation token")]
    public static App OnTokenExchange(this App app, Func<IContext<SignIn.TokenExchangeActivity>, Task<Api.TokenExchange.InvokeResponse>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.SignIn.TokenExchange]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SignIn.TokenExchangeActivity>()).ConfigureAwait(false),
            Selector = activity => activity is SignIn.TokenExchangeActivity
        });

        return app;
    }

    [Obsolete("Use the handler with the cancellation token")]
    public static App OnTokenExchange(this App app, Func<IContext<SignIn.TokenExchangeActivity>, Task<Response>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.SignIn.TokenExchange]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SignIn.TokenExchangeActivity>()).ConfigureAwait(false),
            Selector = activity => activity is SignIn.TokenExchangeActivity
        });

        return app;
    }

    public static App OnTokenExchange(this App app, Func<IContext<SignIn.TokenExchangeActivity>, CancellationToken, Task> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.SignIn.TokenExchange]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context =>
            {
                await handler(context.ToActivityType<SignIn.TokenExchangeActivity>(), context.CancellationToken).ConfigureAwait(false);
                return null;
            },
            Selector = activity => activity is SignIn.TokenExchangeActivity
        });

        return app;
    }

    public static App OnTokenExchange(this App app, Func<IContext<SignIn.TokenExchangeActivity>, CancellationToken, Task<Response<Api.TokenExchange.InvokeResponse>>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.SignIn.TokenExchange]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SignIn.TokenExchangeActivity>(), context.CancellationToken).ConfigureAwait(false),
            Selector = activity => activity is SignIn.TokenExchangeActivity
        });

        return app;
    }

    public static App OnTokenExchange(this App app, Func<IContext<SignIn.TokenExchangeActivity>, CancellationToken, Task<Api.TokenExchange.InvokeResponse>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.SignIn.TokenExchange]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SignIn.TokenExchangeActivity>(), context.CancellationToken).ConfigureAwait(false),
            Selector = activity => activity is SignIn.TokenExchangeActivity
        });

        return app;
    }

    public static App OnTokenExchange(this App app, Func<IContext<SignIn.TokenExchangeActivity>, CancellationToken, Task<Response>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.SignIn.TokenExchange]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SignIn.TokenExchangeActivity>(), context.CancellationToken).ConfigureAwait(false),
            Selector = activity => activity is SignIn.TokenExchangeActivity
        });

        return app;
    }
}