#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;

using IGP.UnitySDK.Models;
using Newtonsoft.Json;

namespace IGP.UnitySDK
{
    public sealed partial class IGPDesktopSessionClient
    {

        public async Task<IGPDesktopUserAvatarInfo> GetDesktopUserAvatarInfoAsync(
            CancellationToken cancellationToken = default)
        {
            var result = await SendCommandAsync(
                IGPDesktopSessionCommandType.GetDesktopUserAvatarInfo,
                cancellationToken);

            return ParseCommandPayload(
                result.ContentJson,
                () => new IGPDesktopUserAvatarInfo());
        }

        public async Task<IGPDesktopSessionCommandResult> GetDesktopUserProfileAsync(
            CancellationToken cancellationToken = default)
        {
            return await SendCommandAsync(
                IGPDesktopSessionCommandType.GetDesktopUserProfile,
                cancellationToken,
                throwOnFailure: false);
        }

        public async Task<IGPDesktopSessionCommandResult> GetDesktopPlayerProfilesAsync(
            string contentJson,
            CancellationToken cancellationToken = default)
        {
            return await SendCommandAsync(
                IGPDesktopSessionCommandType.GetDesktopPlayerProfiles,
                cancellationToken,
                contentJson: contentJson,
                throwOnFailure: false);
        }

        public async Task<IGPDesktopUserAvatarImage> GetDesktopUserAvatarAsync(
            CancellationToken cancellationToken = default)
        {
            var result = await SendCommandAsync(
                IGPDesktopSessionCommandType.GetDesktopUserAvatar,
                cancellationToken);

            return new IGPDesktopUserAvatarImage
            {
                info = ParseCommandPayload(
                    result.ContentJson,
                    () => new IGPDesktopUserAvatarInfo()),
                pngBytes = result.ContentBytes ?? Array.Empty<byte>(),
            };
        }
    }
}
