<?php
namespace Skeleton\Controller\Traits;

use API\Partenaire\Service\PartenaireService;
use DI\Container;
use Helpers\ExportTo\EmailCsvService;
use Helpers\Trad\ITrad;
use Helpers\Trad\Trad;
use Helpers\Validator\FormValidator;
use Helpers\Validator\Validator;
use Psr\SimpleCache\CacheInterface;

trait ControllerShorthand
{
    abstract public function getContainer() :Container;
    abstract public function getPdo() :\PDO;

    public function getTranslationService() :ITrad
    {
        // return $this->contenaire->get(\Helpers\Trad\ITrad::class);
        // TODO: this is a quick fix, ITrad should be refreshed when $_SESSION['Session_Langue'] change
        $trad = $this->getContainer()->get(\Helpers\Trad\ITrad::class);
        $prefLang = Trad::getPreferedLang();
        if ($trad->getLangCode() != $prefLang) {
            $this->getContainer()->set(
                \Helpers\Trad\ITrad::class,
                $trad->copy($prefLang)
            );
        }
        return $trad;
    }

    public function getEmailCsvService() :EmailCsvService
    {
        return $this->contenaire->get(EmailCsvService::class);
    }
    
    public function getPartenaireService() :PartenaireService
    {
        return $this->contenaire->get(PartenaireService::class);
    }
    
    public function createValidator() :Validator
    {
        return new Validator(
            $this->getPdo(),
            $this->getTranslationService(),
            $this->contenaire
        );
    }
    public function createFormValidator() :FormValidator
    {
        return new FormValidator(
            $this->getPdo(),
            $this->getTranslationService(),
            $this->contenaire
        );
    }

    public function getServerCache() :CacheInterface
    {
        return $this->contenaire->get(CacheInterface::class);
    }

    public function getVersion():string
    {
        return $this->contenaire->get('version');
    }

    public function getDateVersion():string
    {
        return $this->contenaire->get('dateVersion');
    }

    public function getCopyright():string
    {
        return $this->contenaire->get('copyright');
    }

    public function getFullVersion():string
    {
        return $this->contenaire->get('fullVersion');
    }

    // protected function startAsyncProcess(Response $resp = null) :void
    // {
    //     ignore_user_abort(true);
    //     \Http\Response\send(
    //         ($resp ?? (new Response(202, ['Connection' => 'close'])))
    //     );
        
    //     session_write_close();
    //     error_log('ASYNC');
    //     $b = \fastcgi_finish_request();
    //     if ($b === false) {
    //         LogSysteme::prefixErrorLog('fastcgi_finish_request failed');
    //     }
    // }
}
