using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
//using System.Runtime.Caching;
using Microsoft.Extensions.Caching.Memory;
using PhotoTkutimes.Models;
namespace PhotoTkutimes.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AdController : ControllerBase
    {
        public MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
        public readonly IConfiguration _configuration;
        public AdController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public MemoryStream DownloadFromFileStorage( string fn)
        {
            MemoryStream ms = new MemoryStream();
            try
            {
                string connectionString = _configuration.GetValue<string>("ShareString");
                string shareName = _configuration.GetValue<string>("ShareName");
                ShareClient share = new ShareClient(connectionString, shareName);
                ShareDirectoryClient directory = share.GetRootDirectoryClient();
                directory = directory.GetSubdirectoryClient("san_tkutimes");                
                directory = directory.GetSubdirectoryClient("Ad");
                ShareFileClient file = directory.GetFileClient(fn);
                ShareFileDownloadInfo download = file.Download();
                download.Content.CopyTo(ms);
            }
            catch (Exception e)
            {
                e.ToString();
            }
            return ms;
        }
        [HttpGet]
        public string Show()
        {
            return "hello";
        }
        [HttpGet("{fn}")]
        public IActionResult Get(string fn)
        {

            string filename;
            MemoryStream ms = new MemoryStream();
            Models.Security security = new Models.Security();
            char[] splitter_securityIDs = { '/' };
            try
            {
                filename = fn;
                filename = Path.GetFileName(filename);
            }
            catch (Exception e)
            {
                filename = e.ToString();
            }

            if (string.IsNullOrEmpty(filename))
            {
                return NotFound();
            }
            try
            {

                if (!memoryCache.TryGetValue(fn, out ms))
                {
                    var memoryStream = DownloadFromFileStorage(filename);
                    using (var bitmap = Bitmap.FromStream(memoryStream))
                    {
                        ms = new MemoryStream();
                        using (var tempBitmap = new Bitmap(bitmap.Width, bitmap.Height))
                        {
                            using (Graphics grp = Graphics.FromImage(tempBitmap))
                            {

                                grp.DrawImage(bitmap, 0, 0);
                                bitmap.Dispose();
                                tempBitmap.Save(ms, ImageFormat.Jpeg);
                                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                    .SetSlidingExpiration(TimeSpan.FromHours(1))
                                    .SetSize(2048);
                                memoryCache.Set(fn, ms, cacheEntryOptions);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
            ms.Seek(0, SeekOrigin.Begin);

            // 回傳檔案到 Client 需要附上 Content Type，否則瀏覽器會解析失敗。
            return new FileStreamResult(ms, "image/jpeg");
        }
    }
}
