using System.Threading;
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Xunit;
using isnd.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Diagnostics;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Configuration;
using isnd.tests;
using NuGet.Protocol;
using NuGet.Configuration;
using System.Threading.Tasks;
using NuGet.Protocol.Core.Types;

namespace isnd.host.tests
{       
    [Collection("Web server collection")]
    public class UnitTestWebHost : IClassFixture<WebServerFixture>
    {
        const string apiindex = "/v3/index";
        WebServerFixture server;
        public UnitTestWebHost(WebServerFixture server)
        {
            this.server = server;
        }

        [Fact]
        public void TestHaveTestDbContextAndMigrate()
        {
            using (var serviceScope = server.Host.Services.CreateScope())
            {
                var services = serviceScope.ServiceProvider;
                var myDependency = services.GetRequiredService<ApplicationDbContext>();
                myDependency.Database.Migrate();
            }
        }

        [Fact]
        void TestDropUser()
        {
          
            using (var serviceScope = server.Host.Services.CreateScope())
            {
                var services = serviceScope.ServiceProvider;
                var dbContext = services.GetRequiredService<ApplicationDbContext>();
                var paul = dbContext.Users.FirstOrDefaultAsync
                (u => u.Email == "paul@pschneider.fr").Result;
                if (paul!=null)
                {
                    dbContext.Users.Remove(paul);
                    dbContext.SaveChanges();
                }
            }
        }


        [Fact]
        public void NugetInstallsTest()
        {
            using (var serviceScope = server.Host.Services.CreateScope())
            { var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
            string pkgSourceUrl = isnSettings.ExternalUrl + apiindex; 
            ProcessStartInfo psi = new ProcessStartInfo("nuget");
            psi.ArgumentList.Add("install");
            psi.ArgumentList.Add("gitversion");
            psi.ArgumentList.Add("-PreRelease");
            psi.ArgumentList.Add("-Source");
            psi.ArgumentList.Add(pkgSourceUrl);
            Process p = Process.Start(psi);
            p.WaitForExit();
            Assert.True(p.ExitCode == 0, "nuget install failed!");
            }
        }

        [Fact]
        public void TestRegistrationV3Resource()
        {
            using (var serviceScope = server.Host.Services.CreateScope())
            { var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
            string pkgSourceUrl = isnSettings.ExternalUrl + apiindex;
            NullThrottle throttle = new NullThrottle();

            PackageSource packageSource = new PackageSource(pkgSourceUrl);
            HttpSource client = new HttpSource(packageSource, PkgSourceMessageHandler, throttle);
            NuGet.Protocol.RegistrationResourceV3 res = new NuGet.Protocol.RegistrationResourceV3(client ,
            new Uri(isnSettings.ExternalUrl + "/v3/registration"));
            }
        }
        
        [Fact]
        public void TrueTestRegistrationV3Resource()
        {
            using (var serviceScope = server.Host.Services.CreateScope())
            { 
                var isnSettings = serviceScope.ServiceProvider.GetService<IOptions<isnd.Entities.IsndSettings>>().Value;
            string pkgSourceUrl = isnSettings.ExternalUrl + apiindex;
            var prov = new RegistrationResourceV3Provider();
            var source = new PackageSource(pkgSourceUrl);
            var repo = new SourceRepository(source, new INuGetResourceProvider[]{ prov });
            prov.TryCreate(repo, CancellationToken.None);
         
            }
        }

        private Task<HttpHandlerResource> PkgSourceMessageHandler()
        {
            throw new NotImplementedException();
        }
    }
}
