#nullable enable
using System.Threading;
using System.Threading.Tasks;

using DesktopProto = IGP.UnitySDK.Generated.IGPProtoContracts.DesktopSession;

namespace IGP.UnitySDK.Abstractions
{
    internal enum IGPHostServiceMessageSubcommand
    {
        Unspecified = (int)DesktopProto.DesktopSessionServiceMessageSubcommand.Unspecified,
        GameKitCommunicationVoiceSpeakingStarted =
            (int)DesktopProto.DesktopSessionServiceMessageSubcommand.GameKitCommunicationVoiceSpeakingStarted,
        GameKitCommunicationVoiceSpeakingStopped =
            (int)DesktopProto.DesktopSessionServiceMessageSubcommand.GameKitCommunicationVoiceSpeakingStopped,
        GameKitCommunicationMessage =
            (int)DesktopProto.DesktopSessionServiceMessageSubcommand.GameKitCommunicationMessage,
        GameKitMatchResultReport =
            (int)DesktopProto.DesktopSessionServiceMessageSubcommand.GameKitMatchResultReport,
        LyingBottleInvitationSend =
            (int)DesktopProto.DesktopSessionServiceMessageSubcommand.LyingBottleInvitationSend,
        LyingBottleInvitationAccepted =
            (int)DesktopProto.DesktopSessionServiceMessageSubcommand.LyingBottleInvitationAccepted,
        WorldSyncSubscribe = (int)DesktopProto.DesktopSessionServiceMessageSubcommand.WorldSyncSubscribe,
        WorldSyncConnection = (int)DesktopProto.DesktopSessionServiceMessageSubcommand.WorldSyncConnection,
        WorldSyncInvalidated = (int)DesktopProto.DesktopSessionServiceMessageSubcommand.WorldSyncInvalidated,
        GameKitFriendsPresence = (int)DesktopProto.DesktopSessionServiceMessageSubcommand.GameKitFriendsPresence,
        End = (int)DesktopProto.DesktopSessionServiceMessageSubcommand.End,
    }

    internal static class IGPHostServiceMessageSubcommands
    {
        internal const IGPHostServiceMessageSubcommand CurrentLimit =
            IGPHostServiceMessageSubcommand.End;

        internal static bool IsSendable(IGPHostServiceMessageSubcommand subcommand)
        {
            return subcommand > IGPHostServiceMessageSubcommand.Unspecified &&
                subcommand < CurrentLimit;
        }
    }

    internal sealed class IGPHostServiceMessage
    {
        internal IGPHostServiceMessage(
            string messageId,
            IGPHostServiceMessageSubcommand subcommand,
            string contentJson,
            string? replyToMessageId = null)
        {
            MessageId = messageId ?? string.Empty;
            Subcommand = subcommand;
            ContentJson = contentJson ?? string.Empty;
            ReplyToMessageId = replyToMessageId ?? string.Empty;
        }

        internal string MessageId { get; }
        internal IGPHostServiceMessageSubcommand Subcommand { get; }
        internal string ContentJson { get; }
        internal string ReplyToMessageId { get; }
    }

    internal interface IIGPHostServiceMessageSource
    {
        event System.Action<IGPHostServiceMessage>? ServiceMessageReceived;
        event System.Action? ServiceMessageStreamReset;
    }

    /// <summary>
    /// Capability-neutral Host Session command channel used by optional packages.
    /// Core resolves the Unity runtime platform before creating one Host runtime.
    /// </summary>
    public interface IIGPHostCommandGateway
    {
        bool IsAttached { get; }
        int AttachedAppId { get; }

        bool SupportsCapability(int capabilityFieldNumber);

        Task<IGPDesktopSessionCommandResult> SendAsync(
            int commandType,
            int capabilityFieldNumber = 0,
            string? contentJson = null,
            byte[]? contentBytes = null,
            CancellationToken cancellationToken = default);
    }

    internal interface IIGPHostNotificationSource
    {
        event System.Action<IGPDesktopSessionCommandResult>? NotificationReceived;
    }
}
