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

namespace Magediary\ProductQuestion\Block\Customer;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Block\Account\Dashboard;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;

/**
 * Question Answer list common block
 *
 * @api
 */
class QuestionAnswer extends Dashboard
{
    /**
     * @var \Magediary\ProductQuestion\Model\ResourceModel\Question\Collection
     */
    protected $_questionCollection;

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

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

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

    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @var \Magento\Customer\Helper\Session\CurrentCustomer
     */
    protected $currentCustomer;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
     * @param CustomerRepositoryInterface $customerRepository
     * @param AccountManagementInterface $customerAccountManagement
     * @param \Magediary\ProductQuestion\Model\ResourceModel\Question\CollectionFactory $questionCollectionFactory
     * @param \Magediary\ProductQuestion\Model\ResourceModel\Answer\CollectionFactory $answerCollectionFactory
     * @param ProductRepositoryInterface $productRepository
     * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
        CustomerRepositoryInterface $customerRepository,
        AccountManagementInterface $customerAccountManagement,
        \Magediary\ProductQuestion\Model\ResourceModel\Question\CollectionFactory $questionCollectionFactory,
        \Magediary\ProductQuestion\Model\ResourceModel\Answer\CollectionFactory $answerCollectionFactory,
        ProductRepositoryInterface $productRepository,
        \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
        array $data = []
    ) {
        $this->_questionsColFactory = $questionCollectionFactory;
        $this->_answersColFactory= $answerCollectionFactory;
        $this->productRepository = $productRepository;

        parent::__construct(
            $context,
            $customerSession,
            $subscriberFactory,
            $customerRepository,
            $customerAccountManagement,
            $data
        );
        $this->currentCustomer = $currentCustomer;
    }

    /**
     * Get collection of questions
     *
     * @return \Magediary\ProductQuestion\Model\ResourceModel\Question\Collection
     */
    public function getQuestionsCollection()
    {
        if (!($customerId = $this->currentCustomer->getCustomerId())) {
            return false;
        }

        if (!$this->_questionCollection) {
            $this->_questionCollection = $this->_questionsColFactory->create()->addStoreFilter(
                $this->_storeManager->getStore()->getId()
            )->addStatusFilter(
                \Magediary\ProductQuestion\Model\Question::STATUS_APPROVED
            )->addCustomerFilter(
                $this->currentCustomer->getCustomerId()
            )->setDateOrder();
        }

        return $this->_questionCollection;
    }

    /**
     * Get collection of answers
     *
     * @return \Magediary\ProductQuestion\Model\ResourceModel\Answer\Collection
     */
    public function getAnswersCollection()
    {
        if (!($customerId = $this->currentCustomer->getCustomerId())) {
            return false;
        }

        if (!$this->_answerCollection) {
            $this->_answerCollection = $this->_answersColFactory->create()->addStatusFilter(
                \Magediary\ProductQuestion\Model\Answer::STATUS_APPROVED
            )->addCustomerFilter(
                $this->currentCustomer->getCustomerId()
            )->setDateOrder();
        }

        return $this->_answerCollection;
    }

    /**
     * Format date in short format
     *
     * @param string $date
     * @return string
     */
    public function dateFormat($date)
    {
        return $this->formatDate($date, \IntlDateFormatter::SHORT);
    }

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

    /**
     * Get product url by id
     *
     * @param int $id
     * @return null|string
     */
    public function getProductUrlById($id)
    {
        try {
            $productUrl = $this->productRepository->getById($id)
                ->getProductUrl();

        } catch (NoSuchEntityException $noSuchEntityException) {
            $productUrl = null;
        }

        return $productUrl;
    }
}
