using System;
using Castle.Windsor;
using IoCConcepts.Domain;
using IoCConcepts.Repositories;

namespace IoCConcepts {
    public interface ICustomerService {
        void EnrollCustomer(Customer customer);
    }

    public class CustomerService : ICustomerService {
        private readonly IWindsorContainer container;
        private readonly ICustomerValidator[] customerValidators;
        private readonly ICreditCheck creditCheck;
        private ICustomerRepository customerRepository;

        public CustomerService(
                ICustomerValidator[] customerValidators, 
                                ICreditCheck creditCheck, 
                                ICustomerRepository customerRepository) {
            this.container = container;
            this.customerValidators = customerValidators;
            this.creditCheck = creditCheck;
            this.customerRepository = customerRepository;
        }

        public void EnrollCustomer(Customer customer) {
            foreach(var customerValidator in customerValidators) {
                customerValidator.Validate(customer);
            }
            creditCheck.Check(customer);
            customerRepository.Save(customer);
        }
    }

    public class CreditCheck : ICreditCheck {
        public void Check(Customer customer) {
            throw new System.NotImplementedException();
        }
    }

    public interface ICreditCheck {
        void Check(Customer customer);
    }

    class AlwaysThrowsTimeoutExceptCreditCheck : ICreditCheck {
        public void Check(Customer customer) {
            throw new TimeoutException();
        }
    }

    public class CustomerValidator : ICustomerValidator {
        public void Validate(Customer customer) {
            throw new System.NotImplementedException();
        }
    }
}