<?php

/* * ********************************************************************************** */
/*      This file is part of the Artaban Marketplace package.                        */
/*                                                                                   */
/* * ********************************************************************************** */
/* * ********************************************************************************** */

namespace ArtabanMarketplace\Hook;

use ArtabanMarketplace\ArtabanMarketplace;
use Symfony\Component\Routing\Router;
use Thelia\Core\Event\Hook\HookRenderBlockEvent;
use Thelia\Core\Event\Hook\HookRenderEvent;
use Thelia\Core\Hook\BaseHook;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\SecurityContext;
use Thelia\Core\Translation\Translator;

/**
 * Class AdminInterfaceHook
 */
class AdminInterfaceHook extends BaseHook {

    protected $router;
    protected $securityContext;

    public function __construct(Router $router, SecurityContext $securityContext) {
        $this->router = $router;
        $this->securityContext = $securityContext;
    }

//    protected function transQuick($id, $locale, $parameters = [])
//    {
//        if ($this->translator === null) {
//            $this->translator = Translator::getInstance();
//        }
//
//        return $this->trans($id, $parameters, Dealer::MESSAGE_DOMAIN, $locale);
//    }

    public function onMainTopMenuTools(HookRenderEvent $event) {

        $isGrantedAdmin = $this->securityContext->isGranted(
                ["ADMIN"], ["admin.artabanmarketplace.administrator"], [ArtabanMarketplace::getModuleCode()], [AccessManager::VIEW]
        );

        $isGrantedSeller = $this->securityContext->isGranted(
                ["ADMIN"], ["admin.artabanmarketplace.owner"], [ArtabanMarketplace::getModuleCode()], [AccessManager::VIEW]
        );

        if ($isGrantedAdmin) {
            $event->add($this->render("menu-sellers-hook.html", $event->getArguments()));
        }

        if ($isGrantedSeller && !$isGrantedAdmin) {
            $event->add($this->render("menu-seller-customers-hook.html", $event->getArguments()));
            $event->add($this->render("menu-seller-orders-hook.html", $event->getArguments()));
            $event->add($this->render("menu-seller-catalog-hook.html", $event->getArguments()));
            $event->add($this->render("menu-seller-profil-hook.html", $event->getArguments()));
        }
    }

    public function infoForSeller(HookRenderBlockEvent $event) {
        $content = trim($this->render("block-information-for-seller.html"));
        if (!empty($content)) {
            $event->add([
                "id" => "block-info-for-seller",
                "content" => $content
            ]);
        }
    }

}
