﻿using Houston.Lib.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Houston.Backbone
{
    class BackboneService : ServiceRoot, IBackboneService
    {
        const string m_name = nameof(IBackboneService);
        Dictionary<string, List<ServiceInstance>> m_services;
        public override string GetName() => m_name;

        public BackboneService()
        {
            m_services = new Dictionary<string, List<ServiceInstance>>();
        }

        public IEnumerable<string> GetServices()
        {
            foreach (var collection in m_services)
                foreach (var service in collection.Value)
                {
                    yield return service.Uri;
                }
        }
        public string GetService(string name)
        {
            if (m_services == null || !m_services.ContainsKey(name))
                throw new InvalidOperationException("Backbone does not have an entry for that service.");
            return m_services[name][0].Uri;
        }
        public void RegisterService(string type, string uri) {
            if (!m_services.ContainsKey(type))
            {
                var serviceList = new List<ServiceInstance>();
                m_services[type] = serviceList;
            }
            var si = new ServiceInstance(uri);
            m_services[type].Add(si);
            Console.WriteLine($"Registered: {type} - {uri}");
        }
    }
}
