using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DigitalData.Core.Legacy.Client;
using Microsoft.Extensions.DependencyInjection;

namespace DigitalData.Core.Legacy.Tests.Client
{
    [TestClass]
    public class BaseHttpClientServiceTests
    {
        private BaseHttpClientService _service;

        [ClassInitialize]
        public static void ClassSetUp(TestContext context)
        {
            ServiceFactory.Services
                .AddHttpClientService("https://jsonplaceholder.typicode.com/todos")
                .BuildServiceProvider();
        }

        [TestInitialize]
        public void SetUp()
        {
            _service = ServiceFactory.Provide<BaseHttpClientService>();
        }

        [TestMethod]
        public async Task FetchJsonAsync_ShouldReturnJsonResponseAsDynamic()
        {
            // Act
            var expectedUserId = (int)await _service.FetchAsync("/1", sendWithCookie: false, saveCookie: false)
                .ThenAsync(res => res.Json())
                .ThenAsync(todo => todo.userId);

            // Assert
            Assert.AreEqual(1, expectedUserId, "The userId of the fetched JSON object should be 1.");
        }

        [TestMethod]
        public async Task FetchJsonAsync_ShouldReturnJsonResponse()
        {
            // Act
            var expectedUserId = await _service.FetchAsync("/1", sendWithCookie: false, saveCookie: false)
                .ThenAsync(res => res.Json<Todo>())
                .ThenAsync(todo => todo.UserId);

            // Assert
            Assert.AreEqual(1, expectedUserId, "The userId of the fetched JSON object should be 1.");
        }
    }

    public class Todo
    {
        public int UserId { get; set; }
        public int Id { get; set; }
        public string Title { get; set; }
        public bool Completed { get; set; }
    }

}