using Google.Cloud.PubSub.V1;
using System.Threading.Tasks;
using Xunit;

[Collection(nameof(PubsubFixture))]
public class PullProtoMessagesAsyncTest
{
    private readonly PubsubFixture _pubsubFixture;
    private readonly PublishProtoMessagesAsyncSample _publishProtoMessagesAsyncSample;
    private readonly PullProtoMessagesAsyncSample _pullProtoMessagesAsyncSample;

    public PullProtoMessagesAsyncTest(PubsubFixture pubsubFixture)
    {
        _pubsubFixture = pubsubFixture;
        _publishProtoMessagesAsyncSample = new PublishProtoMessagesAsyncSample();
        _pullProtoMessagesAsyncSample = new PullProtoMessagesAsyncSample();
    }

    [Fact]
    public async Task PullProtoBinaryMessagesAsync()
    {
        string randomName = _pubsubFixture.RandomName();
        string topicId = $"testTopicForProtoBinaryMessageAck{randomName}";
        string subscriptionId = $"testSubscriptionForProtoBinaryMessageAck{randomName}";
        string schemaId = $"testSchemaForProtoBinaryMessageAck{randomName}";

        var schema = _pubsubFixture.CreateProtoSchema(schemaId);
        _pubsubFixture.CreateTopicWithSchema(topicId, schema.Name.ToString(), Encoding.Binary);
        _pubsubFixture.CreateSubscription(topicId, subscriptionId);

        await _publishProtoMessagesAsyncSample.PublishProtoMessagesAsync(_pubsubFixture.ProjectId, topicId, new Utilities.State[] { new Utilities.State { Name = "New York", PostAbbr = "NY" } });

        await _pubsubFixture.Pull.Eventually(async () =>
        {
            // Pull and acknowledge the messages
            var ackedCount = await _pullProtoMessagesAsyncSample.PullProtoMessagesAsync(_pubsubFixture.ProjectId, subscriptionId, true);
            Assert.Equal(1, ackedCount);
        });

        //Pull the Message to confirm it's gone after it's acknowledged
        var result = await _pullProtoMessagesAsyncSample.PullProtoMessagesAsync(_pubsubFixture.ProjectId, subscriptionId, true);
        Assert.Equal(0, result);
    }

    [Fact]
    public async Task PullProtoJsonMessagesAsync()
    {
        string randomName = _pubsubFixture.RandomName();
        string topicId = $"testTopicForProtoJsonMessageAck{randomName}";
        string subscriptionId = $"testSubscriptionForProtoJsonMessageAck{randomName}";
        string schemaId = $"testSchemaForProtoJsonMessageAck{randomName}";

        var schema = _pubsubFixture.CreateProtoSchema(schemaId);
        _pubsubFixture.CreateTopicWithSchema(topicId, schema.Name.ToString(), Encoding.Json);
        _pubsubFixture.CreateSubscription(topicId, subscriptionId);

        await _publishProtoMessagesAsyncSample.PublishProtoMessagesAsync(_pubsubFixture.ProjectId, topicId, new Utilities.State[] { new Utilities.State { Name = "New York", PostAbbr = "NY" } });

        await _pubsubFixture.Pull.Eventually(async () =>
        {
            // Pull and acknowledge the messages
            var ackedCount = await _pullProtoMessagesAsyncSample.PullProtoMessagesAsync(_pubsubFixture.ProjectId, subscriptionId, true);
            Assert.Equal(1, ackedCount);
        });

        //Pull the Message to confirm it's gone after it's acknowledged
        var result = await _pullProtoMessagesAsyncSample.PullProtoMessagesAsync(_pubsubFixture.ProjectId, subscriptionId, true);
        Assert.Equal(0, result);
    }
}
