using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TEAMModelOS.Service.Models.Core;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;

namespace TEAMModelOS.Controllers.Syllabus
{
    [Route("api/[controller]")]
    [ApiController]
    // [Authorize]
    public class StudentController : BaseController
    {
        private readonly IAzureCosmosDBRepository azureCosmosDBRepository;
        public StudentController(IAzureCosmosDBRepository _azureCosmosDBRepository)
        {
            azureCosmosDBRepository = _azureCosmosDBRepository;
        }

        [HttpPost("SaveStudent")]
        public async Task<BaseJosnRPCResponse> SaveStudentInfo(JosnRPCRequest<Student> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            Student data = await azureCosmosDBRepository.Save<Student>(request.@params);
            return builder.Data(data).build();
        }


        [HttpPost("FindStudent")]
        public async Task<BaseJosnRPCResponse> FindStudent(JosnRPCRequest<Dictionary<string, object>> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();

            if (request.@params.TryGetValue("SchoolCode", out object SchoolCode))
            {
                List<Student> data = await azureCosmosDBRepository.FindByParams<Student>(request.@params);
                return builder.Data(data).build();
            }
            else
            {
                return builder.Error("SchoolCode  is null !").build();
            }
        }
        [HttpPost("SaveAllStudent")]
        public async Task<BaseJosnRPCResponse> SaveAllStudentInfo(JosnRPCRequest<List<Student>> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            Dictionary<string, List<Student>> dictInfo = new Dictionary<string, List<Student>>();
            foreach (IGrouping<string, Student> group in request.@params.GroupBy(c => c.classroomCode))
            {
                dictInfo.Add(group.Key, group.ToList());
            }
            List<Student> studentInfos = new List<Student>();
            foreach (string key in dictInfo.Keys)
            {
                List<Classroom> classroom = await azureCosmosDBRepository.FindByParams<Classroom>(new Dictionary<string, object> { { "ClassroomCode", key } });
                if (classroom.IsNotEmpty())
                {
                    dictInfo.TryGetValue(key, out List<Student> sts);
                    sts.ForEach(x => {
                        x.gradeCode = classroom[0].gradeCode;
                        x.periodCode = classroom[0].periodCode;
                        x.schoolCode = classroom[0].schoolCode;
                        x.id = x.studentId;
                    });
                    List<Student> data = await azureCosmosDBRepository.SaveAll<Student>(sts);
                    studentInfos.AddRange(data);
                }
            }
            return builder.Data(studentInfos).build();
        }

        [HttpPost("UpdateStudent")]
        public async Task<BaseJosnRPCResponse> UpdateStudentInfo(JosnRPCRequest<Student> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            Student data = await azureCosmosDBRepository.Update<Student>(request.@params);
            return builder.Data(data).build();
        }
        [HttpPost("UpdateAllStudent")]
        public async Task<BaseJosnRPCResponse> DeleteALlStudentInfo(JosnRPCRequest<Dictionary<string, Dictionary<string, object>>> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            bool find = request.@params.TryGetValue("find", out Dictionary<string, object> findObj);
            bool update = request.@params.TryGetValue("update", out Dictionary<string, object> updateObj);
            List<Student> data = null;
            if (find && update)
            {
                data = await azureCosmosDBRepository.UpdateAll<Student>(findObj, updateObj);
            }
            return builder.Data(data).build();
        }
        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("DeleteStudent")]
        public async Task<BaseJosnRPCResponse> DeleteStudentInfo(JosnRPCRequest<Student> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            string data = await azureCosmosDBRepository.DeleteAsync<Student>(request.@params.id, request.@params.schoolCode);
            return builder.Data(data).build();
        }
    }
}
