// 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.Api.AdaptiveCards;
using Microsoft.Teams.Apps.Routing;

namespace Microsoft.Teams.Apps.Activities.Invokes;

public static partial class AdaptiveCard
{
    [AttributeUsage(AttributeTargets.Method, Inherited = true)]
    public class ActionAttribute() : InvokeAttribute(Api.Activities.Invokes.Name.AdaptiveCards.Action, typeof(AdaptiveCards.ActionActivity))
    {
        public override object Coerce(IContext<IActivity> context) => context.ToActivityType<AdaptiveCards.ActionActivity>();
    }
}

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

        return app;
    }

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

        return app;
    }

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

        return app;
    }

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

        return app;
    }

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

        return app;
    }

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

        return app;
    }
}