using MediaBrowser.Common.Configuration;
using System;
using System.Configuration;
using System.IO;
namespace MediaBrowser.Common.Implementations
{
    /// 
    /// Provides a base class to hold common application paths used by both the Ui and Server.
    /// This can be subclassed to add application-specific paths.
    /// 
    public abstract class BaseApplicationPaths : IApplicationPaths
    {
        /// 
        /// The _use debug path
        /// 
        private readonly bool _useDebugPath;
        /// 
        /// Initializes a new instance of the  class.
        /// 
        protected BaseApplicationPaths(bool useDebugPath, string applicationPath)
        {
            _useDebugPath = useDebugPath;
            ApplicationPath = applicationPath;
        }
        /// 
        /// Initializes a new instance of the  class.
        /// 
        protected BaseApplicationPaths(string programDataPath, string applicationPath)
        {
            _programDataPath = programDataPath;
            ApplicationPath = applicationPath;
        }
        public string ApplicationPath { get; private set; }
        /// 
        /// The _program data path
        /// 
        private string _programDataPath;
        /// 
        /// Gets the path to the program data folder
        /// 
        /// The program data path.
        public string ProgramDataPath
        {
            get
            {
                return _programDataPath ?? (_programDataPath = GetProgramDataPath());
            }
        }
        /// 
        /// Gets the path to the system folder
        /// 
        public string ProgramSystemPath { get { return Path.Combine(ProgramDataPath, "system"); } }
        /// 
        /// The _data directory
        /// 
        private string _dataDirectory;
        /// 
        /// Gets the folder path to the data directory
        /// 
        /// The data directory.
        public string DataPath
        {
            get
            {
                if (_dataDirectory == null)
                {
                    _dataDirectory = Path.Combine(ProgramDataPath, "data");
                    Directory.CreateDirectory(_dataDirectory);
                }
                return _dataDirectory;
            }
        }
        /// 
        /// Gets the image cache path.
        /// 
        /// The image cache path.
        public string ImageCachePath
        {
            get
            {
                return Path.Combine(CachePath, "images");
            }
        }
        /// 
        /// Gets the path to the plugin directory
        /// 
        /// The plugins path.
        public string PluginsPath
        {
            get
            {
                return Path.Combine(ProgramDataPath, "plugins");
            }
        }
        /// 
        /// Gets the path to the plugin configurations directory
        /// 
        /// The plugin configurations path.
        public string PluginConfigurationsPath
        {
            get
            {
                return Path.Combine(PluginsPath, "configurations");
            }
        }
        /// 
        /// Gets the path to where temporary update files will be stored
        /// 
        /// The plugin configurations path.
        public string TempUpdatePath
        {
            get
            {
                return Path.Combine(ProgramDataPath, "updates");
            }
        }
        /// 
        /// Gets the path to the log directory
        /// 
        /// The log directory path.
        public string LogDirectoryPath
        {
            get
            {
                return Path.Combine(ProgramDataPath, "logs");
            }
        }
        /// 
        /// Gets the path to the application configuration root directory
        /// 
        /// The configuration directory path.
        public string ConfigurationDirectoryPath
        {
            get
            {
                return Path.Combine(ProgramDataPath, "config");
            }
        }
        /// 
        /// Gets the path to the system configuration file
        /// 
        /// The system configuration file path.
        public string SystemConfigurationFilePath
        {
            get
            {
                return Path.Combine(ConfigurationDirectoryPath, "system.xml");
            }
        }
        /// 
        /// The _cache directory
        /// 
        private string _cachePath;
        /// 
        /// Gets the folder path to the cache directory
        /// 
        /// The cache directory.
        public string CachePath
        {
            get
            {
                if (string.IsNullOrEmpty(_cachePath))
                {
                    _cachePath = Path.Combine(ProgramDataPath, "cache");
                    Directory.CreateDirectory(_cachePath);
                }
                return _cachePath;
            }
            set
            {
                _cachePath = value;
            }
        }
        /// 
        /// Gets the folder path to the temp directory within the cache folder
        /// 
        /// The temp directory.
        public string TempDirectory
        {
            get
            {
                return Path.Combine(CachePath, "temp");
            }
        }
        /// 
        /// Gets the path to the application's ProgramDataFolder
        /// 
        /// System.String.
        private string GetProgramDataPath()
        {
            var programDataPath = _useDebugPath ? ConfigurationManager.AppSettings["DebugProgramDataPath"] : ConfigurationManager.AppSettings["ReleaseProgramDataPath"];
            programDataPath = programDataPath.Replace("%ApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
            // If it's a relative path, e.g. "..\"
            if (!Path.IsPathRooted(programDataPath))
            {
                var path = Path.GetDirectoryName(ApplicationPath);
                if (string.IsNullOrEmpty(path))
                {
                    throw new ApplicationException("Unable to determine running assembly location");
                }
                programDataPath = Path.Combine(path, programDataPath);
                programDataPath = Path.GetFullPath(programDataPath);
            }
            Directory.CreateDirectory(programDataPath);
            return programDataPath;
        }
    }
}