using AipGateway.API.Application.Modules;
using AipGateway.API.Domain.IRepositories.IGenericRepositories;
using AipGateway.API.Domain.Models;
using AipGateway.API.Domain.Models.Request;
using AipGateway.API.Extensions;
using AipGateway.API.Service.Models;
using AipGateway.API.Services;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace AipGateway.API.Controllers
{
    [ApiController]
    [Route("v1/aip-api/dummy")]
    [Produces("application/json")]
    public class ApiDummyController : BaseModule
    {
        private readonly ILogger<ApiDummyController> _log;
        private readonly IApiDummyService _apiDummyService;
        public ApiDummyController(
            ILogger<ApiDummyController> logger, 
            IUnitOfWork unitOfWork, 
            IWebHostEnvironment webHostEnvironment,
            IApiDummyService apiDummyService)
            : base(unitOfWork, webHostEnvironment)
        {
            _log = logger;
            _apiDummyService = apiDummyService;
            _log.LogError("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo: ApiDummyController");
        }


        /// <remarks>
        /// Sample request:
        ///
        ///     GET /linked-systems
        ///     {
        ///        "id": 1,
        ///        "name": "Item #1",
        ///        "isComplete": true
        ///     }
        ///
        /// </remarks>
        [HttpGet("")]
        [SwaggerResponse(200, type: typeof(SuccessResponseModel<ApiResponseDto>))]
        public async Task<IResult> GetDummy()
        {
            //GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_DUMMY, apiKey);
            return await CreateResponseAsync(async () =>
            {
                var response = new ApiResponseDto
                {
                   effectCount = 0,
                   errorCode = 0,
                   errorMessage = GlobalConstants.API_RESULT_SUCCESS,
                };
                var result = Results.Ok(new SuccessResponseModel<ApiResponseDto>()
                {
                    Message = GlobalConstants.API_RESULT_SUCCESS,
                    Result = response,
                    StatusCode = System.Net.HttpStatusCode.OK,
                    Success = true
                });
                HttpContext.Items[GlobalConstants.API_RESULT] = result;
                return result;
            });
        }

        [HttpPost("file-data")]
        public FileData GetFileData([FromQuery] RequestFile file)
        {
            FileData result = new FileData();
            try
            {
                //Console.WriteLine("0.::::::::::::::::::::::::::::::::::::::::::::::::::::::: {0}", file.name);
                if (System.IO.File.Exists(file.realFileName))
                {
                    using (FileStream fs = System.IO.File.OpenRead(file.realFileName))
                    {
                        using (var reader = new BinaryReader(fs))
                        {
                            result.byteDataArr = reader.ReadBytes((int)fs.Length);
                            result.base64Data = Convert.ToBase64String(result.byteDataArr);
                            //result.streamData = new MemoryStream();
                            //result.streamData.Write(result.byteDataArr, 0, result.byteDataArr.Length);
                        }

                        var bytes = Convert.FromBase64String(result.base64Data);
                        var contents = new StreamContent(new MemoryStream(bytes));
                        //Console.WriteLine("{0}", contents);
                        var stream = new MemoryStream(Convert.FromBase64String(result.base64Data));

                        //if (file.name.Contains(".txt"))
                        //{
                        //    System.IO.File.WriteAllBytes(@"c:\data\sample.txt.txt", Convert.FromBase64String(result.base64Data));
                        //}
                        //else
                        //{
                        //    System.IO.File.WriteAllBytes(@"c:\data\sample.txt.pptx", Convert.FromBase64String(result.base64Data));
                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine("{0}", ex.Message);
                _log.LogError(ex, "AipFileController::GetFileData.");
            }
            return result;
        }

    }
}
