<?php
/**
 * Created by PhpStorm.
 * User: eater
 * Date: 3/29/16
 * Time: 10:21 PM
 */

namespace Eater\Glim\Handler;


use Eater\Glim\Service\TwigVars;
use Interop\Container\ContainerInterface;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use Eater\Glim\Core;
use Eater\Glim\Helper\EasyContainer;

abstract class Main implements ContainerInterface
{
    use EasyContainer;

    /**
     * @var Core
     */
    protected $core;

    /**
     * @var Request
     */
    protected $request;

    /**
     * @var Response
     */
    protected $response;

    /**
     * Main constructor.
     * @param Core $core
     * @param Request $request
     * @param Response $response
     * @param Container $container
     */
    public function __construct(Core $core, Request $request, Response $response, Container $container)
    {
        $this->core = $core;
        $this->setRequest($request);
        $this->setResponse($response);
        $this->setContainer($container);
    }

    /**
     * @return Core
     */
    protected function getCore()
    {
        return $this->core;
    }

    /**
     * @return Response|void
     */
    public function beforeHandle()
    {
    }

    /**
     * @return Response
     */
    public function handle()
    {
        return $this->getResponse();
    }

    /**
     * @return Response
     */
    public function getResponse()
    {
        return $this->response;
    }

    /**
     * @param Response $response
     */
    public function setResponse($response)
    {
        $this->response = $response;
    }

    /**
     * @return Request
     */
    public function getRequest()
    {
        return $this->request;
    }

    /**
     * @param Request $request
     */
    public function setRequest($request)
    {
        $this->request = $request;
    }

    /**
     * @param string $template
     * @param array $context
     * @return Response
     */
    public function render($template, $context = [])
    {
        $core = $this->getCore();
        $twig = $core->getTwig();
        /** @var TwigVars */
        $twigVars = $this->get('twig-vars');

        return $this->getResponse()->write($twig->render($template, $twigVars->getVars() + $context));
    }

    /**
     * @param string $name
     * @return string
     */
    public function post($name)
    {
        $request = $this->getRequest();
        return $request->getParsedBodyParam($name);
    }

    public function redirect($url)
    {
        $response = $this->getResponse();
        return $response->withRedirect($url);
    }

    /**
     * @param array $array
     * @return Response
     */
    public function json($array)
    {
        return $this->getResponse()
            ->withHeader('Content-Type', 'application/json')
            ->write(json_encode($array));
    }

    /**
     * @param string $name
     * @return string
     */
    public function attr($name)
    {
        return $this->getRequest()->getAttribute($name);
    }
}