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
{
    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>());
                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>, 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>()),
            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>, 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>()),
            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);
                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),
            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),
            Selector = activity =>
            {
                if (activity is SearchActivity search)
                {
                    return search.Value.Kind.IsTypeahead;
                }

                return false;
            }
        });

        return app;
    }
}