using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using Sockeye.Models;
using Sockeye.Api.ControllerHelpers;
using Sockeye.Biz;
using Newtonsoft.Json.Linq;

namespace Sockeye.Api.Controllers
{
    [ApiController]
    [ApiVersion("8.0")]
    [Route("api/v{version:apiVersion}/import")]
    [Produces("application/json")]
    [Authorize]
    public class ImportController : ControllerBase
    {
        private readonly AyContext ct;
        private readonly ILogger<ImportController> log;
        private readonly ApiServerState serverState;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="dbcontext"></param>
        /// <param name="logger"></param>
        /// <param name="apiServerState"></param>  
        public ImportController(AyContext dbcontext, ILogger<ImportController> logger, ApiServerState apiServerState)
        {
            ct = dbcontext;
            log = logger;
            serverState = apiServerState;
        }



        /// <summary>
        /// Import / Update JSON data to indicated object type
        /// </summary>
        /// <param name="importData"></param>
        /// <param name="apiVersion">From route path</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostImportData([FromBody] AyImportData importData, ApiVersion apiVersion)
        {
            if (!serverState.IsOpen)
                return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));

            if (!Authorized.HasCreateRole(HttpContext.Items, importData.SockType))
                return StatusCode(403, new ApiNotAuthorizedResponse());
            if (!ModelState.IsValid)
                return BadRequest(new ApiErrorResponse(ModelState));

            List<string> ImportResult = new List<string>();
            ImportResult.Add("Import results\n");
            log.LogDebug($"Instantiating biz object handler for {importData.SockType}");
            var biz = BizObjectFactory.GetBizObject(importData.SockType, ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items));

            if (!(biz is IImportAbleObject))
                return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, $"Import not supported for {importData.SockType} objects"));
            ImportResult.AddRange(await ((IImportAbleObject)biz).ImportData(importData));

            return Ok(ApiOkResponse.Response(ImportResult));

        }



        // /// <summary>
        // /// Upload and import file
        // /// Max 100MiB total
        // /// </summary>
        // /// <returns>Results</returns>
        // [Authorize]
        // [HttpPost("upload")]
        // [DisableFormValueModelBinding]
        // [RequestSizeLimit(Sockeye.Util.ServerBootConfig.MAX_IMPORT_FILE_UPLOAD_BYTES)]
        // public async Task<IActionResult> UploadAsync()
        // {
        //     //Adapted from the example found here: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads#uploading-large-files-with-streaming

        //     if (!serverState.IsOpen)
        //         return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));

        //     //This route is ONLY available to users with full rights to Global object
        //     if (!Authorized.HasModifyRole(HttpContext.Items, SockType.Global))
        //     {
        //         return StatusCode(403, new ApiNotAuthorizedResponse());
        //     }

        //     // SockTypeId attachToObject = null;
        //     ApiUploadProcessor.ApiUploadedFilesResult uploadFormData = null;
        //     List<string> ImportResult = new List<string>();
        //     ImportResult.Add("Import results\n");
        //     try
        //     {
        //         if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
        //             return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, $"Expected a multipart request, but got {Request.ContentType}"));

        //         //Save uploads to disk under temporary file names until we decide how to handle them
        //         // uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext);xx


        //         string UploadAType = string.Empty;

        //         string errorMessage = string.Empty;
        //         string Notes = string.Empty;
        //         List<UploadFileData> FileData = new List<UploadFileData>();

        //         //Save uploads to disk under temporary file names until we decide how to handle them
        //         uploadFormData = await ApiUploadProcessor.ProcessUploadAsync(HttpContext);
        //         if (!string.IsNullOrWhiteSpace(uploadFormData.Error))
        //         {
        //             errorMessage = uploadFormData.Error;
        //             //delete temp files
        //             ApiUploadProcessor.DeleteTempUploadFile(uploadFormData);
        //             //file too large is most likely issue so in that case return this localized properly
        //             if (errorMessage.Contains("413"))
        //             {
        //                 var TransId = UserTranslationIdFromContext.Id(HttpContext.Items);
        //                 return BadRequest(new ApiErrorResponse(
        //                     ApiErrorCode.VALIDATION_LENGTH_EXCEEDED,
        //                     null,
        //                     String.Format(await TranslationBiz.GetTranslationStaticAsync("AyaFileFileTooLarge", TransId, ct), Sockeye.Util.FileUtil.GetBytesReadable(Sockeye.Util.ServerBootConfig.MAX_IMPORT_FILE_UPLOAD_BYTES))));
        //             }
        //             else//not too big, something else                    
        //                 return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, null, errorMessage));
        //         }

        //         if (!uploadFormData.FormFieldData.ContainsKey("FileData"))//only filedata is required                
        //             return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, "Missing required FormFieldData value: FileData"));


        //         if (uploadFormData.FormFieldData.ContainsKey("SockType"))
        //             UploadAType = uploadFormData.FormFieldData["SockType"].ToString();
        //         else
        //             return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, "Missing required FormFieldData value: SockType"));

        //         //fileData in JSON stringify format which contains the actual last modified dates etc
        //         //"[{\"name\":\"Client.csv\",\"lastModified\":1582822079618},{\"name\":\"wmi4fu06nrs41.jpg\",\"lastModified\":1586900220990}]"
        //         FileData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<UploadFileData>>(uploadFormData.FormFieldData["FileData"].ToString());



        //         //Instantiate the business object handler
        //         SockType TheType = System.Enum.Parse<SockType>(UploadAType, true);
        //         log.LogDebug($"Instantiating biz object handler for {TheType}");
        //         var biz = BizObjectFactory.GetBizObject(TheType, ct, UserIdFromContext.Id(HttpContext.Items), UserRolesFromContext.Roles(HttpContext.Items));

        //         if (!(biz is IImportAbleObject))
        //             return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, $"Import not supported for {TheType} objects"));

        //         //We have our files now can parse and insert into db
        //         if (uploadFormData.UploadedFiles.Count > 0)
        //         {
        //             //deserialize each file and import
        //             foreach (UploadedFileInfo a in uploadFormData.UploadedFiles)
        //             {
        //                 JArray ja = JArray.Parse(System.IO.File.ReadAllText(a.InitialUploadedPathName));
        //                 ImportResult.AddRange(await ((IImportAbleObject)biz).ImportData(ja));
        //             }
        //         }
        //     }
        //     catch (System.IO.InvalidDataException ex)
        //     {
        //         return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, ex.Message));
        //     }
        //     finally
        //     {
        //         //delete all the files temporarily uploaded and return bad request

        //         ApiUploadProcessor.DeleteTempUploadFile(uploadFormData);
        //     }

        //     return Ok(ApiOkResponse.Response(ImportResult));
        // }





        //-----------------------------------------





    }//eoc
}//eons