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

namespace Microsoft.Teams.Apps.Activities.Invokes;

public static partial class Search
{
    [AttributeUsage(AttributeTargets.Method, Inherited = true)]
    public class Typeahead() : SearchAttribute
    {
        public override bool Select(IActivity activity)
        {
            if (activity is SearchActivity search)
            {
                return search.Value.Kind.IsTypeahead;
            }

            return false;
        }
    }
}

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

                return false;
            }
        });

        return app;
    }

    [Obsolete("Use the handler with the cancellation token")]
    public static App OnTypeaheadSearch(this App app, Func<IContext<SearchActivity>, Task<Response<SearchResponse>>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.Search, SearchType.Typeahead]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SearchActivity>()).ConfigureAwait(false),
            Selector = activity =>
            {
                if (activity is SearchActivity search)
                {
                    return search.Value.Kind.IsTypeahead;
                }

                return false;
            }
        });

        return app;
    }

    [Obsolete("Use the handler with the cancellation token")]
    public static App OnTypeaheadSearch(this App app, Func<IContext<SearchActivity>, Task<SearchResponse>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.Search, SearchType.Typeahead]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SearchActivity>()).ConfigureAwait(false),
            Selector = activity =>
            {
                if (activity is SearchActivity search)
                {
                    return search.Value.Kind.IsTypeahead;
                }

                return false;
            }
        });

        return app;
    }

    public static App OnTypeaheadSearch(this App app, Func<IContext<SearchActivity>, CancellationToken, Task> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.Search, SearchType.Typeahead]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context =>
            {
                await handler(context.ToActivityType<SearchActivity>(), context.CancellationToken).ConfigureAwait(false);
                return null;
            },
            Selector = activity =>
            {
                if (activity is SearchActivity search)
                {
                    return search.Value.Kind.IsTypeahead;
                }

                return false;
            }
        });

        return app;
    }

    public static App OnTypeaheadSearch(this App app, Func<IContext<SearchActivity>, CancellationToken, Task<Response<SearchResponse>>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.Search, SearchType.Typeahead]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SearchActivity>(), context.CancellationToken).ConfigureAwait(false),
            Selector = activity =>
            {
                if (activity is SearchActivity search)
                {
                    return search.Value.Kind.IsTypeahead;
                }

                return false;
            }
        });

        return app;
    }

    public static App OnTypeaheadSearch(this App app, Func<IContext<SearchActivity>, CancellationToken, Task<SearchResponse>> handler)
    {
        app.Router.Register(new Route()
        {
            Name = string.Join("/", [ActivityType.Invoke, Name.Search, SearchType.Typeahead]),
            Type = app.Status is null ? RouteType.System : RouteType.User,
            Handler = async context => await handler(context.ToActivityType<SearchActivity>(), context.CancellationToken).ConfigureAwait(false),
            Selector = activity =>
            {
                if (activity is SearchActivity search)
                {
                    return search.Value.Kind.IsTypeahead;
                }

                return false;
            }
        });

        return app;
    }
}