<?php

namespace KupShop\SellerBundle\View;

use KupShop\ComponentsBundle\View\ComponentsViewInterface;
use KupShop\ContentBundle\Entity\Page;
use KupShop\KupShopBundle\Views\View;
use KupShop\SellerBundle\Twig\ComponentsTemplateOverride;
use KupShop\SellerBundle\Utils\SellerUtil;
use KupShop\SellerBundle\Utils\TypeProvider;

class MapView extends View implements ComponentsViewInterface
{
    use ComponentsTemplateOverride;

    protected $template = 'seller/seller.tpl';
    protected $title = 'Prodejci';

    /** @var SellerUtil */
    protected $sellerUtil;

    /** @var TypeProvider */
    protected $typeProvider;

    /** @required */
    public function setSellerUtil(SellerUtil $sellerUtil): void
    {
        $this->sellerUtil = $sellerUtil;
    }

    /** @required */
    public function setTypeProvider(TypeProvider $typeProvider): void
    {
        $this->typeProvider = $typeProvider;
    }

    public function getTitle()
    {
        return translate('title', 'sellers');
    }

    /** @var Page */
    protected $page;

    public function getWpjToolbar()
    {
        return array_merge(parent::getWpjToolbar(), [
            'url' => getAdminUrl('sellers', ['ID' => getVal('ID')]),
            'title' => 'Úprava prodejce',
        ]);
    }

    public function getBodyVariables()
    {
        $body = parent::getBodyVariables();

        $body['types'] = $this->typeProvider->getTypes();
        $body['dealers'] = $this->sellerUtil->getSellers();
        $body['dealersByTypes'] = [];

        foreach ($body['dealers'] as $seller) {
            $body['dealersByTypes'][$seller['type']][] = $seller;
        }

        return $body;
    }
}
