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

#nullable disable

#pragma warning disable CS1591

using System;
using System.Globalization;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading;
using Jellyfin.Data;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Database.Implementations.Enums;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Querying;

namespace MediaBrowser.Controller.Channels
{
    public class Channel : Folder
    {
        [JsonIgnore]
        public override bool SupportsInheritedParentImages => false;

        [JsonIgnore]
        public override SourceType SourceType => SourceType.Channel;

        /// <summary>
        /// Determines whether this channel is visible to the specified user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="skipAllowedTagsCheck">Whether to skip the allowed tags check.</param>
        /// <returns>True if visible; otherwise, false.</returns>
        public override bool IsVisible(User user, bool skipAllowedTagsCheck = false)
        {
            var blockedChannelsPreference = user.GetPreferenceValues<Guid>(PreferenceKind.BlockedChannels);
            if (blockedChannelsPreference.Length != 0)
            {
                if (blockedChannelsPreference.Contains(this.Id))
                {
                    return false;
                }
            }
            else
            {
                if (!user.HasPermission(PermissionKind.EnableAllChannels)
                    && !user.GetPreferenceValues<Guid>(PreferenceKind.EnabledChannels).Contains(this.Id))
                {
                    return false;
                }
            }

            return base.IsVisible(user, skipAllowedTagsCheck);
        }

        /// <summary>
        /// Gets items from the channel.
        /// </summary>
        /// <param name="query">The items query.</param>
        /// <returns>The query result.</returns>
        protected override QueryResult<BaseItem> GetItemsInternal(InternalItemsQuery query)
        {
            try
            {
                query.Parent = this;
                query.ChannelIds = new Guid[] { this.Id };

                // Don't blow up here because it could cause parent screens with other content to fail
                return ChannelManager.GetChannelItemsInternal(query, new Progress<double>(), CancellationToken.None).GetAwaiter().GetResult();
            }
            catch
            {
                // Already logged at lower levels
                return new QueryResult<BaseItem>();
            }
        }

        /// <summary>
        /// Gets the internal metadata path for this channel.
        /// </summary>
        /// <param name="basePath">The base path.</param>
        /// <returns>The internal metadata path.</returns>
        protected override string GetInternalMetadataPath(string basePath)
        {
            return GetInternalMetadataPath(basePath, this.Id);
        }

        /// <summary>
        /// Gets the internal metadata path for a channel with the specified ID.
        /// </summary>
        /// <param name="basePath">The base path.</param>
        /// <param name="id">The channel ID.</param>
        /// <returns>The internal metadata path.</returns>
        public static string GetInternalMetadataPath(string basePath, Guid id)
        {
            return System.IO.Path.Combine(basePath, "channels", id.ToString("N", CultureInfo.InvariantCulture), "metadata");
        }

        /// <summary>
        /// Determines whether this channel can be deleted.
        /// </summary>
        /// <returns>True if deletable; otherwise, false.</returns>
        public override bool CanDelete()
        {
            return false;
        }

        /// <summary>
        /// Determines whether the channel item is visible to the specified user.
        /// </summary>
        /// <param name="channelItem">The channel item.</param>
        /// <param name="user">The user.</param>
        /// <returns>True if visible; otherwise, false.</returns>
        internal static bool IsChannelVisible(BaseItem channelItem, User user)
        {
            var channel = ChannelManager.GetChannel(channelItem.ChannelId.ToString(string.Empty));

            return channel.IsVisible(user);
        }
    }
}
