<?php


namespace App\Model\Facades\Writers;


use App\Model\Entities\Localization\Language;
use App\Model\Entities\Security\User;
use App\Model\Exceptions\Translator\TranslationAlreadyExistsException;
use App\Model\Facades\Readers\LanguageReader;
use App\Model\Facades\Readers\TranslationReader;
use App\Model\Interfaces\ITranslation;
use App\Model\Services\TranslationService;
use app\model\services\TranslatorApiService;
use Throwable;

class TranslationWriter extends BaseWriter
{
    /** @var TranslationService */
    protected $translationService;
    /** @var TranslationReader */
    protected $translatorReader;
    /** @var  LanguageReader */
    protected $languageReader;
    /** @var  TranslatorApiService */
    protected $translatorApiService;

    public function inject(TranslationService $translationService, TranslationReader $translatorReader, LanguageReader $languageReader, TranslatorApiService $translatorApiService)
    {
        $this->translationService = $translationService;
        $this->translatorReader = $translatorReader;
        $this->languageReader = $languageReader;
        $this->translatorApiService = $translatorApiService;
    }

    public function createGuiTranslation($identifier, Language $language)
    {
        $translation = $this->translationService->findGuiTranslation($language, $identifier);
        if ($translation) {
            throw new TranslationAlreadyExistsException();
        }

        try {
            $this->beginTransaction();
            $translation = $this->translationService->createGuiTranslation($identifier, $language);
            if ($language->getIdentifier() === $this->languageReader->getDefaultLanguage()->getIdentifier()) {
                $translation->setTranslation($identifier);
            } else {
                $translation->setTranslation($this->translatorApiService->translate('en', $language->getIdentifier(), $identifier));
                $translation->setState(ITranslation::BY_TRANSLATOR);
            }
            $this->commit();
        } catch (Throwable $e) {
            $this->rollback();
            throw $e;
        }

        return $translation;
    }

    public function translate(ITranslation $translation, $text, User $user)
    {
        try {
            $this->beginTransaction();
            $translation = $this->translationService->translate($translation, $text, $user);
            $this->commit();
        } catch (Throwable $e) {
            $this->rollback();
            throw $e;
        }

        return $translation;
    }

//    public function autoTranslate($lang)
//    {
//        $language = $this->languageReader->validateLanguage($lang);
//        $pendingTranslations = $this->translatorReader->getPendingTranslationsAssoc($language);
//        if (empty($pendingTranslations)) {
//            return;
//        }
//
//        $eng = $this->translatorReader->getEnglishTranslations(array_keys($pendingTranslations));
//        $xy = $this->translatorApiService->translate('en', $language->getIdentifier(), $eng);
//        // result is too large, this wont work
//
//        try {
//            $this->beginTransaction();
//            foreach ($pendingTranslations as $translation) {
//                $translation;
//            }
//            $this->commit();
//        } catch (Throwable $e) {
//            $this->rollback();
//            throw $e;
//        }
//    }


}