<?php
/**
 * @author  Magediary
 * @package Magediary_ProductQuestion
 */

namespace Magediary\ProductQuestion\Block\Product;

use Magediary\ProductQuestion\Model\Answer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magediary\ProductQuestion\Model\ResourceModel\Question\Collection as QuestionCollection;

class View extends \Magento\Catalog\Block\Product\View
{
    /**
     * @var QuestionCollection
     */
    protected $_questionsCollection;

    /**
     * @var \Magediary\ProductQuestion\Model\ResourceModel\Question\Collection Factory
     */
    protected $_questionsColFactory;

    /**
     * @var \Magediary\ProductQuestion\Model\ResourceModel\Answer\Collection Factory
     */
    protected $_answersColFactory;

    /**
     * @var \Magediary\ProductQuestion\Api\QuestionRepositoryInterface
     */
    protected $_questionRepository;

    /**
     * @var \Magediary\ProductQuestion\Helper\Data|null
     */
    protected $_helperData = null;

    /**
     * @param \Magento\Catalog\Block\Product\Context $context
     * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
     * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
     * @param \Magento\Framework\Stdlib\StringUtils $string
     * @param \Magento\Catalog\Helper\Product $productHelper
     * @param \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig
     * @param \Magento\Framework\Locale\FormatInterface $localeFormat
     * @param \Magento\Customer\Model\Session $customerSession
     * @param ProductRepositoryInterface $productRepository
     * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
     * @param \Magediary\ProductQuestion\Model\ResourceModel\Question\CollectionFactory $questionCollectionFactory
     * @param \Magediary\ProductQuestion\Model\ResourceModel\Answer\CollectionFactory $answerCollectionFactory
     * @param \Magediary\ProductQuestion\Api\QuestionRepositoryInterface $questionRepository
     * @param \Magediary\ProductQuestion\Helper\Data $helperData
     * @param array $data
     */
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Url\EncoderInterface $urlEncoder,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\Framework\Stdlib\StringUtils $string,
        \Magento\Catalog\Helper\Product $productHelper,
        \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
        \Magento\Framework\Locale\FormatInterface $localeFormat,
        \Magento\Customer\Model\Session $customerSession,
        ProductRepositoryInterface $productRepository,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        \Magediary\ProductQuestion\Model\ResourceModel\Question\CollectionFactory $questionCollectionFactory,
        \Magediary\ProductQuestion\Model\ResourceModel\Answer\CollectionFactory $answerCollectionFactory,
        \Magediary\ProductQuestion\Api\QuestionRepositoryInterface $questionRepository,
        \Magediary\ProductQuestion\Helper\Data $helperData,
        array $data = []
    ) {
        $this->_questionsColFactory = $questionCollectionFactory;
        $this->_answersColFactory= $answerCollectionFactory;
        $this->_questionRepository = $questionRepository;
        $this->_helperData = $helperData;

        parent::__construct(
            $context,
            $urlEncoder,
            $jsonEncoder,
            $string,
            $productHelper,
            $productTypeConfig,
            $localeFormat,
            $customerSession,
            $productRepository,
            $priceCurrency,
            $data
        );
    }

    /**
     * Render block HTML
     *
     * @return string
     */
    protected function _toHtml()
    {
        $this->getProduct()->setShortDescription(null);

        return parent::_toHtml();
    }

    /**
     * Get collection of questions
     *
     * @return QuestionCollection
     */
    public function getQuestionsCollection()
    {
        if (null === $this->_questionsCollection) {
            $this->_questionsCollection = $this->_questionsColFactory->create()->addStoreFilter(
                $this->_storeManager->getStore()->getId()
            )->addStatusFilter(
                \Magediary\ProductQuestion\Model\Question::STATUS_APPROVED
            )->addProductFilter(
                $this->getProduct()->getId()
            )->setDateOrder();

            $searchTerm = $this->getRequest()->getParam('search_term');
            if (!empty($searchTerm)) {
                $this->_questionsCollection->addFieldToFilter(
                    'detail',
                    ['like' => '%' . $searchTerm . '%']
                );
            }
        }

        return $this->_questionsCollection;
    }

    /**
     * Get collection of answers
     *
     * @param int $questionId
     * @return mixed
     */
    public function getAnswersCollection($questionId)
    {
        $answersCollection = $this->_answersColFactory->create()->addStatusFilter(
            Answer::STATUS_APPROVED
        )->addQuestionFilter(
            $questionId
        )->setDateOrder()
        ->setPageSize(1)
        ->load();

        return $answersCollection->getFirstItem();
    }

    /**
     * Force product view page behave like without options
     *
     * @return bool
     */
    public function hasOptions()
    {
        return false;
    }

    /**
     * Get config value
     *
     * @return bool
     */
    public function allowLike()
    {
        return $this->_helperData->getConfig('question/answer/enable_like');
    }

    /**
     * Get config value
     *
     * @return bool
     */
    public function allowDislike()
    {
        return $this->_helperData->getConfig('question/answer/enable_dislike');
    }

    /**
     * Get config value
     *
     * @return bool
     */
    public function allowReportAbuse()
    {
        return $this->_helperData->getConfig('question/report_abuse/enabled');
    }
}
