using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TEAMModelOS.SDK.Context.Constant.Common;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
using TEAMModelOS.SDK.Helper.Common.JsonHelper;
using TEAMModelOS.SDK.Helper.Common.XmlHelper;
using TEAMModelOS.SDK.Module.AzureBlob.Container;
using TEAMModelOS.SDK.Module.AzureBlob.Interfaces;
using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
using TEAMModelOS.SDK.Module.PowerPointX.Model;

namespace TEAMModelOS.Controllers.Syllabus
{
    [Route("api/[controller]")]
    [ApiController]
    public class FileController:BaseController
    {
        private readonly IAzureTableDBRepository azureTableDBRepository;
        private readonly IAzureBlobDBRepository _azureBlobDBRepository;
        public FileController(IAzureTableDBRepository _azureTableDBRepository, IAzureBlobDBRepository azureBlobDBRepository)
        {
            azureTableDBRepository = _azureTableDBRepository;
            _azureBlobDBRepository = azureBlobDBRepository;
        }


        [HttpPost("uploadFiles")]
        [RequestSizeLimit(102_400_000_00)] //最大10000m左右
        public async Task<BaseJosnRPCResponse> BlobSaveFile([FromForm] IFormFile[] files)
        {
            JsonRPCResponseBuilder responseBuilder = new JsonRPCResponseBuilder();
            FileTypeMap fileTypeMap = new FileTypeMap();
            fileTypeMap.GetFileTypes().TryGetValue(FileType.GetExtention(files[0].FileName).ToLower(), out FileType fileTpye);
            string type = "";
            if (fileTpye != null)
            {
                type = fileTpye.Type;
            }

            List<AzureBlobModel> list = await _azureBlobDBRepository.UploadFiles(files);
            await azureTableDBRepository.SaveAll<AzureBlobModel>(list);
            return responseBuilder.Data(list).build();
        }
        [HttpPost("uploadFile")]
        [RequestSizeLimit(102_400_000_00)] //最大10000m左右
        public async Task<BaseJosnRPCResponse> BlobSaveFile([FromForm] IFormFile file)
        {
            //IFormFileCollection s =  Request.Form.Files;
            JsonRPCResponseBuilder responseBuilder = new JsonRPCResponseBuilder();
            FileTypeMap fileTypeMap = new FileTypeMap();
            fileTypeMap.GetFileTypes().TryGetValue(FileType.GetExtention(file.FileName).ToLower(), out FileType fileTpye);
            string type = "";
            if (fileTpye != null)
            {
                type = fileTpye.Type;
            }
            List<IFormFile> files = new List<IFormFile>();
            files.Add(file);
            List<AzureBlobModel> list = await _azureBlobDBRepository.UploadFiles(files.ToArray());
            await azureTableDBRepository.SaveAll<AzureBlobModel>(list);
            return responseBuilder.Data(list).build();
        }
       
        [HttpPost("uploadWangEditor")]
        [RequestSizeLimit(102_400_000_00)] //最大10000m左右
        public async Task<Dictionary<string, Object>> uploadWangEditor([FromForm] IFormFile[] files)
        {
            //IFormFileCollection s =  Request.Form.Files;
            JsonRPCResponseBuilder responseBuilder = new JsonRPCResponseBuilder();
            FileTypeMap fileTypeMap = new FileTypeMap();
            fileTypeMap.GetFileTypes().TryGetValue(FileType.GetExtention(files[0].FileName).ToLower(), out FileType fileTpye);
            string type = "";
            if (fileTpye != null)
            {
                type = fileTpye.Type;
            }
            List<AzureBlobModel> list = await _azureBlobDBRepository.UploadFiles(files.ToArray());
            await azureTableDBRepository.SaveAll<AzureBlobModel>(list);
            return new Dictionary<string, object> { { "errno", 0 }, { "data", list.Select(x => x.BlobUrl) } };
        }
       

 


        /// <summary>
        /// {"Sha1Code":"1111","Length":233444}
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("uploaded")]
        public async Task<BaseJosnRPCResponse> uploaded(JosnRPCRequest<Dictionary<string, object>> request)
        {
            JsonRPCResponseBuilder responseBuilder = new JsonRPCResponseBuilder();
            List<AzureBlobModel> models = await azureTableDBRepository.FindListByDict<AzureBlobModel>(request.@params);
            if (models.IsNotEmpty())
            {
                return responseBuilder.Data(models.First()).build();
            }
            else
            {
                return responseBuilder.Data(null).build();
            }
        }



        /// <summary>
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("saveBlob")]
        public async Task<BaseJosnRPCResponse> saveBlob(JosnRPCRequest<AzureBlobModel> request)
        {
            JsonRPCResponseBuilder responseBuilder = new JsonRPCResponseBuilder();
            AzureBlobModel model = await azureTableDBRepository.SaveOrUpdate<AzureBlobModel>(request.@params);
            if (model != null && !string.IsNullOrEmpty(model.RowKey))
            {
                return responseBuilder.Data(model).build();
            }
            else
            {
                return responseBuilder.Data(null).build();
            }
        }
    }
}
