using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.Cosmos;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models;

namespace TEAMModelFunction
{
    public class MonitorCosmosDB
    {
        private readonly IHttpClientFactory _clientFactory;
        private readonly AzureCosmosFactory _azureCosmos;
        private readonly AzureServiceBusFactory _serviceBus;
        private readonly AzureStorageFactory _azureStorage;
        private readonly DingDing _dingDing;

        public MonitorCosmosDB(IHttpClientFactory clientFactory, AzureCosmosFactory azureCosmos, AzureServiceBusFactory azureServiceBus, AzureStorageFactory azureStorage, DingDing dingDing)
        {
            _clientFactory = clientFactory;
            _azureCosmos = azureCosmos;
            _serviceBus = azureServiceBus;
            _azureStorage = azureStorage;
            _dingDing = dingDing;
        }

        [FunctionName("Common")]
        public async Task Common([CosmosDBTrigger(
            databaseName: "TEAMModelOS",
            collectionName: "Common",
            ConnectionStringSetting = "Azure:Cosmos:ConnectionString",
            LeaseCollectionName = "leases")]IReadOnlyList<Document> inputs, ILogger log)
        {
            if (inputs != null && inputs.Count > 0)
            {
                log.LogInformation("Documents modified " + inputs.Count);
                log.LogInformation("First document Id " + inputs[0].Id);
                var client = _azureCosmos.GetCosmosClient();

                foreach (var input in inputs)
                {
                    string pk = input.GetPropertyValue<string>("pk");
                    if (!string.IsNullOrWhiteSpace(pk))
                    {
                        long stime = input.GetPropertyValue<long>("startTime");
                        long etime = input.GetPropertyValue<long>("endTime");
                        string school = input.GetPropertyValue<string>("school");
                        string code = input.GetPropertyValue<string>("code");
                        await _dingDing.SendBotMsg($"CosmosDBTrigger,{pk}\n" +
                                        $"Start Time：{DateTimeOffset.FromUnixTimeMilliseconds(stime).AddHours(8)}\n" +
                                        $"End Time：{DateTimeOffset.FromUnixTimeMilliseconds(etime).AddHours(8)}",
                                        GroupNames.醍摩豆服務運維群組);
                        switch (pk)
                        {
                            case "Exam":
                                ExamTrigger.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client,input,code,stime,etime,school);
                                break;
                            case "Vote":
                                VoteTrigger.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school);
                                break;
                            case "Survey":
                                SurveyTrigger.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school);
                                break;

                        }
                    }
                }
            }
        }
        
    }
}
