using IRaCIS.Core.Domain.Share;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders;
using System.IO;

namespace IRaCIS.Core.API
{
    public static class IRacisHostFileStore
    {

        public static void UseIRacisHostStaticFileStore(this IApplicationBuilder app, IWebHostEnvironment env)
        {

            var uploadPath = Path.Combine(Directory.GetParent(env.ContentRootPath.TrimEnd('\\')).FullName, StaticData.UploadFileFolder);
            var dicomPath = Path.Combine(Directory.GetParent(env.ContentRootPath.TrimEnd('\\')).FullName, StaticData.TrialDataFolder);

            var systemDataFolder = Path.Combine(Directory.GetParent(env.ContentRootPath.TrimEnd('\\')).FullName, StaticData.SystemDataFolder);

         
            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }

            if (!Directory.Exists(dicomPath))
            {
                Directory.CreateDirectory(dicomPath);
            }

            if (!Directory.Exists(systemDataFolder))
            {
                Directory.CreateDirectory(systemDataFolder);
            }
       
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(uploadPath),
                RequestPath = $"/{StaticData.UploadFileFolder}"
            });


            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(dicomPath),
                RequestPath = $"/{StaticData.TrialDataFolder}"
            });

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(systemDataFolder),
                RequestPath = $"/{StaticData.SystemDataFolder}"
            });


            #region 兼容之前的文档能查看

            //var comonPathPath = Path.Combine(Directory.GetParent(env.ContentRootPath.TrimEnd('\\')).FullName, StaticData.CommonFileFolder);

            //var systemNoticePath = Path.Combine(Directory.GetParent(env.ContentRootPath.TrimEnd('\\')).FullName, StaticData.SystemNoticeFolder);

            //if (!Directory.Exists(comonPathPath))
            //{
            //    Directory.CreateDirectory(comonPathPath);
            //}
            //app.UseStaticFiles(new StaticFileOptions
            //{
            //    FileProvider = new PhysicalFileProvider(comonPathPath),
            //    RequestPath = $"/{StaticData.CommonFileFolder}"
            //});

            #endregion




        }
    }
}
