using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtensiblePortfolioSite.SDK.Git
{
    internal class GitService
    {
        public String Service { get; }

        public GitService(String Service)
        {
            this.Service = Service;
        }

        private readonly List<IGitProvider> Providers = new();

        internal void removeProvider(IGitProvider provider)
        {
            lock (this.Providers)
                this.Providers.Remove(provider);
        }
        internal void addProvider(IGitProvider provider)
        {
            lock (this.Providers)
                this.Providers.Add(provider);
        }
        
        public IUser GetUser(Uri Path)
        {
            lock (this.Providers)
            {
                foreach(IGitProvider Provider in this.Providers)
                    if (Provider.TryGetUserByURL(Path, out IUser? User))
                        return User!;
                
                throw new GitObjectNotFoundException(GitReferenceKind.User, Path);
            }
        }
        public IRepository GetRepository(Uri Path)
        {
            lock (this.Providers)
            {
                foreach (IGitProvider Provider in this.Providers)
                    if (Provider.TryGetRepositoryByURL(Path, out IRepository? Repository))
                        return Repository!;

                throw new GitObjectNotFoundException(GitReferenceKind.User, Path);
            }
        }
    }
}
