<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_GdprCompliance
 * @author    Webkul <support@webkul.com>
 * @copyright Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html ASL Licence
 * @link      https://store.webkul.com/license.html
 */

namespace Webkul\GdprCompliance\Model;

if (!defined('DS')) {
    define("DS", DIRECTORY_SEPARATOR);
}
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Webkul\MobikulCore\Helper\Data as HelperData;

abstract class ApiHandler
{
    protected $returnArray = [];
    protected $wholeData;

    /**
     * Constructer method.
     *
     * @param \Webkul\MobikulCore\Helper\Data       $helper     helper
     * @param \Magento\Framework\App\Action\Context $context    context
     * @param \Magento\Framework\Json\Helper\Data   $jsonHelper jsonHelper
     */
    public function __construct(
        \Magento\Framework\Json\Helper\Data $jsonHelper,
        \Magento\Framework\Webapi\Rest\Response $response,
        \Magento\Framework\Webapi\Rest\Request $request,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        HelperData $helper
    ) {
        $this->jsonHelper = $jsonHelper;
        $this->_response = $response;
        $this->request = $request;
        $this->_objectManager = $objectManager;
        $this->helper = $helper;
        $this->returnArray["success"] = false;
        $this->returnArray["message"] = "";
        $this->dispatch();
    }

    /**
     * Get config value by key.
     *
     * @param RequestInterface $request request
     *
     * @return $request
     */
    public function dispatch()
    {
        $this->headers = $this->request->getHeaders();
        $returnArray = [];
        $returnArray["success"] = false;
        $returnArray["message"] = "";
        $authKey = $this->request->getHeader("authKey");
        $apiAuthType = $this->request->getHeader("apiAuthType");
        $authData = $this->helper->isAuthorized($authKey);
        if ($authData["code"] != 1) {
            return $this->getResponse($returnArray, 401, $authData["token"]);
        }
    }

    public function getRequest()
    {
        return $this->request;
    }

    /**
     * Return json response.
     *
     * @param array  $responseContent response content
     * @param string $responseCode    response code
     * @param string $token           token
     *
     * @return Webkul\GdprCompliance\Api\Data\ResponseInterface
     */
    protected function getResponse($responseContent = [], $responseCode = 200, $token = "")
    {
        if ($responseCode == 401) {
            $this->_response->setHeader('token', $token, true);
            throw new \Magento\Framework\Webapi\Exception(__("Unauthorized access."), 401, 401);
        }
        return $this->getJsonResponse($responseContent);
    }


    /**
     * getJsonResponse returns json response.
     *
     * @param array $responseContent
     *
     * @return Webkul\GdprCompliance\Api\Data\ResponseInterface
     */
    protected function getJsonResponse($responseContent = [])
    {
        $res = $this->_objectManager->get("\Webkul\GdprCompliance\Api\Data\ResponseInterface");
        $res->setSuccess($responseContent['success']);
        $res->setMessage($responseContent['message']);
        return $res;
    }
}
