using System;

namespace Jellyfin.Plugin.SRFPlay.Utilities;

/// <summary>
/// Helper class for determining MIME content types.
/// </summary>
public static class MimeTypeHelper
{
    /// <summary>
    /// Gets the MIME content type for an image based on URL or file extension.
    /// </summary>
    /// <param name="url">The image URL.</param>
    /// <returns>The MIME content type, defaulting to "image/jpeg" for SRF images.</returns>
    public static string GetImageContentType(string? url)
    {
        if (string.IsNullOrEmpty(url))
        {
            return "image/jpeg";
        }

        var uri = new Uri(url);
        var path = uri.AbsolutePath.ToLowerInvariant();

        if (path.EndsWith(".png", StringComparison.Ordinal))
        {
            return "image/png";
        }

        if (path.EndsWith(".gif", StringComparison.Ordinal))
        {
            return "image/gif";
        }

        if (path.EndsWith(".webp", StringComparison.Ordinal))
        {
            return "image/webp";
        }

        if (path.EndsWith(".jpg", StringComparison.Ordinal) || path.EndsWith(".jpeg", StringComparison.Ordinal))
        {
            return "image/jpeg";
        }

        // Default to JPEG for SRF images (most common)
        return "image/jpeg";
    }

    /// <summary>
    /// Gets the MIME content type for a media segment based on file extension.
    /// </summary>
    /// <param name="path">The segment path or filename.</param>
    /// <returns>The MIME content type.</returns>
    public static string GetSegmentContentType(string path)
    {
        if (path.EndsWith(".ts", StringComparison.OrdinalIgnoreCase))
        {
            return "video/MP2T";
        }

        if (path.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase) ||
            path.EndsWith(".m4s", StringComparison.OrdinalIgnoreCase))
        {
            return "video/mp4";
        }

        if (path.EndsWith(".aac", StringComparison.OrdinalIgnoreCase))
        {
            return "audio/aac";
        }

        return "application/octet-stream";
    }
}
