using CloudBuilder.Core.DatabaseAccessor.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudBuilder.Security.Policy
{
    public class FileTypePolicy
    {
        public const string VIDEO = "V";
        public const string AUDIO = "A";
        public const string PHOTO = "P";
        public const string DOCUMENT = "D";
        public const string LYRIC = "L";

        public static string GetType(string value)
        {
            if (string.IsNullOrEmpty(value)) return DOCUMENT;
            else if (value.ToLower().Contains(".lrc")) return LYRIC;

            else if (value.ToLower().Contains(".jpg") || value.ToLower().Contains(".png") ||
                value.ToLower().Contains(".webp") || value.ToLower().Contains(".bmp") ||
                   value.ToLower().Contains(".gif") || value.ToLower().Contains(".jpeg")) return PHOTO;

            else if (value.ToLower().Contains(".avi") || value.ToLower().Contains(".mpeg") ||
                value.ToLower().Contains(".mov") || value.ToLower().Contains(".wmv") ||
                value.ToLower().Contains(".mp4") || value.ToLower().Contains(".mkv") ||
                value.ToLower().Contains(".flv") || value.ToLower().Contains(".rm")) return VIDEO;

            else if (value.ToLower().Contains(".mp3") || value.ToLower().Contains(".wav") ||
                value.ToLower().Contains(".flac") || value.ToLower().Contains(".ape") ||
                value.ToLower().Contains(".wma") || value.ToLower().Contains(".mid")) return AUDIO;

            return DOCUMENT;
        }


        public static bool IsAudio(string value)
        {
            return GetType(value) == AUDIO;
        }

        public static bool IsPhoto(string value)
        {
            return GetType(value) == PHOTO;
        }

        public static bool IsVideo(string value)
        {
            return GetType(value) == VIDEO;
        }

        public static bool IsLyric(string value)
        {
            return GetType(value) == LYRIC;
        }


        public static string PackUrl(IApplicationService service, string createdBy, string orderKey, string transcodeIndc)
        {
            string url = string.Empty;
            var webRootPath = service.Configuration["FileServiceSettings:RequestPath"];
            var phy = service.Configuration["FileServiceSettings:PhysicalFileDirectory"];

            var fileExtension = Path.GetExtension(orderKey);//获取文件格式，拓展名
            if (GetType(orderKey!) == FileTypePolicy.PHOTO)
            {
                if (transcodeIndc == YesNoPolicy.YES)
                {
                    var backup = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"].Replace(phy, "");
                    var filePath = $"{backup.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
                    url = service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;
                }
                else
                {
                    var scale = service.Configuration["FileServiceSettings:ScaleDmsFileDirectory"].Replace(phy, "");
                    var filePath = $"{scale.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
                    url = service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;
                }
            }
            else if (GetType(orderKey!) == FileTypePolicy.AUDIO)
            {
                var backup = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"].Replace(phy, "");
                var filePath = $"{backup.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
                url = service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;
            }
            else if (GetType(orderKey!) == FileTypePolicy.VIDEO)
            {
                if (transcodeIndc == YesNoPolicy.YES)
                {
                    var upload = service.Configuration["FileServiceSettings:UploadDmsFileDirectory"].Replace(phy, "");
                    var filePath = $"{upload.Replace("\\", "/")}/{createdBy}/m3u8/";
                    url = service.Configuration["applicationUrl"] + webRootPath + filePath + Path.GetFileNameWithoutExtension(orderKey) + "/main.m3u8";
                }
                else
                {
                    var backup = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"].Replace(phy, "");
                    var filePath = $"{backup.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
                    url = service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;
                }
            }

            return url;
        }

        public static string PackSourceVideoUrl(IApplicationService service, string createdBy, string orderKey)
        {
            var webRootPath = service.Configuration["FileServiceSettings:RequestPath"];
            var phy = service.Configuration["FileServiceSettings:PhysicalFileDirectory"];
            var upload = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"].Replace(phy, "");
            var fileExtension = Path.GetExtension(orderKey);//获取文件格式，拓展名
            var filePath = $"{upload.Replace("\\", "/")}/{createdBy}/{fileExtension.Replace(".", "")}/";
            return service.Configuration["applicationUrl"] + webRootPath + filePath + orderKey;

        }
        public static string PackFilePath(IApplicationService service, string createdBy, string orderKey)
        {
            var backup = service.Configuration["FileServiceSettings:BackupDmsFileDirectory"]!;
            var fileExtension = Path.GetExtension(orderKey).Replace(".", "");//获取文件格式，拓展名

            return Path.Combine(backup, createdBy, fileExtension, orderKey);
        }
    }
}
