using AutoMapper;
using IRaCIS.Core.Application.Helper;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure.Extention;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Threading.Tasks;

namespace IRaCIS.Core.API.Controllers
{
    [ApiExplorerSettings(GroupName = "Trial")]
    [ApiController]
    public class CommonController : ControllerBase
    {
        public IMapper _mapper { get; set; }
        public IUserInfo _userInfo { get; set; }

        private readonly IWebHostEnvironment _hostEnvironment;



        public CommonController(IMapper mapper, IUserInfo userInfo, IMediator mediator, IWebHostEnvironment hostEnvironment)
        {
            _hostEnvironment = hostEnvironment;
            _mapper = mapper;
            _userInfo = userInfo;
        }


        [AllowAnonymous]
        [HttpGet("Common/LocalFilePreview")]
        public async Task<FileContentResult> LocalFilePreview(string relativePath)
        {
            var rootPath = Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\')).IfNullThrowException().FullName;

            var _fileStorePath = Path.Combine(rootPath, relativePath.Replace('/', '\\').Trim('\\'));


            var storePreviewPath = _fileStorePath + ".preview.jpeg";

            //if (!System.IO.File.Exists(storePreviewPath))
            //{
                ImageResizeHelper.ResizeSave(_fileStorePath, storePreviewPath);
            //}

            return new FileContentResult(await System.IO.File.ReadAllBytesAsync(storePreviewPath), "image/jpeg");

            //_logger.LogError(rootPath);

            //_logger.LogError(_fileStorePath);


            //if (!File.Exists(storePreviewPath))
            //{

            //Bitmap sourceImage = new Bitmap(File.OpenRead(_fileStorePath));

            //System.Drawing.Image destinationImage = new Bitmap(500, 500);
            //Graphics g = Graphics.FromImage(destinationImage);

            //g.DrawImage(
            //  sourceImage,
            //  new Rectangle(0, 0, 500, 500),
            //  new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
            //  GraphicsUnit.Pixel
            //);

            //destinationImage.Save(storePreviewPath);

            //var image = SKBitmap.Decode(_fileStorePath);
            ////设置图片新的size
            //var newImg = image.Resize(new SKSizeI(50, 50), SKFilterQuality.Medium);
            //using var fs = new FileStream(storePreviewPath, FileMode.Create);
            //newImg.Encode(fs, SKEncodedImageFormat.Png, 100);
            //fs.Flush();


            //var image = NetVips.Image.NewFromFile(_fileStorePath);
            //var newImg = image.Resize(0.5);
            //newImg.WriteToFile(storePreviewPath);


            //var image = NetVips.Image.NewFromFile(_fileStorePath);
            //var newImg = image.ThumbnailImage(500);
            //newImg.WriteToFile(_fileStorePath);

        }

    }
}
