<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Agorae
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\Agorae\Model\Plugin;

use Magento\Framework\Phrase;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\State;
use Magento\Framework\View\Asset\File;
use Magento\Framework\View\Asset\Source;
use Magento\Framework\Css\PreProcessor\File\Temporary;
use Magento\Framework\View\Asset\ContentProcessorException;
use Magento\Framework\View\Asset\ContentProcessorInterface;

use Webkul\Agorae\Helper\Data;

/**
 * Agorae Model Plugin Processor
 */
class Processor
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var State
     */
    private $appState;

    /**
     * @var Source
     */
    private $assetSource;

    /**
     * @var Temporary
     */
    private $temporaryFile;

    /**
     * @var Data
     */
    private $helper;

    /**
     * __construct
     * @param LoggerInterface $logger
     * @param State           $appState
     * @param Source          $assetSource
     * @param Temporary       $temporaryFile
     * @param Data            $helper
     */
    public function __construct(
        LoggerInterface $logger,
        State $appState,
        Source $assetSource,
        Temporary $temporaryFile,
        Data $helper
    ) {
        $this->logger = $logger;
        $this->appState = $appState;
        $this->assetSource = $assetSource;
        $this->temporaryFile = $temporaryFile;
        $this->helper = $helper;
    }
    /**
     * AroundProcessContent calls in place of processContent method
     *
     * @return string
     */
    public function aroundProcessContent(
        \Magento\Framework\Css\PreProcessor\Adapter\Less\Processor $subject,
        \Closure $proceed,
        $asset
    ) {
        $path = $asset->getPath();
        $lessArray = $this->helper->getModifiedArrayOfVariablesForLess();
        try {
            $parser = new \Less_Parser(
                [
                    'relativeUrls' => false,
                    'compress' => $this->appState->getMode() !== State::MODE_DEVELOPER
                ]
            );
            $content = $this->assetSource->getContent($asset);

            if (trim($content) === '') {
                return '';
            }
            $tmpFilePath = $this->temporaryFile->createFile($path, $content);
            $parser->parseFile($tmpFilePath, '');
            $parser->ModifyVars($lessArray);
            $content = $parser->getCss();
            if (trim($content) === '') {
                $errorMessage = PHP_EOL . ContentProcessorInterface::ERROR_MESSAGE_PREFIX . PHP_EOL . $path;
                $this->logger->critical($errorMessage);
                throw new ContentProcessorException(new Phrase($errorMessage));
            }
            return $content;
        } catch (\Exception $e) {
            $errorMessage = PHP_EOL
                . ContentProcessorInterface::ERROR_MESSAGE_PREFIX
                . PHP_EOL
                . $path
                . PHP_EOL
                . $e->getMessage();
            $this->logger->critical($errorMessage);
            throw new ContentProcessorException(new Phrase($errorMessage));
        }
    }
}
