using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using TEAMModelOS.SDK;

using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
using System;
using TEAMModelOS.SDK.Helper.Common.JsonHelper;
using System.Linq;
using TEAMModelOS.Models;
using System.Text.Json;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models;
using Microsoft.AspNetCore.Http;

namespace TEAMModelOS.Controllers
{
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    //[Authorize(Roles = "IES5")]
    [Route("syllabus")]
    [ApiController]
    //[Authorize]
    public class SyllabusController : ControllerBase
    {
        private readonly AzureCosmosFactory _azureCosmos;


        public SyllabusController(AzureCosmosFactory azureCosmos)
        {
            _azureCosmos = azureCosmos;
        }

        // TODO 代码优化

        /// <summary>
        /// 批量保存或更新课纲
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [ProducesDefaultResponseType]
        [HttpPost("upsert-tree")]
        public async Task<IActionResult> SaveOrUpdateAsTree(List<SyllabusTree> request)
        {
            ////ResponseBuilder builder = ResponseBuilder.custom();
            //List<SyllabusTree> treess = await SaveOrUpdateToTree(request);
            ////return builder.Data(treess).build();
            return Ok();
        }

        // TODO 代码优化

        /// <summary>
        /// 按节点新增课纲
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [ProducesDefaultResponseType]
        [HttpPost("upsert-nodes")]
        public async Task<IActionResult> SaveOrUpdateAsNodes(List<SyllabusNode> request)
        {
            return Ok();
            //List<Syllabus> syllabuses = await SaveOrUpdateToNodes(request);
            //if (syllabuses.IsNotEmpty()) return Ok(request);
            //else return Ok(syllabuses);
        }

        // TODO 代码优化

        /// <summary>
        /// 删除课纲
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [ProducesDefaultResponseType]
        [HttpPost("delete")]
        public async Task<IActionResult> Delete(JsonElement request)
        {
            return Ok();
            //Dictionary<string, object> dict = new Dictionary<string, object>();
            //var emobj = request.EnumerateObject();
            //while (emobj.MoveNext())
            //{
            //    dict[emobj.Current.Name] = emobj.Current.Value;
            //}
            //if (dict.Keys.Count > 0)
            //{
            //    List<Syllabus> syllabuses = await DeleteSyllabus(dict);

            //    if (syllabuses.IsNotEmpty())
            //    {
            //        return Ok(syllabuses);
            //    }
            //    else
            //    {
            //        return Ok("全部删除成功");

            //    }
            //}
            //else
            //{
            //    //return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常！").build();
            //    return Ok(new { ResponseCode.PARAMS_ERROR, V = "参数异常！" });
            //}


        }

        // TODO 代码优化

        /// <summary>
        /// 查找课纲 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [ProducesDefaultResponseType]
        [HttpPost("find")]
        public async Task<IActionResult> Find(JsonElement request)
        {
            return Ok();
            //ResponseBuilder builder = ResponseBuilder.custom();
            //Dictionary<string, object> dict = new Dictionary<string, object>();
            //var emobj = request.EnumerateObject();
            //while (emobj.MoveNext())
            //{
            //    dict[emobj.Current.Name] = emobj.Current.Value;
            //}
            //List<SyllabusTree> treess = await Find(dict);
            ////return builder.Data(treess).build();
            //return Ok(treess);
        }
    }
}
