<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\Localization\GuiTranslation;
use App\Model\Entities\Localization\Language;
use App\Model\Services\TranslationService;
use TypeError;

class TranslationReader extends BaseReader
{
    /** @var TranslationService */
    protected $translationService;
    /** @var LanguageReader */
    protected $languageReader;

    public function __construct(TranslationService $translationService, LanguageReader $languageReader)
    {
        $this->translationService = $translationService;
        $this->languageReader = $languageReader;
    }

    /**
     * @param Language $language
     * @param $identifier
     * @return GuiTranslation
     * @throws TypeError
     */
    public function findGuiTranslation($language, $identifier) : GuiTranslation
    {
        $languageEntity = $this->languageReader->validateLanguage($language);

        return $this->translationService->findGuiTranslation($languageEntity, $identifier);
    }

    /**
     * @param $id
     * @return GuiTranslation
     */
    public function findGuiTranslationById($id) : GuiTranslation
    {
        return $this->translationService->findGuiTranslationById($id);
    }

    /**
     * @param Language $language
     * @return GuiTranslation[]
     */
    public function getGuiTranslations(Language $language)
    {
        return $this->translationService->getGuiTranslations($language);
    }

    public function getGuiTranslationsAssoc(Language $language)
    {
        return $this->translationService->getGuiTranslationAssoc($language);
    }

    public function countGuiTranslations($language, $state = null)
    {
        $languageEntity = $this->languageReader->validateLanguage($language);
        $criteria = ['language' => $languageEntity];
        if ($state !== null) {
            $criteria['state'] = $state;
        }
        return $this->translationService->countGuiTranslations($criteria);
    }

    public function getPendingTranslationsAssoc($language)
    {
        $languageEntity = $this->languageReader->validateLanguage($language);

        return $this->translationService->getPendingTranslationsAssoc($languageEntity);
    }

    public function getEnglishTranslations(array $identifiers)
    {
        $eng = $this->languageReader->getDefaultLanguage();

        return $this->translationService->geTranslationsByIdentifiers($eng, $identifiers);
    }
}