using MTWorkHR.Application.Models;
using MTWorkHR.Core.UnitOfWork;
using MTWorkHR.Application.Services.Interfaces;
using MTWorkHR.Core.Entities;
using Microsoft.AspNetCore.Identity;
using MTWorkHR.Application.Mapper;
using MTWorkHR.Core.Global;
using MTWorkHR.Infrastructure.Entities;
using MTWorkHR.Infrastructure.Repositories;
using MTWorkHR.Infrastructure.UnitOfWorks;
using MTWorkHR.Core.IRepositories.Base;
using Microsoft.AspNetCore.Http;
using System.IO;

namespace MTWorkHR.Application.Services
{
    public class ContractTaskAttachmentService : BaseService<ContractTaskAttachment, ContractTaskAttachmentDto, ContractTaskAttachmentDto>, IContractTaskAttachmentService
    {
        private readonly IUnitOfWork _unitOfWork;
        //private readonly AppSettingsConfiguration _configuration;
        //private readonly GlobalInfo _globalInfo;
        private readonly IFileService _fileService;

        public ContractTaskAttachmentService(IUnitOfWork unitOfWork, IFileService fileService) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _fileService = fileService;
        }


        public override async Task<ContractTaskAttachmentDto> GetById(long id)
        {
            var entity = await _unitOfWork.ContractTaskAttachment.GetByIdAsync(id);
            var response = MapperObject.Mapper.Map<ContractTaskAttachmentDto>(entity);
            return response;
        }

        public override async Task<ContractTaskAttachmentDto> Create(ContractTaskAttachmentDto input)
        {
            if (input.FileData != null)
            {
               // input.FilePath = await _fileService.UploadFile(input.FileData);
                input.OriginalName = input.FileData.FileName;
                List<AttachmentDto> attachs = new List<AttachmentDto>();
                attachs.Add(new AttachmentDto {FileData = input.FileData, OriginalName = input.OriginalName, FileName = input.FileData.FileName });
                _fileService.CopyFileToCloud(ref attachs);

            }
            
            var entity = MapperObject.Mapper.Map<ContractTaskAttachment>(input);
            if (entity is null)
            {
                throw new AppException(ExceptionEnum.MapperIssue);
            }

            var task = await _unitOfWork.ContractTaskAttachment.AddAsync(entity);
            await _unitOfWork.CompleteAsync();

            var response = MapperObject.Mapper.Map<ContractTaskAttachmentDto>(task);
            //using (var stream = new MemoryStream(attach.Content))
            //{
            //    var file = new FormFile(stream, 0, stream.Length, Path.GetFileNameWithoutExtension(attach.FileName), attach.FileName)
            //    {
            //        Headers = new HeaderDictionary(),
            //        ContentType = attach.ContentType,
            //    };

            //    System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
            //    {
            //        FileName = file.FileName
            //    };
            //    file.ContentDisposition = cd.ToString();
            //}
            return response;
        }


        //public override async Task<ContractTaskAttachmentDto> Update(ContractTaskAttachmentDto input)
        //{
        //    var entitiy = await _unitOfWork.ContractTaskAttachment.GetByIdAsync(input.Id);

        //    if (entitiy == null)
        //        throw new AppException(ExceptionEnum.RecordNotExist);

        //    MapperObject.Mapper.Map(input, entitiy, typeof(ContractTaskAttachmentDto), typeof(ContractTaskAttachment));

        //    await _unitOfWork.CompleteAsync();

        //    return input;
        //}
    }
}