using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EVCB_OCPP.WEBAPI;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;

namespace EVCB_OCPP.WEBAPI.Controllers
{
    [ApiExplorerSettings(IgnoreApi = true)]
    public class FileController : ControllerBase
    {
        private readonly IWebHostEnvironment webHostEnvironment;

        public FileController(IWebHostEnvironment webHostEnvironment)
        {
            this.webHostEnvironment = webHostEnvironment;
        }

        [HttpPut]
        [HttpPost]
        public async Task<IActionResult> Upload()
        {

            var ctx = HttpContext;// HttpContextHelper.Current;
            //var root = ctx.Server.MapPath("~/UploadFiles/Diagnostics");
            string root = string.Format($"{webHostEnvironment.ContentRootPath}/UploadFiles/Diagnostics");
            //var provider = new MultipartFileStreamProvider(root);

            try
            {
                if (!Request.HasFormContentType)
                //if (!Request.Content.IsMimeMultipartContent())
                {
                    //throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                    return StatusCode(StatusCodes.Status415UnsupportedMediaType);
                }

                //await Request.Content.ReadAsMultipartAsync(provider);

                foreach (var file in Request.Form.Files)
                {
                    if (file.Headers.ContentType.Contains("application/zip"))
                    {
                        var name = file.FileName;
                        name = name.Trim('"');

                        //var localFileName = file.LocalFileName;
                        var filePath = Path.Combine(root, name);
                        if (!System.IO.File.Exists(filePath))
                        {
                            //System.IO.File.Move(localFileName, filePath);
                            using (var fstream = System.IO.File.Open(filePath, FileMode.CreateNew))
                            {
                                await file.CopyToAsync(fstream);
                            }
                        }
                        else
                        {
                            //return Request.CreateResponse(HttpStatusCode.BadRequest);
                            return StatusCode(StatusCodes.Status400BadRequest);
                        }
                    }
                    else
                    {
                        //return Request.CreateResponse(HttpStatusCode.Forbidden);
                        return StatusCode(StatusCodes.Status403Forbidden);
                    }
                }

                //foreach (var file in provider.FileData)
                //{
                //    if (file.Headers.ContentType.MediaType == "application/zip")
                //    {
                //        var name = file.Headers.ContentDisposition.FileName;
                //        name = name.Trim('"');

                //        var localFileName = file.LocalFileName;
                //        var filePath = Path.Combine(root, name);
                //        if (!System.IO.File.Exists(filePath))
                //        {
                //            System.IO.File.Move(localFileName, filePath);
                //        }
                //        else
                //        {
                //            //return Request.CreateResponse(HttpStatusCode.BadRequest);
                //            return StatusCode(StatusCodes.Status400BadRequest);
                //        }
                //    }
                //    else
                //    {
                //        //return Request.CreateResponse(HttpStatusCode.Forbidden);
                //        return StatusCode(StatusCodes.Status403Forbidden);
                //    }
                //}
                //return Request.CreateResponse(HttpStatusCode.OK);
                return StatusCode(StatusCodes.Status200OK);
            }
            catch (Exception ex)
            {
                //return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.ToString());
                return StatusCode(StatusCodes.Status500InternalServerError, ex.ToString());
            }


        }
    }
}
