using System.IO;
using System.Threading.Tasks;
using System.Xml;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.LocalMetadata.Savers
{
    /// 
    /// Playlist xml saver.
    /// 
    public class PlaylistXmlSaver : BaseXmlSaver
    {
        /// 
        /// The default file name to use when creating a new playlist.
        /// 
        public const string DefaultPlaylistFilename = "playlist.xml";
        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        /// Instance of the  interface.
        public PlaylistXmlSaver(IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILibraryManager libraryManager, ILogger logger)
            : base(fileSystem, configurationManager, libraryManager, logger)
        {
        }
        /// 
        public override bool IsEnabledFor(BaseItem item, ItemUpdateType updateType)
        {
            if (!item.SupportsLocalMetadata)
            {
                return false;
            }
            return item is Playlist && updateType >= ItemUpdateType.MetadataImport;
        }
        /// 
        protected override Task WriteCustomElementsAsync(BaseItem item, XmlWriter writer)
        {
            var game = (Playlist)item;
            if (game.PlaylistMediaType == MediaType.Unknown)
            {
                return Task.CompletedTask;
            }
            return writer.WriteElementStringAsync(null, "PlaylistMediaType", null, game.PlaylistMediaType.ToString());
        }
        /// 
        protected override string GetLocalSavePath(BaseItem item)
        {
            return GetSavePath(item.Path);
        }
        /// 
        /// Get the save path.
        /// 
        /// The item path.
        /// The save path.
        public static string GetSavePath(string itemPath)
        {
            var path = itemPath;
            if (Playlist.IsPlaylistFile(path))
            {
                return Path.ChangeExtension(itemPath, ".xml");
            }
            return Path.Combine(path, DefaultPlaylistFilename);
        }
    }
}