using Azure.Cosmos;
using Microsoft.Azure.WebJobs;
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.Cosmos.Common;

namespace TEAMModelFunction
{
    public class CourseServiceBus
    {
        private readonly AzureCosmosFactory _azureCosmos;
        private readonly DingDing _dingDing;
        public CourseServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing)
        {
            _azureCosmos = azureCosmos;
            _dingDing = dingDing;
        }

        /// <summary>
        /// 完善课程变更
        /// </summary>
        /// <data msg>
        /// CourseChange
        ///// </data>
        /// <param name="msg"></param>
        /// <returns></returns>
        [FunctionName("Course")]
        public async Task Course([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "course", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
        {
            var client = _azureCosmos.GetCosmosClient();
            try
            {
                await _dingDing.SendBotMsg($"ServiceBus,CourseChange:{msg}", GroupNames.醍摩豆服務運維群組);
                var jsonMsg = JsonDocument.Parse(msg);
                CourseChange courseChange = msg.ToObject<CourseChange>();
                if (courseChange == null) {
                    return;
                }
                //根据新增名单获取 新增的学生id 及timdid
                (List<string> addTmdids, List<Students> addStudents) = await TriggerStuActivity.GetStuList(client, _dingDing, courseChange.addList, courseChange.school);
                //根据删除名单获取 新增的学生id 及timdid
                (List<string> delTmdids, List<Students> delStudents) = await TriggerStuActivity.GetStuList(client, _dingDing, courseChange.delList, courseChange.school);
                foreach (var addStu in addStudents)
                {
                    var course = new StuCourse
                    {
                        id = courseChange.id,
                        scode = courseChange.code,
                        name = courseChange.name,
                        code = $"StuCourse-{courseChange.school}-{addStu.id}",
                        scope = courseChange.scope,
                        school = courseChange.school,
                        creatorId = courseChange.creatorId,
                        pk = "StuCourse"
                    };
                    await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(course, new PartitionKey(course.code));
                }
                foreach (var addTmd in addTmdids)
                {
                    var course = new StuCourse
                    {
                        id = courseChange.id,
                        scode = courseChange.code,
                        name = courseChange.name,
                        code = $"StuCourse-{addTmd}",
                        scope = courseChange.scope,
                        //school = courseChange.school,
                        creatorId = courseChange.creatorId,
                        pk = "StuCourse"
                    };
                    await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(course, new PartitionKey(course.code));
                }
                foreach (var delStu in delStudents)
                {
                    await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{courseChange.school}-{delStu.id}"));
                }
                foreach (var delTmd in delTmdids)
                {
                    await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{delTmd}"));
                }
            }
            catch (Exception ex)
            {
                await _dingDing.SendBotMsg($"CourseServiceBus-Course\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
            }
        }
    }
}
