using MediaBrowser.Common.Net;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
using ServiceStack.ServiceHost;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace MediaBrowser.Api
{
    /// 
    /// Class GetDirectoryContents
    /// 
    [Route("/Environment/DirectoryContents", "GET")]
    [Api(Description = "Gets the contents of a given directory in the file system")]
    public class GetDirectoryContents : IReturn>
    {
        /// 
        /// Gets or sets the path.
        /// 
        /// The path.
        [ApiMember(Name = "Path", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
        public string Path { get; set; }
        /// 
        /// Gets or sets a value indicating whether [include files].
        /// 
        /// true if [include files]; otherwise, false.
        [ApiMember(Name = "IncludeFiles", Description = "An optional filter to include or exclude files from the results. true/false", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
        public bool IncludeFiles { get; set; }
        /// 
        /// Gets or sets a value indicating whether [include directories].
        /// 
        /// true if [include directories]; otherwise, false.
        [ApiMember(Name = "IncludeDirectories", Description = "An optional filter to include or exclude folders from the results. true/false", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
        public bool IncludeDirectories { get; set; }
        /// 
        /// Gets or sets a value indicating whether [include hidden].
        /// 
        /// true if [include hidden]; otherwise, false.
        [ApiMember(Name = "IncludeHidden", Description = "An optional filter to include or exclude hidden files and folders. true/false", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
        public bool IncludeHidden { get; set; }
    }
    /// 
    /// Class GetDrives
    /// 
    [Route("/Environment/Drives", "GET")]
    [Api(Description = "Gets available drives from the server's file system")]
    public class GetDrives : IReturn>
    {
    }
    /// 
    /// Class GetNetworkComputers
    /// 
    [Route("/Environment/NetworkDevices", "GET")]
    [Api(Description = "Gets a list of devices on the network")]
    public class GetNetworkDevices : IReturn>
    {
    }
    /// 
    /// Class EnvironmentService
    /// 
    public class EnvironmentService : BaseApiService
    {
        /// 
        /// The _network manager
        /// 
        private readonly INetworkManager _networkManager;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The network manager.
        /// networkManager
        public EnvironmentService(INetworkManager networkManager)
        {
            if (networkManager == null)
            {
                throw new ArgumentNullException("networkManager");
            }
            _networkManager = networkManager;
        }
        /// 
        /// Gets the specified request.
        /// 
        /// The request.
        /// System.Object.
        /// Path
        /// 
        public object Get(GetDirectoryContents request)
        {
            var path = request.Path;
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("Path");
            }
            if (path.StartsWith(NetworkPrefix, StringComparison.OrdinalIgnoreCase) && path.LastIndexOf('\\') == 1)
            {
                return ToOptimizedResult(GetNetworkShares(path).OrderBy(i => i.Path).ToList());
            }
            // Reject invalid input
            if (!Path.IsPathRooted(path))
            {
                throw new ArgumentException(string.Format("Invalid path: {0}", path));
            }
            return ToOptimizedResult(GetFileSystemEntries(request).OrderBy(i => i.Path).ToList());
        }
        /// 
        /// Gets the specified request.
        /// 
        /// The request.
        /// System.Object.
        public object Get(GetDrives request)
        {
            var result = GetDrives().ToList();
            return ToOptimizedResult(result);
        }
        /// 
        /// Gets the specified request.
        /// 
        /// The request.
        /// System.Object.
        public object Get(GetNetworkDevices request)
        {
            var result = GetNetworkDevices().OrderBy(i => i.Path).ToList();
            return ToOptimizedResult(result);
        }
        /// 
        /// Gets the list that is returned when an empty path is supplied
        /// 
        /// IEnumerable{FileSystemEntryInfo}.
        private IEnumerable GetDrives()
        {
            // Only include drives in the ready state or this method could end up being very slow, waiting for drives to timeout
            return DriveInfo.GetDrives().Where(d => d.IsReady).Select(d => new FileSystemEntryInfo
            {
                Name = GetName(d),
                Path = d.RootDirectory.FullName,
                Type = FileSystemEntryType.Directory
            });
        }
        /// 
        /// Gets the network computers.
        /// 
        /// IEnumerable{FileSystemEntryInfo}.
        private IEnumerable GetNetworkDevices()
        {
            return _networkManager.GetNetworkDevices().Select(c => new FileSystemEntryInfo
            {
                Name = c,
                Path = NetworkPrefix + c,
                Type = FileSystemEntryType.NetworkComputer
            });
        }
        /// 
        /// Gets the name.
        /// 
        /// The drive.
        /// System.String.
        private string GetName(DriveInfo drive)
        {
            return drive.Name;
        }
        /// 
        /// Gets the network shares.
        /// 
        /// The path.
        /// IEnumerable{FileSystemEntryInfo}.
        private IEnumerable GetNetworkShares(string path)
        {
            return _networkManager.GetNetworkShares(path).Where(s => s.ShareType == NetworkShareType.Disk).Select(c => new FileSystemEntryInfo
            {
                Name = c.Name,
                Path = Path.Combine(path, c.Name),
                Type = FileSystemEntryType.NetworkShare
            });
        }
        /// 
        /// Gets the file system entries.
        /// 
        /// The request.
        /// IEnumerable{FileSystemEntryInfo}.
        private IEnumerable GetFileSystemEntries(GetDirectoryContents request)
        {
            var entries = new DirectoryInfo(request.Path).EnumerateFileSystemInfos().Where(i =>
            {
                if (!request.IncludeHidden && i.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    return false;
                }
                var isDirectory = i.Attributes.HasFlag(FileAttributes.Directory);
                if (!request.IncludeFiles && !isDirectory)
                {
                    return false;
                }
                if (!request.IncludeDirectories && isDirectory)
                {
                    return false;
                }
                
                return true;
            });
            return entries.Select(f => new FileSystemEntryInfo
            {
                Name = f.Name,
                Path = f.FullName,
                Type = f.Attributes.HasFlag(FileAttributes.Directory) ? FileSystemEntryType.Directory : FileSystemEntryType.File
            }).ToList();
        }
        /// 
        /// Gets the network prefix.
        /// 
        /// The network prefix.
        private string NetworkPrefix
        {
            get { return Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture) + Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture); }
        }
    }
}