<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\Templates\BaseTemplate;
use App\Model\Entities\Templates\EventTemplate;
use App\Model\Entities\Templates\ForumTemplate;
use App\Model\Entities\Templates\GalleryTemplate;
use App\Model\Entities\Templates\NewsTemplate;
use App\Model\Entities\Templates\SiteTemplate;
use App\Model\Interfaces\ITemplate;
use App\Model\Services\TemplateService;

class TemplateReader extends BaseReader
{
    /** @var TemplateService */
    protected $templateService;
    /** @var SiteReader */
    protected $siteReader;

    public function __construct(TemplateService $templateService, SiteReader $siteReader)
    {
        $this->templateService = $templateService;
        $this->siteReader = $siteReader;
    }

    public function findSiteTemplateById(int $id) : SiteTemplate
    {
        $template = $this->templateService->findTemplateById($id, SiteTemplate::class);
        $this->validateEntity($template);

        return $template;
    }

    public function getSiteSelectBoxPairs($site) : array
    {
        $siteEntity = $this->siteReader->validateSite($site);

        return $this->templateService->getTemplateSelectBoxPairs(SiteTemplate::class, $siteEntity);
    }

    public function validateSiteTemplate($template) : SiteTemplate
    {
        if ($template instanceof SiteTemplate) {
            return $template;
        }

        return $this->findSiteTemplateById($template);
    }

    public function findNewsTemplateById(int $id) : NewsTemplate
    {
        $template = $this->templateService->findTemplateById($id, NewsTemplate::class);
        $this->validateEntity($template);

        return $template;
    }

    public function getNewsSelectBoxPairs($site) : array
    {
        $siteEntity = $this->siteReader->validateSite($site);

        return $this->templateService->getTemplateSelectBoxPairs(NewsTemplate::class, $siteEntity);
    }

    public function validateNewsTemplate($template) : NewsTemplate
    {
        if ($template instanceof NewsTemplate) {
            return $template;
        }

        return $this->findNewsTemplateById($template);
    }

    public function findGalleryTemplateById(int $id) : GalleryTemplate
    {
        $template = $this->templateService->findTemplateById($id, GalleryTemplate::class);
        $this->validateEntity($template);

        return $template;
    }

    public function getGallerySelectBoxPairs($site) : array
    {
        $siteEntity = $this->siteReader->validateSite($site);

        return $this->templateService->getTemplateSelectBoxPairs(GalleryTemplate::class, $siteEntity);
    }

    public function validateGalleryTemplate($template)
    {
        if ($template instanceof GalleryTemplate) {
            return $template;
        }

        return $this->findGalleryTemplateById($template);
    }

    public function findForumTemplateById(int $id) : ForumTemplate
    {
        $template = $this->templateService->findTemplateById($id, ForumTemplate::class);
        $this->validateEntity($template);

        return $template;
    }

    public function getForumSelectBoxPairs($site) : array
    {
        $siteEntity = $this->siteReader->validateSite($site);

        return $this->templateService->getTemplateSelectBoxPairs(ForumTemplate::class, $siteEntity);
    }

    public function validateForumTemplate($template)
    {
        if ($template instanceof ForumTemplate) {
            return $template;
        }

        return $this->findForumTemplateById($template);
    }

    public function findEventTemplateById(int $id) : EventTemplate
    {
        $template = $this->templateService->findTemplateById($id, EventTemplate::class);
        $this->validateEntity($template);

        return $template;
    }

    public function getEventSelectBoxPairs($site) : array
    {
        $siteEntity = $this->siteReader->validateSite($site);

        return $this->templateService->getTemplateSelectBoxPairs(EventTemplate::class, $siteEntity);
    }

    public function validateEventTemplate($template) : EventTemplate
    {
        if ($template instanceof EventTemplate) {
            return $template;
        }

        return $this->findEventTemplateById($template);
    }

    public function findTemplateById($id) : ITemplate
    {
        $template = $this->templateService->findTemplateById($id, BaseTemplate::class);
        $this->validateEntity($template);

        return $template;
    }

    public function validateTemplate($template) : ITemplate
    {
        if ($template instanceof ITemplate) {
            return $template;
        }

        return $this->findTemplateById($template);
    }

}