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

namespace Magediary\ProductQuestion\Block\Customer;

use Magediary\ProductQuestion\Model\Data\Question;
use Magento\Framework\View\Element\AbstractBlock;

/**
 * Product Questions list block
 *
 * @api
 */
class ListQuestion extends QuestionAnswer
{
    /**
     * Initializes toolbar
     *
     * @return AbstractBlock
     */
    protected function _prepareLayout()
    {
        if ($this->getQuestionsCollection()) {
            $toolbar = $this->getLayout()->createBlock(
                \Magento\Theme\Block\Html\Pager::class,
                'customer_question_list.toolbar'
            )->setPageVarName(
                'pq'
            )->setLimitVarName(
                'lq'
            )->setCollection(
                $this->getQuestionsCollection()
            );

            $this->setChild('question.toolbar', $toolbar);
        }
        return parent::_prepareLayout();
    }

    /**
     * Get html code for toolbar
     *
     * @return string
     */
    public function getToolbarHtml()
    {
        return $this->getChildHtml('question.toolbar');
    }

    /**
     * Get product URL
     *
     * @param Question $question
     * @return string
     */
    public function getProductUrl($question)
    {
        return $this->getProductUrlById($question->getProductId());
    }
}
