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

namespace TEAMModelFunction
{
    public static class TriggerVote
    {

        public static async void Trigger( AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
            CosmosClient client, Document input, TriggerData tdata, AzureRedisFactory _azureRedis)
        {
            if ((tdata.status != null && tdata.status.Value == 404)||tdata.ttl>0)
            {
                return;
            }
            var adid = tdata.id;
            var adcode = "";
            string blobcntr = null;
            if (tdata.scope == "school")
            {
                adcode = $"Activity-{tdata.school}";
                blobcntr = tdata.school;
            }
            else if (tdata.scope == "private")
            {
                adcode = $"Activity-{tdata.creatorId}";
                blobcntr = tdata.creatorId;
            }
            await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
            Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
            List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
            if (vote != null) {
                switch (vote.progress)
                {
                    case "pending":
                        var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
                        messageVote.ApplicationProperties.Add("name", "Vote");
                        if (voteRecords.Count > 0)
                        {
                            long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
                            await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
                            voteRecords[0].sequenceNumber = start;
                            await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
                        }
                        else
                        {
                            long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
                            ChangeRecord changeRecord = new ChangeRecord
                            {
                                RowKey = input.Id,
                                PartitionKey = "pending",
                                sequenceNumber = start,
                                msgId = messageVote.MessageId
                            };
                            await _azureStorage.Save<ChangeRecord>(changeRecord);
                        }
                        break;
                    case "going":
                        
                        (List<string> tmdids, List<Students> students) = await TriggerStuActivity.GetStuList(client, _dingDing, vote.classes, vote.school);
                        List<string> tmds = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string>() ;
                        if (tmdids.IsNotEmpty()) {
                            tmds.AddRange(tmdids);
                        }
                        List<StuActivity> stuActivities = new List<StuActivity>();
                        List<StuActivity> tmdActivities = new List<StuActivity>();
                        if (tmds.IsNotEmpty())
                        {
                            tmds.ForEach(x => {
                                tmdActivities.Add(new StuActivity
                                {
                                    pk = "Activity",
                                    id = vote.id,
                                    code = $"Activity-{x}",
                                    type = "Vote",
                                    name = vote.name,
                                    startTime = vote.startTime,
                                    endTime = vote.endTime,
                                    scode = vote.code,
                                    scope = vote.scope,
                                    school = vote.school,
                                    creatorId = vote.creatorId,
                                    subjects = new List<string> { "" } ,
                                    blob = null,
                                    owner=vote.owner
                                });
                            });
                        }
                        if (students.IsNotEmpty())
                        {
                            students.ForEach(x => {
                                stuActivities.Add(new StuActivity
                                {
                                    pk = "Activity",
                                    id = vote.id,
                                    code = $"Activity-{vote.school}-{x.id}",
                                    type = "Vote",
                                    name = vote.name,
                                    startTime = vote.startTime,
                                    endTime = vote.endTime,
                                    scode = vote.code,
                                    scope = vote.scope,
                                    school = vote.school,
                                    creatorId = vote.creatorId,
                                    subjects = new List<string> { "" },
                                    blob=null,
                                    owner = vote.owner
                                });
                            });
                        }
                        await TriggerStuActivity.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities);
                        //向学生或醍摩豆账号发起通知
                        #region
                        Notice notice = new Notice()
                        {
                           
                            creation = vote.startTime,
                            expire = vote.endTime,
                            creatorId = vote.creatorId,
                            stuids = students,
                            tmdids = tmdids,
                            type = "notice",//问卷参加参加通知
                            priority = "normal",
                            msgId=vote.id,
                            school = vote.school,
                            scope = vote.scope,
                            //data = new { }.ToJsonString()
                            body = new Body { sid = vote.id, scode = vote.code, spk = vote.pk,  biztype = "vote-join" }

                        };

                        //var messageBlob = new ServiceBusMessage(notice.ToJsonString());
                        //messageBlob.ApplicationProperties.Add("name", "Notice");
                        //await _serviceBus.GetServiceBusClient().SendMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:NoticeTask"), messageBlob);
                        #endregion
                        var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
                        messageVoteEnd.ApplicationProperties.Add("name", "Vote");
                        if (voteRecords.Count > 0)
                        {
                            long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
                            await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
                            voteRecords[0].sequenceNumber = end;
                            await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
                        }
                        else
                        {
                            long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
                            ChangeRecord changeRecord = new ChangeRecord
                            {
                                RowKey = input.Id,
                                PartitionKey = "going",
                                sequenceNumber = end,
                                msgId = messageVoteEnd.MessageId
                            };
                            await _azureStorage.Save<ChangeRecord>(changeRecord);
                        }
#if DEBUG
                        await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动{tdata.id}将于:{tdata.etime}完成并结算！", GroupNames.成都开发測試群組);
#endif
                        break;
                    case "finish":
#if DEBUG
                        await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动{tdata.id}开始结算{tdata.etime}！", GroupNames.成都开发測試群組);
#endif
                        //获取投票活动的所有投票记录
                        var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}");
                        //获取投票活动的选项及投票数
                        var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
                        List<dynamic> countcds = new List<dynamic>();
                        if (counts != null && counts.Length > 0)
                        {
                            foreach (var count in counts)
                            {
                                countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
                            }
                        }
                        List<Task<string>> tasks = new List<Task<string>>();
                        List<VoteRecord> recordsBlob = new List<VoteRecord>();
                        foreach (var rcd in records)
                        {
                            var value = rcd.Value.ToString().ToObject<VoteRecord>();
                            recordsBlob.Add(value);
                        }
                        //分组每个人的 
                        var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() });
                        foreach (var g in gp)
                        {
                            tasks.Add(_azureStorage.UploadFileByContainer(blobcntr, g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json"));
                        }
                        //处理活动方的记录, 
                        string url = $"/vote/{vote.id}/record.json";
                        tasks.Add(_azureStorage.UploadFileByContainer(blobcntr, new { options = countcds, records = recordsBlob }.ToJsonString(), "vote", $"{vote.id}/record.json"));
                        //处理投票者的记录

                        if (string.IsNullOrEmpty(vote.recordUrl))
                        {
                            vote.recordUrl = url;
                            await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
                        }
                        else {
                            //异动，且已经有结算记录则不必再继续。
                            _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}");
                            _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}");
                            break; 
                        }
                        await Task.WhenAll(tasks);
                        break;
                }
            }
           
        }
    }
}
