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.Service.Models.Syllabus;
using TEAMModelOS.SDK.Context.Exception;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
using TEAMModelOS.SDK.Extension.SnowFlake;
using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
using TEAMModelOS.SDK.Helper.Security.ShaHash;
using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;
using TEAMModelOS.Controllers;

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

        /// <summary>
        /// 新增册别
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("SaveOrUpdate")]
        public async Task<BaseJosnRPCResponse> SaveOrUpdate(JosnRPCRequest<Volume> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            if (request.@params.editors != null && request.@params.editors.Length > 5)
            {
                throw new BizException("共编人数大于5人！");
            }
            if (string.IsNullOrEmpty(request.@params.id))
            {
                if (request.@params.type == 0)
                {
                    string key = ShaHashHelper.GetSHA1(request.@params.schoolCode + request.@params.periodCode + request.@params.subjectCode + request.@params.gradeCode + request.@params.semesterCode);
                    request.@params.id = key;
                    request.@params.volumeCode = key;
                }
                else
                {
                    string key = ShaHashHelper.GetSHA1(request.@params.schoolCode + request.@params.TEAMModelId + IdWorker.getInstance().NextId());
                    request.@params.id = key;
                    request.@params.volumeCode = key;
                }
            }
            List<Volume> volumes = new List<Volume>();
            volumes.Add(request.@params);
            List<Volume> volume = await azureCosmosDBRepository.SaveAll<Volume>(volumes);
            return builder.Data(volume.IsEmpty() ? null : volume[0]).build();
        }
        /// <summary>
        /// 删除册别
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("Delete")]
        public async Task<BaseJosnRPCResponse> Delete(JosnRPCRequest<Volume> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            bool flag = false;
            if (string.IsNullOrEmpty(request.@params.id))
            {
                if (request.@params.type == 0)
                {
                    request.@params.status = 0;
                    List<Volume> volumes = new List<Volume>();
                    volumes.Add(request.@params);
                    List<Volume> volume = await azureCosmosDBRepository.SaveAll<Volume>(volumes);
                    flag = true;
                }
                else
                {
                    await azureCosmosDBRepository.DeleteAsync<Volume>(request.@params.id, request.@params.schoolCode);
                    List<SyllabusNode> syllabusNodes = await azureCosmosDBRepository.DeleteAll<SyllabusNode>(new Dictionary<string, object>() { { "volumeCode", request.@params.id } });
                    flag = true;
                }
            }
            return builder.Data(flag).build();
        }
        /// <summary>
        /// 查找册别
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("Find")]
        public async Task<BaseJosnRPCResponse> Find(JosnRPCRequest<Dictionary<string, object>> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            List<Volume> volumes = null;
            if (request.@params.TryGetValue("schoolCode", out _))
            {
                volumes = await azureCosmosDBRepository.FindByParams<Volume>(request.@params);
            }
            else
            {
                throw new BizException("schoolCode is  null!");
            }
            return builder.Data(volumes).build();
        }
    }
}
