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

namespace TEAMModelFunction
{
   public class SurveyTrigger
    {
        public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
               CosmosClient client, Document input, string code, long stime, long etime, string school)
        {
            Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
            //messageSurvey.ScheduledEnqueueTime = DateTimeOffset.FromUnixTimeMilliseconds(stime);
            //string msgid = messageSurvey.MessageId;
            List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", survey.progress } });
            //ChangeRecord surveyRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{survey.progress}"));
            switch (survey.progress)
            {
                case "pending":
                    var messageSurvey = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
                    messageSurvey.ApplicationProperties.Add("name", "Survey");
                    if (changeRecords.Count > 0)
                    {
                        await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(stime));
                        changeRecords[0].sequenceNumber = start;
                        await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
                        //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(surveyRecord, surveyRecord.id, new Azure.Cosmos.PartitionKey($"{surveyRecord.code}"));
                    }
                    else
                    {
                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(stime));
                        ChangeRecord changeRecord = new ChangeRecord
                        {
                            RowKey = input.Id,
                            PartitionKey = "pending",
                            sequenceNumber = start,
                            msgId = messageSurvey.MessageId
                        };
                        await _azureStorage.Save<ChangeRecord>(changeRecord);
                        //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
                    }
                    break;
                case "going":
                    var messageSurveyEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
                    messageSurveyEnd.ApplicationProperties.Add("name", "Survey");
                    if (changeRecords.Count > 0)
                    {
                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                        await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
                        changeRecords[0].sequenceNumber = end;
                        await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
                    }
                    else
                    {
                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                        ChangeRecord changeRecord = new ChangeRecord
                        {
                            RowKey = input.Id,
                            PartitionKey = "going",
                            sequenceNumber = end,
                            msgId = messageSurveyEnd.MessageId
                        };
                        await _azureStorage.Save<ChangeRecord>(changeRecord);
                        //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
                    }
                    break;
            }
        }
    }
}
