#pragma warning disable CS1591
using System;
using System.Linq;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Controller.Entities
{
    public static class BaseItemExtensions
    {
        /// 
        /// Gets the image path.
        /// 
        /// The item.
        /// Type of the image.
        /// System.String.
        public static string GetImagePath(this BaseItem item, ImageType imageType)
        {
            return item.GetImagePath(imageType, 0);
        }
        public static bool HasImage(this BaseItem item, ImageType imageType)
        {
            return item.HasImage(imageType, 0);
        }
        /// 
        /// Sets the image path.
        /// 
        /// The item.
        /// Type of the image.
        /// The file.
        public static void SetImagePath(this BaseItem item, ImageType imageType, FileSystemMetadata file)
        {
            item.SetImagePath(imageType, 0, file);
        }
        /// 
        /// Sets the image path.
        /// 
        /// The item.
        /// Type of the image.
        /// The file.
        public static void SetImagePath(this BaseItem item, ImageType imageType, string file)
        {
            if (file.StartsWith("http", StringComparison.OrdinalIgnoreCase))
            {
                item.SetImage(
                    new ItemImageInfo
                    {
                        Path = file,
                        Type = imageType
                    },
                    0);
            }
            else
            {
                item.SetImagePath(imageType, BaseItem.FileSystem.GetFileInfo(file));
            }
        }
        /// 
        /// Copies all properties on object. Skips properties that do not exist.
        /// 
        /// The source object.
        /// The destination object.
        /// Source type.
        /// Destination type.
        public static void DeepCopy(this T source, TU dest)
            where T : BaseItem
            where TU : BaseItem
        {
            ArgumentNullException.ThrowIfNull(source);
            ArgumentNullException.ThrowIfNull(dest);
            var destProps = typeof(TU).GetProperties().Where(x => x.CanWrite).ToList();
            foreach (var sourceProp in typeof(T).GetProperties())
            {
                // We should be able to write to the property
                // for both the source and destination type
                // This is only false when the derived type hides the base member
                // (which we shouldn't copy anyway)
                if (!sourceProp.CanRead || !sourceProp.CanWrite)
                {
                    continue;
                }
                var v = sourceProp.GetValue(source);
                if (v is null)
                {
                    continue;
                }
                var p = destProps.Find(x => x.Name == sourceProp.Name);
                p?.SetValue(dest, v);
            }
        }
        /// 
        /// Copies all properties on newly created object. Skips properties that do not exist.
        /// 
        /// The source object.
        /// Source type.
        /// Destination type.
        /// Destination object.
        public static TU DeepCopy(this T source)
            where T : BaseItem
            where TU : BaseItem, new()
        {
            var dest = new TU();
            source.DeepCopy(dest);
            return dest;
        }
        /// 
        /// Determines if the item has changed.
        /// 
        /// The source object.
        /// The timestamp to detect changes as of.
        /// Source type.
        /// Whether the item has changed.
        public static bool HasChanged(this T source, DateTime asOf)
            where T : BaseItem
        {
            ArgumentNullException.ThrowIfNull(source);
            return source.DateModified.Subtract(asOf).Duration().TotalSeconds > 1;
        }
    }
}