using MediaBrowser.Common.Plugins;
using MediaBrowser.Controller.ScheduledTasks;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Plugins.Trailers.Configuration;
using MediaBrowser.Plugins.Trailers.ScheduledTasks;
using System;
using System.ComponentModel.Composition;
using System.IO;
namespace MediaBrowser.Plugins.Trailers
{
    /// 
    /// Class Plugin
    /// 
    [Export(typeof(IPlugin))]
    public class Plugin : BasePlugin
    {
        /// 
        /// Gets the name of the plugin
        /// 
        /// The name.
        public override string Name
        {
            get { return "Trailers"; }
        }
        /// 
        /// Gets the description.
        /// 
        /// The description.
        public override string Description
        {
            get
            {
                return "Movie trailers for your collection.";
            }
        }
        /// 
        /// Gets the instance.
        /// 
        /// The instance.
        public static Plugin Instance { get; private set; }
        /// 
        /// Initializes a new instance of the  class.
        /// 
        public Plugin()
            : base()
        {
            Instance = this;
        }
        /// 
        /// The _download path
        /// 
        private string _downloadPath;
        /// 
        /// Gets the path to the trailer download directory
        /// 
        /// The download path.
        public string DownloadPath
        {
            get
            {
                if (_downloadPath == null)
                {
                    // Use 
                    _downloadPath = Configuration.DownloadPath;
                    if (string.IsNullOrWhiteSpace(_downloadPath))
                    {
                        _downloadPath = Path.Combine(Controller.Kernel.Instance.ApplicationPaths.DataPath, Name);
                    }
                    if (!Directory.Exists(_downloadPath))
                    {
                        Directory.CreateDirectory(_downloadPath);
                    }
                }
                return _downloadPath;
            }
        }
        /// 
        /// Starts the plugin on the server
        /// 
        /// if set to true [is first run].
        protected override void InitializeOnServer(bool isFirstRun)
        {
            base.InitializeOnServer(isFirstRun);
            if (isFirstRun)
            {
                Kernel.TaskManager.QueueScheduledTask();
            }
        }
        /// 
        /// Completely overwrites the current configuration with a new copy
        /// Returns true or false indicating success or failure
        /// 
        /// The configuration.
        public override void UpdateConfiguration(BasePluginConfiguration configuration)
        {
            var config = (PluginConfiguration) configuration;
            var pathChanged = !string.Equals(Configuration.DownloadPath, config.DownloadPath, StringComparison.OrdinalIgnoreCase);
            base.UpdateConfiguration(configuration);
            if (pathChanged)
            {
                _downloadPath = null;
                Kernel.TaskManager.QueueScheduledTask();
            }
        }
    }
}