#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<IGPDesktopHostedBootstrapContext> RequestHostedBootstrapAsync(
            int? appId = null,
            CancellationToken cancellationToken = default)
        {
            var payload = appId.HasValue && appId.Value > 0
                ? JsonConvert.SerializeObject(new
                {
                    appId = appId.Value,
                })
                : null;

            var result = await SendCommandAsync(
                IGPDesktopSessionCommandType.RequestHostedBootstrap,
                cancellationToken,
                contentJson: payload);

            if (string.IsNullOrWhiteSpace(result.ContentJson))
            {
                return new IGPDesktopHostedBootstrapContext();
            }

            return JsonConvert.DeserializeObject<IGPDesktopHostedBootstrapContext>(result.ContentJson)
                ?? new IGPDesktopHostedBootstrapContext();
        }
    }
}
