using Knoks.Api;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using System;
using System.IO;
using System.Net.Http;

namespace Knoks.Test.Integrations.Api.Tools
{
    public static class ApiTestServer
    {
        public const string ApiIndetifier = "138659EBEBBF408AA1282D46EBDFBDC7";
        public const string BaseAddress = "http://localhost:52281/v1/";

        public static HttpClient CreateClient()
        {
            var httpClient = TestServer.CreateClient();
            if (!string.IsNullOrEmpty(BaseAddress))
                httpClient.BaseAddress = new Uri(BaseAddress);

            return httpClient;
        }

        private static object obj = new object();
        private static TestServer _testServer;
        public static TestServer TestServer
        {
            get
            {
                if (_testServer == null)
                {
                    lock (obj)
                    {
                        if (_testServer == null)
                        {
                            var builder = new WebHostBuilder()
                                .UseContentRoot(Directory.GetCurrentDirectory())
                                .UseStartup(typeof(Startup));

                            _testServer = new TestServer(builder);
                        }
                    }
                }

                return _testServer;
            }
        }
    }
}
