using System;
using System.IO;
using Emby.Server.Implementations.AppBase;
using MediaBrowser.Controller;
namespace Emby.Server.Implementations
{
    /// 
    /// Extends BaseApplicationPaths to add paths that are only applicable on the server
    /// 
    public class ServerApplicationPaths : BaseApplicationPaths, IServerApplicationPaths
    {
        /// 
        /// Initializes a new instance of the  class.
        /// 
        public ServerApplicationPaths(
            string programDataPath,
            string appFolderPath,
            string applicationResourcesPath,
            string logDirectoryPath = null,
            string configurationDirectoryPath = null)
            : base(programDataPath, appFolderPath, logDirectoryPath, configurationDirectoryPath)
        {
            ApplicationResourcesPath = applicationResourcesPath;
        }
        public string ApplicationResourcesPath { get; private set; }
        /// 
        /// Gets the path to the base root media directory
        /// 
        /// The root folder path.
        public string RootFolderPath => Path.Combine(ProgramDataPath, "root");
        /// 
        /// Gets the path to the default user view directory.  Used if no specific user view is defined.
        /// 
        /// The default user views path.
        public string DefaultUserViewsPath => Path.Combine(RootFolderPath, "default");
        /// 
        /// Gets the path to localization data.
        /// 
        /// The localization path.
        public string LocalizationPath => Path.Combine(ProgramDataPath, "localization");
        /// 
        /// Gets the path to the People directory
        /// 
        /// The people path.
        public string PeoplePath => Path.Combine(InternalMetadataPath, "People");
        public string ArtistsPath => Path.Combine(InternalMetadataPath, "artists");
        /// 
        /// Gets the path to the Genre directory
        /// 
        /// The genre path.
        public string GenrePath => Path.Combine(InternalMetadataPath, "Genre");
        /// 
        /// Gets the path to the Genre directory
        /// 
        /// The genre path.
        public string MusicGenrePath => Path.Combine(InternalMetadataPath, "MusicGenre");
        /// 
        /// Gets the path to the Studio directory
        /// 
        /// The studio path.
        public string StudioPath => Path.Combine(InternalMetadataPath, "Studio");
        /// 
        /// Gets the path to the Year directory
        /// 
        /// The year path.
        public string YearPath => Path.Combine(InternalMetadataPath, "Year");
        /// 
        /// Gets the path to the General IBN directory
        /// 
        /// The general path.
        public string GeneralPath => Path.Combine(InternalMetadataPath, "general");
        /// 
        /// Gets the path to the Ratings IBN directory
        /// 
        /// The ratings path.
        public string RatingsPath => Path.Combine(InternalMetadataPath, "ratings");
        /// 
        /// Gets the media info images path.
        /// 
        /// The media info images path.
        public string MediaInfoImagesPath => Path.Combine(InternalMetadataPath, "mediainfo");
        /// 
        /// Gets the path to the user configuration directory
        /// 
        /// The user configuration directory path.
        public string UserConfigurationDirectoryPath => Path.Combine(ConfigurationDirectoryPath, "users");
        private string _defaultTranscodingTempPath;
        public string DefaultTranscodingTempPath => _defaultTranscodingTempPath ?? (_defaultTranscodingTempPath = Path.Combine(ProgramDataPath, "transcoding-temp"));
        private string _transcodingTempPath;
        public string TranscodingTempPath
        {
            get => _transcodingTempPath ?? (_transcodingTempPath = DefaultTranscodingTempPath);
            set => _transcodingTempPath = value;
        }
        public string GetTranscodingTempPath()
        {
            var path = TranscodingTempPath;
            if (!string.Equals(path, DefaultTranscodingTempPath, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    Directory.CreateDirectory(path);
                    var testPath = Path.Combine(path, Guid.NewGuid().ToString());
                    Directory.CreateDirectory(testPath);
                    Directory.Delete(testPath);
                    return path;
                }
                catch
                {
                }
            }
            path = DefaultTranscodingTempPath;
            Directory.CreateDirectory(path);
            return path;
        }
        /// 
        /// Gets the game genre path.
        /// 
        /// The game genre path.
        public string GameGenrePath => Path.Combine(InternalMetadataPath, "GameGenre");
        private string _internalMetadataPath;
        public string InternalMetadataPath
        {
            get => _internalMetadataPath ?? (_internalMetadataPath = Path.Combine(DataPath, "metadata"));
            set => _internalMetadataPath = value;
        }
        private const string _virtualInternalMetadataPath = "%MetadataPath%";
        public string VirtualInternalMetadataPath => _virtualInternalMetadataPath;
    }
}