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 static  class VoteTrigger
    {

        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)
        {
            Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
            List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
            //ChangeRecord voteRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{vote.progress}"));
            switch (vote.progress)
            {
                case "pending":
                    var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
                    messageVote.ApplicationProperties.Add("name", "Vote");
                    if (voteRecords.Count > 0)
                    {
                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
                        await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
                        voteRecords[0].sequenceNumber = start;
                        await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
                        //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
                    }
                    else
                    {
                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
                        ChangeRecord changeRecord = new ChangeRecord
                        {
                            RowKey = input.Id,
                            PartitionKey = "pending",
                            sequenceNumber = start,
                            msgId = messageVote.MessageId
                        };
                        await _azureStorage.Save<ChangeRecord>(changeRecord);
                        //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
                    }
                    break;
                case "going":
                    var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
                    messageVoteEnd.ApplicationProperties.Add("name", "Vote");
                    if (voteRecords.Count > 0)
                    {
                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                        await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
                        voteRecords[0].sequenceNumber = end;
                        await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
                        //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
                    }
                    else
                    {
                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
                        ChangeRecord changeRecord = new ChangeRecord
                        {
                            RowKey = input.Id,
                            PartitionKey = "going",
                            sequenceNumber = end,
                            msgId = messageVoteEnd.MessageId
                        };
                        await _azureStorage.Save<ChangeRecord>(changeRecord);
                        //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
                    }
                    break;
            }
        }
    }
}
