<?php
class ChairmanContainer {
    private $db;
    private $config;
    private $services = array();

    public function __construct(mysqli $db, array $config=array()) {
        $this->db = $db;
        $this->config = $config;
    }

    public function db(){ return $this->db; }
    public function config(){ return $this->config; }

    public function get($name) {
        if(isset($this->services[$name])) return $this->services[$name];

        switch($name){
            case 'health':
                $this->services[$name] = new ChairmanHealthService($this->db, $this->config);
                break;
            case 'race':
                $this->services[$name] = new ChairmanRaceService($this->db, $this->config);
                break;
            case 'cache':
                $this->services[$name] = new ChairmanCacheService($this->db, $this->config, $this->get('race'));
                break;
            case 'bridge':
                $this->services[$name] = new ChairmanBridgeService($this->db, $this->config, $this->get('race'));
                break;
            case 'events':
                $this->services[$name] = new ChairmanEventDispatcher();
                break;
            case 'raceRepo':
                $this->services[$name] = new RaceRepository($this->db);
                break;
            case 'optimizerRepo':
                $this->services[$name] = new OptimizerRepository($this->db);
                break;
            case 'rankingEngine':
                $this->services[$name] = new RankingEngine($this->get('raceRepo'), $this->get('events'));
                break;
            case 'decisionEngine':
                $this->services[$name] = new DecisionEngine();
                break;
            case 'cacheEngine':
                $this->services[$name] = new CacheEngine($this->get('cache'));
                break;
            case 'productionEngine':
                $this->services[$name] = new ProductionEngine($this->get('rankingEngine'), $this->get('decisionEngine'), $this->get('optimizerRepo'), $this->get('events'));
                break;
            default:
                throw new Exception('Unknown Chairman service: '.$name);
        }
        return $this->services[$name];
    }
}
