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


using CompatBot;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Schema;
using Microsoft.Teams.Apps.BotBuilder;
using Microsoft.Teams.Core;

// using Microsoft.Bot.Connector.Authentication;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.AddTeamsBotFrameworkHttpAdapter();

//builder.Services.AddSingleton<BotFrameworkAuthentication, ConfigurationBotFrameworkAuthentication>();
//builder.Services.AddSingleton<IBotFrameworkHttpAdapter>(provider => 
//    new CloudAdapter(
//        provider.GetRequiredService<BotFrameworkAuthentication>(),
//        provider.GetRequiredService<ILogger<CloudAdapter>>()));


MemoryStorage storage = new();
ConversationState conversationState = new(storage);
builder.Services.AddSingleton(conversationState);
builder.Services.AddTransient<IBot, EchoBot>();

WebApplication app = builder.Build();

TeamsBotFrameworkHttpAdapter compatAdapter = (TeamsBotFrameworkHttpAdapter)app.Services.GetRequiredService<IBotFrameworkHttpAdapter>();
compatAdapter.Use(new MyCompatMiddleware());
compatAdapter.Use(new MyCompatMiddleware());
compatAdapter.OnTurnError = async (turnContext, exception) =>
{
    await turnContext.SendActivityAsync(MessageFactory.Text("Oops, something went wrong! Please try again later."));
};

app.MapPost("/api/messages", async (IBotFrameworkHttpAdapter adapter, IBot bot, HttpRequest request, HttpResponse response, CancellationToken ct) =>
    await adapter.ProcessAsync(request, response, bot, ct)).RequireAuthorization();

app.MapGet("/api/notify/{cid}", async (IBotFrameworkHttpAdapter adapter, string cid, CancellationToken ct) =>
{
    Activity proactive = new()
    {
        Conversation = new() { Id = cid },
        ServiceUrl = "https://smba.trafficmanager.net/teams"
    };
    await ((BotAdapter)adapter).ContinueConversationAsync(
        string.Empty,
        proactive.GetConversationReference(),
        async (turnContext, ct) =>
        {
            await turnContext.SendActivityAsync(
                MessageFactory.Text($"Proactive.  <br/> SDK `{BotApplication.Version}` at {DateTime.Now:T}"), ct);
        },
        ct);
});

app.Run();
