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

namespace Magediary\ProductQuestion\Block\Product;

use Magediary\ProductQuestion\Helper\Data;
use Magediary\ProductQuestion\Model\Question;
use Magediary\ProductQuestion\Model\ResourceModel\Question\Collection;
use Magediary\ProductQuestion\Model\ResourceModel\Question\CollectionFactory;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class Tab extends Template
{
    /**
     * @var Registry
     */
    protected $_coreRegistry;

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

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

    /**
     * @param Context $context
     * @param Registry $registry
     * @param CollectionFactory $collectionFactory
     * @param Data $helperData
     * @param array $data
     */
    public function __construct(
        Context $context,
        Registry $registry,
        CollectionFactory $collectionFactory,
        Data $helperData,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        $this->_questionsColFactory = $collectionFactory;
        $this->_helperData = $helperData;
        parent::__construct($context, $data);

        $this->setTabTitle();
    }

    /**
     * Get product id
     *
     * @return null|int
     */
    public function getProductId()
    {
        $product = $this->_coreRegistry->registry('product');
        return $product ? $product->getId() : null;
    }

    /**
     * Get question search enabled
     *
     * @return boolean
     */
    public function getEnableSearch()
    {
        return $this->_helperData->getConfig('question/question/enable_search');
    }

    /**
     * Get URL for ajax call
     *
     * @return string
     */
    public function getProductQuestionUrl()
    {
        return $this->getUrl(
            'question/product/listAjax',
            [
                '_secure' => $this->getRequest()->isSecure(),
                'id' => $this->getProductId(),
            ]
        );
    }

    /**
     * Set tab title
     *
     * @return void
     */
    public function setTabTitle()
    {
        $tab_title = $this->_helperData->getConfig('question/general/tab_title');

        $title = $this->getCollectionSize()
            ? __("$tab_title %1", '<span class="counter">' . $this->getCollectionSize() . '</span>')
            : __($tab_title);
        $this->setTitle($title);
    }

    /**
     * Get size of question collection
     *
     * @return int
     */
    public function getCollectionSize()
    {
        $collection = $this->_questionsColFactory->create()->addStoreFilter(
            $this->_storeManager->getStore()->getId()
        )->addStatusFilter(
            Question::STATUS_APPROVED
        )->addProductFilter(
            $this->getProductId()
        );

        return $collection->getSize();
    }

    /**
     * Return unique ID(s) for each object in system
     *
     * @return array
     */
    public function getIdentities()
    {
        return [Question::CACHE_TAG];
    }
}
