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

namespace Magediary\ProductQuestion\Block\Answer;

use Magediary\ProductQuestion\Api\QuestionRepositoryInterface;
use Magediary\ProductQuestion\Helper\Data;
use Magediary\ProductQuestion\Model\Answer;
use Magediary\ProductQuestion\Model\ResourceModel\Answer\Collection as AnswerCollection;
use Magediary\ProductQuestion\Model\ResourceModel\Answer\CollectionFactory;
use Magediary\ProductQuestion\Model\ResourceModel\Question\Collection;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class ListView extends Template
{
    /**
     * @var AnswerCollection
     */
    protected $_answersCollection;

    /**
     * @var QuestionRepositoryInterface
     */
    protected $_questionRepository;

    /**
     * Question resource model
     *
     * @var Collection Factory
     */
    protected $_questionsColFactory;

    /**
     * Answer resource model
     *
     * @var AnswerCollection Factory
     */
    protected $_answersColFactory;

    /**
     * @var Data|null
     */
    protected $_helperData = null;

    /**
     * @param Context $context
     * @param QuestionRepositoryInterface $questionRepository
     * @param CollectionFactory $answerCollectionFactory
     * @param Data $helperData
     * @param array $data
     */
    public function __construct(
        Context $context,
        QuestionRepositoryInterface $questionRepository,
        CollectionFactory $answerCollectionFactory,
        Data $helperData,
        array $data = []
    ) {
        $this->_questionRepository = $questionRepository;
        $this->_answersColFactory= $answerCollectionFactory;
        $this->_helperData = $helperData;
        parent::__construct($context, $data);
    }

    /**
     * Get question id by get parameter
     *
     * @return int|null
     */
    public function getQuestionId()
    {
        return $this->getRequest()->getParam('question_id');
    }

    /**
     * Get question object
     *
     * @return \Magediary\ProductQuestion\Api\Data\QuestionInterface
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function getQuestion()
    {
        $id = $this->getQuestionId();
        return $this->_questionRepository->getById($id);
    }

    /**
     * Get product id
     *
     * @return int
     */
    public function getProductId()
    {
        return $this->getRequest()->getParam('product_id', false);
    }

    /**
     * Get answer trigger URL for ajax call
     *
     * @return string
     */
    public function getAnswerTriggerUrl()
    {
        return $this->getUrl(
            'question/answer/trigger',
            [
                '_secure' => $this->getRequest()->isSecure(),
            ]
        );
    }

    /**
     * Get collection of answer
     *
     * @param int $questionId
     * @return mixed
     */
    public function getAnswersCollection()
    {
        if (null === $this->_answersCollection) {
            $this->_answersCollection = $this->_answersColFactory->create()
                ->addStatusFilter(
                    Answer::STATUS_APPROVED
                )->addQuestionFilter(
                    $this->getQuestionId()
                )->setDateOrder();
        }

        return $this->_answersCollection;
    }

    /**
     * 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');
    }

    /**
     * Prepare product answer list toolbar
     *
     * @return $this
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $toolbar = $this->getLayout()->getBlock('question_answer_list.toolbar');
        if ($toolbar) {
            $toolbar->setCollection($this->getAnswersCollection());
            $this->setChild('toolbar', $toolbar);
        }

        return $this;
    }
}
