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.Configuration;
using Microsoft.Extensions.Logging;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models;

namespace TEAMModelFunction
{
    public class MonitorCosmosDB
    {
        private readonly AzureCosmosFactory _azureCosmos;
        private readonly AzureServiceBusFactory _serviceBus;
        private readonly AzureStorageFactory _azureStorage;
        private readonly DingDing _dingDing;
        private readonly AzureRedisFactory _azureRedis;
       // private   IConfiguration _configuration { get; set; }
        public MonitorCosmosDB( AzureCosmosFactory azureCosmos, AzureServiceBusFactory azureServiceBus, AzureStorageFactory azureStorage, DingDing dingDing, AzureRedisFactory azureRedis
           // , IConfiguration configuration
            )
        {
            _azureCosmos = azureCosmos;
            _serviceBus = azureServiceBus;
            _azureStorage = azureStorage;
            _dingDing = dingDing;
            _azureRedis = azureRedis;
          //  _configuration = configuration;
        }

        [FunctionName("Common")]
        public async Task Common([CosmosDBTrigger(
            databaseName: "TEAMModelOS",
            collectionName: "Common",
            ConnectionStringSetting = "Azure:Cosmos:ConnectionString",
            LeaseCollectionName = "leases",
            LeaseCollectionPrefix = "TEAMModelOS")]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))
                    {
                        if (pk.Equals("Receiver", StringComparison.OrdinalIgnoreCase))
                        { 
                            ///通知接收者的变更
                            return;
                        }
                        else {
                            ///活动类型的变更
                            int ttl = input.GetPropertyValue<int>("ttl");
                            long stime = input.GetPropertyValue<long>("startTime");
                            long etime = input.GetPropertyValue<long>("endTime");
                            string school = input.GetPropertyValue<string>("school");
                            string code = input.GetPropertyValue<string>("code");
                            string creatorId = input.GetPropertyValue<string>("creatorId");
                            string progress = input.GetPropertyValue<string>("progress");
                            string scope = input.GetPropertyValue<string>("scope");
                            string name = input.GetPropertyValue<string>("name");
                            int? status = input.GetPropertyValue<int?>("status");
                            var data = new TriggerData
                            {
                                stime = stime,
                                etime = etime,
                                school = school,
                                code = code,
                                creatorId = creatorId,
                                progress = progress,
                                scope = scope,
                                ttl = ttl,
                                id = input.Id,
                                status = status
                            };
#if DEBUG
                            await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDBTrigger,{pk}触发变更\n{data.ToJsonString()}",
                                            GroupNames.成都开发測試群組);
#endif
                            switch (pk)
                            {
                                case "Exam":
                                    TriggerExam.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school);
                                    break;
                                case "Vote":
                                    TriggerVote.Trigger(_serviceBus, _azureStorage, _dingDing, client, input, data, _azureRedis);
                                    break;
                                case "Survey":
                                    TriggerSurvey.Trigger(_serviceBus, _azureStorage, _dingDing, client, input, data, _azureRedis);
                                    break;
                                case "Correct":
                                    TriggerCorrect.Trigger(_serviceBus, _azureStorage, _dingDing, client, input, data, _azureRedis);
                                    break;

                            }

                        }
                        
                    }
                }
            }
        }
    }
    public class TriggerData {
        public int? status { get; set; }
        public string name { get; set; }
        public long stime { get; set; }
        public long etime { get; set; }
        public string school { get; set; }
        public string code { get; set; }
        public string creatorId { get; set; }
        public string progress { get; set; }
        public string scope { get; set; }
        public int ttl { get; set; }
        public string id { get; set; }
    }
}
