<?php


namespace App\Model\Facades\Writers;


use App\Model\Entities\Components\Navigation\NavItemForum;
use App\Model\Entities\Components\Navigation\NavItemGallery;
use App\Model\Entities\Components\Navigation\NavItemNews;
use App\Model\Facades\Readers\ForumReader;
use App\Model\Facades\Readers\GalleryReader;
use App\Model\Facades\Readers\NewsReader;
use App\Model\Facades\Readers\SiteReader;
use App\Model\Services\NavigationService;
use Throwable;

class NavigationWriter extends BaseWriter
{
    /** @var  NavigationService */
    protected $navigationService;
    /** @var  SiteReader */
    protected $siteReader;
    /** @var  NewsReader */
    protected $newsReader;
    /** @var  GalleryReader */
    protected $galleryReader;
    /** @var  ForumReader */
    protected $forumReader;

    public function inject(NavigationService $navigationService, SiteReader $siteReader,
                           NewsReader $newsReader, GalleryReader $galleryReader,
                           ForumReader $forumReader)
    {
        $this->navigationService = $navigationService;
        $this->siteReader = $siteReader;
        $this->newsReader = $newsReader;
        $this->galleryReader = $galleryReader;
        $this->forumReader = $forumReader;
    }

    public function attachNews($site, $newsContainer, string $label, string $icon = null) : NavItemNews
    {
        $siteEntity = $this->siteReader->validateSite($site);
        $containerEntity = $this->newsReader->validateContainer($newsContainer);

        try {
            $this->beginTransaction();
            $item = $this->navigationService->attachNews($siteEntity, $containerEntity, $label, $icon);
            if (!$this->navigationService->findHomepage($site)) {
                $item->setIsHomepage(true);
            }
            $this->commit();
        } catch (Throwable $e) {
            $this->rollback();
            throw $e;
        }

        return $item;
    }

    public function attachGallery($site, $gallery, string $label, string $icon = null) : NavItemGallery
    {
        $siteEntity = $this->siteReader->validateSite($site);
        $galleryEntity = $this->galleryReader->validateGallery($gallery);

        try {
            $this->beginTransaction();
            $item = $this->navigationService->attachGallery($siteEntity, $galleryEntity, $label, $icon);
            if (!$this->navigationService->findHomepage($site)) {
                $item->setIsHomepage(true);
            }
            $this->commit();
        } catch (Throwable $e) {
            $this->rollback();
            throw $e;
        }

        return $item;
    }

    public function attachForum($site, $forum, string $label, string $icon = null) : NavItemForum
    {
        $siteEntity = $this->siteReader->validateSite($site);
        $forumEntity = $this->forumReader->validateForum($forum);

        try {
            $this->beginTransaction();
            $item = $this->navigationService->attachForum($siteEntity, $forumEntity, $label, $icon);
            if (!$this->navigationService->findHomepage($site)) {
                $item->setIsHomepage(true);
            }
            $this->commit();
        } catch (Throwable $e) {
            $this->rollback();
            throw $e;
        }

        return $item;
    }
}