<?php
use DI\Container;
use Psr\Container\ContainerInterface;
use Slim\Views\Twig;
use App\View\Pages;
// Обработчики запросов
use App\Actions\MainAction;
use App\Actions\Session\SessionAction;
use App\Actions\Session\CreateSessionAction;
use App\Actions\Session\GetSessionListAction;
use App\Actions\Session\ReportAction;
use App\Actions\Config\ConfigAction;
use App\Actions\Phrase\PhraseAction;
use App\Actions\Phonebook\BookAction;
use App\Actions\Phonebook\GroupAction;
use App\Actions\OperatorAction;
use App\Actions\MediaAction;
use App\Actions\LoginAction;
// Серсисные классы
use App\Services\ApiService;
use App\Services\LdapService;


$container = new Container();


// Регистрация Twig (View)
$container->set('view', function (ContainerInterface $c) {
    return Twig::create('./public', ['cache' => false]);
});

// Регистрация класса для рендера стриниц
$container->set(Pages::class, function (ContainerInterface $c) {
    $view = $c->get('view');
    return new Pages($view);
});


// Регестрируем параметры конфигурации
$container->set('settings',function(ContainerInterface $c) {
    
    try {
	require '/persist/etc/casper/config.php';
	$config_path = "/persist/etc/casper/";
    } catch (Error $ex) {
	require_once __DIR__ . '/config.php';
	$config_path = __DIR__ . "/";
    }
    
    
    return ['ldap' => $config['ldap'],
            'media' => $config['media'],
            'rest' => $config['rest'],
            'defaults' => $config['defaults'],
            'call_status' => $config['call_status'],
            'config_path' => $config_path
    ];
});


////////////////////////////////////////////////////////////////////////////////
// Регистрация Serice обработчиков
////////////////////////////////////////////////////////////////////////////////
$container->set(ApiService::class, function (ContainerInterface $c) {
    return new ApiService(
        $c->get('settings')['rest']
    );
});

$container->set(LdapService::class, function (ContainerInterface $c) {
    return new LdapService(
        $c->get('settings')['ldap']
    );
});

////////////////////////////////////////////////////////////////////////////////
// Регистрация Persistence обработчиков
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Регистрация Action обработчиков
////////////////////////////////////////////////////////////////////////////////
// Регистрируем контроллер главной страницы в контейнере
$container->set(MainAction::class, function (ContainerInterface $c) {
    return new MainAction(
        $c->get(ApiService::class),
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер страницы входа в систему в контейнере
$container->set(LoginAction::class, function (ContainerInterface $c) {
    return new LoginAction(
        $c->get(LdapService::class),
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер справочника групп в контейнере
$container->set(BookAction::class, function (ContainerInterface $c) {
    return new BookAction(
        $c->get(LdapService::class),
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер страницы групп в контейнере
$container->set(GroupAction::class, function (ContainerInterface $c) {
    return new GroupAction(
        $c->get(LdapService::class),
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер страницы операторов в контейнере
$container->set(OperatorAction::class, function (ContainerInterface $c) {
    return new OperatorAction(
        $c->get(LdapService::class),
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер управление сессиями в контейнере
$container->set(SessionAction::class, function (ContainerInterface $c) {
    return new SessionAction(
        $c->get('settings'),
	$c->get(LdapService::class),
	$c->get(ApiService::class),
	$c->get(Pages::class)
    );
});

$container->set(CreateSessionAction::class, function (ContainerInterface $c) {
    return new CreateSessionAction(
        $c->get('settings'),
	$c->get(ApiService::class),
	$c->get(Pages::class)
    );
});

$container->set(GetSessionListAction::class, function (ContainerInterface $c) {
    return new GetSessionListAction(
	$c->get(ApiService::class),
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер управление отчетом и архивами в контейнере
$container->set(ReportAction::class, function (ContainerInterface $c) {
    return new ReportAction(
        $c->get('settings'),
	$c->get(LdapService::class),
	$c->get(ApiService::class),
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер управление конфигурацией в контейнере
$container->set(ConfigAction::class, function (ContainerInterface $c) {
    return new ConfigAction(
        $c->get('settings'),
	$c->get(ApiService::class),
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер управления медиа файлами в контейнере
$container->set(MediaAction::class, function (ContainerInterface $c) {
    return new MediaAction(
        $c->get('settings'),
	$c->get(ApiService::class),
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер для страницы обновления в контейнере
$container->set(UpdateAction::class, function (ContainerInterface $c) {
    return new UpdateAction(
	$c->get(Pages::class)
    );
});

// Регистрируем контроллер для фраз в контейнере
$container->set(PhraseAction::class, function (ContainerInterface $c) {
    return new PhraseAction(
        $c->get('settings')['defaults']['phrase'],
	$c->get(Pages::class)
    );
});
