<?php
/**
 * User: Rafał Ossowski
 * Date: 11.01.18
 * Time: 13:39
 */

namespace ModulesGarden\HostingQuotaNotifications\Core\Http;


use ModulesGarden\HostingQuotaNotifications\Core\DependencyInjection;
use ModulesGarden\HostingQuotaNotifications\Core\UI\View;
use ModulesGarden\HostingQuotaNotifications\Core\ModuleConstants;

class AbstractHooksClient extends AbstractController
{
    protected $controller;
    protected $action;
    /**
     * @return string
     */
    public function getControllerClass()
    {
        return 'ModulesGarden\HostingQuotaNotifications\App\Http\Client\Hooks\\' . ucfirst($this->getController());
    }

    /**
     * @return string
     */
    public function getController()
    {
        return $this->getParamByKey('filename', null);
    }

    /**
     * @return string
     */
    public function getAction()
    {
        return str_replace('-', '', $this->getParamByKey('templatefile', ''));
    }

    public function execute()
    {
        $this->loadContectLang();

        try
        {

            $this->getSmarty()
                ->setLang($this->lang)
                ->setTemplateDir($this->getTemplateDir());

            $this->lang->stagCurrentContext('generate' . $this->getController());
            $this->lang->addToContext(lcfirst($this->getController()));
            $result = DependencyInjection::create(
                $this->getControllerClass(),
                $this->getAction()
            );

            if ($result)
            {
                if ($context = $this->showPage($result))
                {
                    return $context;
                }
            }
        }
        catch (\ModulesGarden\HostingQuotaNotifications\Core\HandlerError\Exceptions\MGModuleException $exc)
        {

        }
        catch (\Exception $exc)
        {
            $newExc = new \ModulesGarden\HostingQuotaNotifications\Core\HandlerError\Exceptions\MGModuleException(self::class, $exc->getMessage(), $exc->getCode());
        }

        return null;
    }

    /**
     *
     * @param Response $result
     */
    protected function showPage($result)
    {
        if ($result instanceof View)
        {
            /**
             * @var View $result
             */
            $result->validateAcl($this->isAdmin);
            $result = $result->genResponse();
        }

        $result->setLang($this->lang);
        $this->getSmarty()->setTemplateDir($this->getTemplateDir(true));

        if ($result instanceof JsonResponse)
        {
            $result->send();
            die();
        }
        elseif ($result instanceof RedirectResponse)
        {
            $result->send();
            die();
        }
        elseif ($result instanceof Response)
        {
            return $result->loadlayoutHooksContenct(
                $this->getPageContext($result), $this->getController(), $this->getAction(), $this->getTemplateDir()
            )
                ->getContent();
        }
    }

    public function getTemplateDir($withController = false, $fullpathWithClient = false)
    {
        $root = ((!$this->isAdmin || $fullpathWithClient) ? (ModuleConstants::getTemplateDir() . DS) : '');

        $return = $root . strtolower($this->getControllerType())
            . (($this->isAdmin) ? '' : (DS . $this->getClientTemplate()))
            . (($withController) ? ( DS . 'pages' . DS . lcfirst($this->getController())) : '');

        return $return;
    }
}