using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace MediaBrowser.Controller.Resolvers
{
    /// 
    /// Class EntityResolutionHelper
    /// 
    public static class EntityResolutionHelper
    {
        /// 
        /// Any extension in this list is considered a video file - can be added to at runtime for extensibility
        /// 
        public static List VideoFileExtensions = new List
            {
                ".mkv",
                ".m2t",
                ".m2ts",
                ".img",
                ".iso",
                ".mk3d",
                ".ts",
                ".rmvb",
                ".mov",
                ".avi",
                ".mpg",
                ".mpeg",
                ".wmv",
                ".mp4",
                ".divx",
                ".dvr-ms",
                ".wtv",
                ".ogm",
                ".ogv",
                ".asf",
                ".m4v",
                ".flv",
                ".f4v",
                ".3gp",
                ".webm",
                ".mts"
        };
        private static readonly Dictionary VideoFileExtensionsDictionary = VideoFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
        private static readonly Regex MultiFileRegex = new Regex(
            @"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)(.*?)(\.[^.]+)$",
            RegexOptions.Compiled | RegexOptions.IgnoreCase);
        private static readonly Regex MultiFolderRegex = new Regex(
            @"(.*?)([ _.-]*(?:cd|dvd|p(?:ar)?t|dis[ck]|d)[ _.-]*[0-9]+)$",
            RegexOptions.Compiled | RegexOptions.IgnoreCase);
        /// 
        /// Determines whether [is multi part file] [the specified path].
        /// 
        /// The path.
        /// true if [is multi part file] [the specified path]; otherwise, false.
        public static bool IsMultiPartFile(string path)
        {
            return MultiFileRegex.Match(path).Success || MultiFolderRegex.Match(path).Success;
        }
        /// 
        /// The audio file extensions
        /// 
        public static readonly string[] AudioFileExtensions = new[]
            {
                ".mp3",
                ".flac",
                ".wma",
                ".aac",
                ".acc",
                ".m4a",
                ".m4b",
                ".wav",
                ".ape",
                ".ogg",
                ".oga"
            };
        private static readonly Dictionary AudioFileExtensionsDictionary = AudioFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
        /// 
        /// Determines whether [is audio file] [the specified args].
        /// 
        /// The path.
        /// true if [is audio file] [the specified args]; otherwise, false.
        public static bool IsAudioFile(string path)
        {
            var extension = Path.GetExtension(path);
            if (string.IsNullOrEmpty(extension))
            {
                return false;
            }
            return AudioFileExtensionsDictionary.ContainsKey(extension);
        }
        /// 
        /// Determines whether [is video file] [the specified path].
        /// 
        /// The path.
        /// true if [is video file] [the specified path]; otherwise, false.
        public static bool IsVideoFile(string path)
        {
            var extension = Path.GetExtension(path);
            if (string.IsNullOrEmpty(extension))
            {
                return false;
            }
            return VideoFileExtensionsDictionary.ContainsKey(extension);
        }
        /// 
        /// Ensures DateCreated and DateModified have values
        /// 
        /// The item.
        /// The args.
        /// if set to true [include creation time].
        public static void EnsureDates(BaseItem item, ItemResolveArgs args, bool includeCreationTime)
        {
            if (!Path.IsPathRooted(item.Path))
            {
                return;
            }
            // See if a different path came out of the resolver than what went in
            if (!string.Equals(args.Path, item.Path, StringComparison.OrdinalIgnoreCase))
            {
                var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
                if (childData != null)
                {
                    if (includeCreationTime)
                    {
                        item.DateCreated = childData.CreationTimeUtc;
                    }
                    item.DateModified = childData.LastWriteTimeUtc;
                }
                else
                {
                    var fileData = FileSystem.GetFileSystemInfo(item.Path);
                    if (fileData.Exists)
                    {
                        if (includeCreationTime)
                        {
                            item.DateCreated = fileData.CreationTimeUtc;
                        }
                        item.DateModified = fileData.LastWriteTimeUtc;
                    }
                }
            }
            else
            {
                if (includeCreationTime)
                {
                    item.DateCreated = args.FileInfo.CreationTimeUtc;
                }
                item.DateModified = args.FileInfo.LastWriteTimeUtc;
            }
        }
    }
}