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

#pragma warning disable SA1615  // Element should be documented

namespace MediaBrowser.Model.Dlna;

using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Xml.Serialization;
using MediaBrowser.Model.Extensions;

/// <summary>
/// Defines the <see cref="CodecProfile"/>.
/// </summary>
public class CodecProfile
{
    /// <summary>
    /// Initializes a new instance of the <see cref="CodecProfile"/> class.
    /// </summary>
    public CodecProfile()
    {
        this.Conditions = [];
        this.ApplyConditions = [];
    }

    /// <summary>
    /// Gets or sets the <see cref="CodecType"/> which this container must meet.
    /// </summary>
    [XmlAttribute("type")]
    public CodecType Type { get; set; }

    /// <summary>
    /// Gets or sets the list of <see cref="ProfileCondition"/> which this profile must meet.
    /// </summary>
    public List<ProfileCondition> Conditions { get; set; } = [];

    /// <summary>
    /// Gets or sets the list of <see cref="ProfileCondition"/> to apply if this profile is met.
    /// </summary>
    public List<ProfileCondition> ApplyConditions { get; set; } = [];

    /// <summary>
    /// Gets or sets the codec(s) that this profile applies to.
    /// </summary>
    [XmlAttribute("codec")]
    public string? Codec { get; set; }

    /// <summary>
    /// Gets or sets the container(s) which this profile will be applied to.
    /// </summary>
    [XmlAttribute("container")]
    public string? Container { get; set; }

    /// <summary>
    /// Gets or sets the sub-container(s) which this profile will be applied to.
    /// </summary>
    [XmlAttribute("subcontainer")]
    public string? SubContainer { get; set; }

    /// <summary>
    /// Checks to see whether the codecs and containers contain the given parameters.
    /// </summary>
    /// <param name="codec">The codec to match (single codec or comma-separated list).</param>
    /// <param name="container">The container to match.</param>
    /// <param name="useSubContainer">When true and Container is "hls", uses SubContainer instead of Container for matching.</param>
    /// <returns>True if both the codec and container conditions are met.</returns>
    public bool ContainsAnyCodec(string? codec, string? container, bool useSubContainer = false)
    {
        if (codec is null)
        {
            return false;
        }

        var containerToCheck =
            useSubContainer &&
            string.Equals(this.Container, "hls", StringComparison.OrdinalIgnoreCase)
            ? this.SubContainer
            : this.Container;

        return ContainerHelper.ContainsContainer(containerToCheck, container)
            && ContainerHelper.ContainsContainer(this.Codec, codec);
    }

    /// <summary>
    /// Checks to see whether the codecs and containers contain the given parameters.
    /// </summary>
    /// <param name="codecs">The list of codecs to match.</param>
    /// <param name="container">The container to match.</param>
    /// <param name="useSubContainer">When true and Container is "hls", uses SubContainer instead of Container for matching.</param>
    /// <returns>True if both the codec and container conditions are met.</returns>
    public bool ContainsAnyCodec(IReadOnlyList<string> codecs, string? container, bool useSubContainer = false)
    {
        if (codecs == null || codecs.Count == 0)
        {
            return false;
        }

        // Join codecs with comma to match ContainerHelper's expected format
        var codecString = string.Join(',', codecs);
        return ContainsAnyCodec(codecString, container, useSubContainer);
    }
}
