using Microsoft.Extensions.Hosting;
using System;
using System.IO;
using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Infrastructure.Extention;

namespace IRaCIS.Core.Application
{
    public class DicomFileStoreHelper
    {
        private  readonly IWebHostEnvironment _hostEnvironment;
        private static string _fileStorePath = string.Empty;

        private readonly ILogger<DicomFileStoreHelper> _logger;

        public  DicomFileStoreHelper(IWebHostEnvironment hostEnvironment, ILogger<DicomFileStoreHelper> logger)
        {
            _logger = logger;
            _hostEnvironment = hostEnvironment;
            var rootPath = Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\')).IfNullThrowException().FullName;

            //上传根路径
             _fileStorePath = Path.Combine(rootPath, StaticData.TrialDataFolder);

        }

        public  string GetInstanceFilePath(DicomStudy dicomStudy, Guid seriesId, string instanceId)
        {
            return Path.Combine(_fileStorePath,  dicomStudy.TrialId.ToString(),
                dicomStudy.SiteId.ToString(), dicomStudy.SubjectId.ToString(), dicomStudy.SubjectVisitId.ToString(), StaticData.DicomFolder, dicomStudy.Id.ToString(),  instanceId.ToString() + ".dcm");
        }


        public  string CreateInstanceFilePath(DicomStudy dicomStudy, Guid seriesId, Guid instanceId)
        {

            //加入访视层级  和Data
            var path = Path.Combine(_fileStorePath,  dicomStudy.TrialId.ToString(),
                dicomStudy.SiteId.ToString(), dicomStudy.SubjectId.ToString(), dicomStudy.SubjectVisitId.ToString(), StaticData.DicomFolder, dicomStudy.Id.ToString());

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return Path.Combine(path, instanceId.ToString() + ".dcm");
        }


        public string GetSubjectVisitPath(Guid trialId,Guid siteId,Guid subjectId, Guid subjectVisitId)
        {
            return Path.Combine(_fileStorePath, trialId.ToString(),
                siteId.ToString(), subjectId.ToString(), subjectVisitId.ToString(), StaticData.DicomFolder);
        }

        //public static string CreateInstanceFilePath(DicomStudy dicomStudy, Guid seriesId, Guid instanceId)
        //{
        //    string path = Path.Combine(_fileStorePath, "Dicom", dicomStudy.CreateTime.Year.ToString(), dicomStudy.TrialId.ToString(),
        //        dicomStudy.SiteId.ToString(), dicomStudy.SubjectId.ToString(), dicomStudy.Id.ToString());
        //    if (!Directory.Exists(path)) Directory.CreateDirectory(path);

        //    return Path.Combine(path, instanceId.ToString() + ".dcm");
        //}

        //public static string GetInstanceFilePath(DicomStudy dicomStudy, Guid seriesId, Guid instanceId)
        //{
        //    return Path.Combine(_fileStorePath, "Dicom", dicomStudy.CreateTime.Year.ToString(), dicomStudy.TrialId.ToString(),
        //        dicomStudy.SiteId.ToString(), dicomStudy.SubjectId.ToString(), dicomStudy.Id.ToString(), instanceId.ToString() + ".dcm");
        //}


        //public static void RemoveStudyDirectory(DicomStudy dicomStudy)
        //{
        //    string path = Path.Combine(_fileStorePath, "Dicom", dicomStudy.CreateTime.Year.ToString(), dicomStudy.TrialId.ToString(),
        //        dicomStudy.SiteId.ToString(), dicomStudy.SubjectId.ToString(), dicomStudy.Id.ToString());
        //    if (Directory.Exists(path)) Directory.Delete(path, true);
        //}
    }
}