// <copyright file="PhotoProvider.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

#pragma warning disable SA1309 // Variables should not begin with underscore

namespace Emby.Photos
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using Jellyfin.Extensions;
    using MediaBrowser.Controller.Drawing;
    using MediaBrowser.Controller.Entities;
    using MediaBrowser.Controller.Library;
    using MediaBrowser.Controller.Providers;
    using MediaBrowser.Model.Drawing;
    using MediaBrowser.Model.Entities;
    using MediaBrowser.Model.IO;
    using Microsoft.Extensions.Logging;
    using TagLib;
    using TagLib.IFD;
    using TagLib.IFD.Entries;
    using TagLib.IFD.Tags;

    /// <summary>
    /// Metadata provider for photos.
    /// </summary>
    /// <param name="logger">The logger.</param>
    /// <param name="imageProcessor">The image processor.</param>
    public class PhotoProvider(ILogger<PhotoProvider> logger, IImageProcessor imageProcessor) : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor
    {
        // Other extensions might cause taglib to hang
        private readonly string[] _includeExtensions = [".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif"];

        /// <inheritdoc />
        public string Name => "Embedded Information";

        /// <inheritdoc />
        public bool HasChanged(BaseItem item, IDirectoryService directoryService)
        {
            if (item.IsFileProtocol)
            {
                FileSystemMetadata? file = directoryService.GetFile(item.Path);
                return file is not null && item.HasChanged(file.LastWriteTimeUtc);
            }

            return false;
        }

        /// <inheritdoc />
        public Task<ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
        {
            item.SetImagePath(ImageType.Primary, item.Path);

            // Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs
            if (this._includeExtensions.Contains(Path.GetExtension(item.Path.AsSpan()), StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    using TagLib.File file = TagLib.File.Create(item.Path);
                    if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag)
                    {
                        IFDStructure? structure = tag.Structure;
                        if (structure?.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
                        {
                            IFDStructure? exifStructure = exif.Structure;
                            if (exifStructure is not null)
                            {
                                if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) is RationalIFDEntry apertureEntry)
                                {
                                    item.Aperture = (double)apertureEntry.Value.Numerator / apertureEntry.Value.Denominator;
                                }

                                if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) is RationalIFDEntry shutterSpeedEntry)
                                {
                                    item.ShutterSpeed = (double)shutterSpeedEntry.Value.Numerator / shutterSpeedEntry.Value.Denominator;
                                }
                            }
                        }
                    }

                    if (file is TagLib.Image.File image)
                    {
                        item.CameraMake = image.ImageTag.Make;
                        item.CameraModel = image.ImageTag.Model;

                        item.Width = image.Properties.PhotoWidth;
                        item.Height = image.Properties.PhotoHeight;

                        item.CommunityRating = image.ImageTag.Rating;

                        item.Overview = image.ImageTag.Comment;

                        if (!string.IsNullOrWhiteSpace(image.ImageTag.Title)
                            && !item.LockedFields.Contains(MetadataField.Name))
                        {
                            item.Name = image.ImageTag.Title;
                        }

                        DateTime? dateTaken = image.ImageTag.DateTime;
                        if (dateTaken.HasValue)
                        {
                            item.DateCreated = dateTaken.Value.ToUniversalTime();
                            item.PremiereDate = dateTaken.Value;
                            item.ProductionYear = dateTaken.Value.Year;
                        }

                        item.Genres = image.ImageTag.Genres;
                        item.Tags = image.ImageTag.Keywords;
                        item.Software = image.ImageTag.Software;

                        if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
                        {
                            item.Orientation = null;
                        }
                        else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation))
                        {
                            item.Orientation = orientation;
                        }

                        item.ExposureTime = image.ImageTag.ExposureTime;
                        item.FocalLength = image.ImageTag.FocalLength;

                        item.Latitude = image.ImageTag.Latitude;
                        item.Longitude = image.ImageTag.Longitude;
                        item.Altitude = image.ImageTag.Altitude;

                        item.IsoSpeedRating = image.ImageTag.ISOSpeedRatings.HasValue
                            ? Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value)
                            : null;
                    }
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
                }
            }

            if (item.Width <= 0 || item.Height <= 0)
            {
                ItemImageInfo img = item.GetImageInfo(ImageType.Primary, 0);

                try
                {
                    ImageDimensions size = imageProcessor.GetImageDimensions(item, img);

                    if (size.Width > 0 && size.Height > 0)
                    {
                        item.Width = size.Width;
                        item.Height = size.Height;
                    }
                }
                catch (ArgumentException)
                {
                    // format not supported
                }
            }

            const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
            return Task.FromResult(Result);
        }
    }
}
