<?php

namespace Concrete\Core\Board\Instance\Slot;

use Concrete\Core\Block\Block;
use Concrete\Core\Block\View\BlockView;
use Concrete\Core\Board\Instance\Slot\Menu\Manager;
use Concrete\Core\Filesystem\FileLocator;
use Concrete\Core\Application\Application;
use Concrete\Core\Filesystem\TemplateService;

class SlotRenderer
{

    /**
     * @var FileLocator
     */
    protected $fileLocator;

    /**
     * @var RenderedSlotCollection
     */
    protected $renderedSlotCollection;

    /**
     * @var bool
     */
    protected $enableEditing = false;

    /**
     * @var Application
     */
    protected $app;

    /**
     * @param bool $enableEditing
     */
    public function setEnableEditing($enableEditing)
    {
        $this->enableEditing = $enableEditing;
    }

    public function __construct(Application $app, FileLocator $fileLocator, RenderedSlotCollection $renderedSlotCollection)
    {
        $this->app = $app;
        $this->fileLocator = $fileLocator;
        $this->renderedSlotCollection = $renderedSlotCollection;
    }

    /**
     * Returns true if the slot has a block inside it.
     *
     * @param int $slot
     */
    public function hasContents(int $slot)
    {
        $slot = $this->renderedSlotCollection->getRenderedSlot($slot);
        if ($slot) {
            return $slot->getBlockID() > 0;
        }
        return false;
    }

    public function display(int $slot)
    {
        $slot = $this->renderedSlotCollection->getRenderedSlot($slot);
        if ($slot) {
            $templateService = $this->enableEditing ? app(TemplateService::class) : null;
            $block = Block::getByID($slot->getBlockID());
            if ($this->enableEditing) {
                $menuManager = $this->app->make(Manager::class);
                $menu = $menuManager->getMenu($slot);

                $header = $this->fileLocator->getRecord(DIRNAME_ELEMENTS . '/' . DIRNAME_BOARDS . '/slot_header.php')
                    ->getFile();
                echo $templateService->renderTemplate($header, get_defined_vars(), $this);
            }

            if ($block) {
                $view = new BlockView($block);
                $view->render();
            }

            if ($this->enableEditing) {
                $footer = $this->fileLocator->getRecord(DIRNAME_ELEMENTS . '/' . DIRNAME_BOARDS . '/slot_footer.php')
                    ->getFile();
                echo $templateService->renderTemplate($footer, get_defined_vars(), $this);
            }
        }

    }

    public function render(int $slot): string
    {
        ob_start();
        $this->display($slot);
        return ob_get_clean();
    }
}

