<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\Game;
use App\Model\Entities\MmorpgGuild;
use App\Model\Entities\PortalSite;
use App\Model\Exceptions\Db\RecordNotFoundException;
use App\Model\Interfaces\ISite;
use App\Model\Services\SiteService;

class SiteReader extends BaseReader
{
    /** @var SiteService */
    protected $siteService;

    public function __construct(SiteService $siteService)
    {
        $this->siteService = $siteService;
    }

    /**
     * @param int $id
     * @return ISite
     * @throws RecordNotFoundException
     */
    public function findSiteById(int $id) : ISite
    {
        $site = $this->siteService->findSiteById($id);
        $this->validateEntity($site);

        return $site;
    }

    public function findPortalSite() : PortalSite
    {
        $site = $this->siteService->findPortalSite();
        $this->validateEntity($site);

        return $site;
    }

    public function getGuildsGridDataSource($game)
    {
        $game = $this->validateGame($game);

        return $this->siteService->getGuildsGridDataSource($game);
    }

    public function validateSite($site) : ISite
    {
        if ($site instanceof ISite) {
            return $site;
        }

        return $this->findSiteById($site);
    }

    public function validateGuild($guild) : MmorpgGuild
    {
        if ($guild instanceof MmorpgGuild) {
            return $guild;
        }

        return $this->findSiteById($guild);
    }

    public function validateGame($site) : Game
    {
        if ($site instanceof Game) {
            return $site;
        }

        return $this->findSiteById($site);
    }
}