<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Faqs
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\Faqs\Block\Tag;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Phrase;
use Mageplaza\Faqs\Block\Article\ArticleList;
use Mageplaza\Faqs\Helper\Data;
use Mageplaza\Faqs\Model\Tag;
use Mageplaza\Faqs\Model\ResourceModel\Article\Collection;
use Mageplaza\Faqs\Model\ResourceModel\Tag\Collection as TagCollection;

/**
 * Class View
 *
 * @package Mageplaza\Faqs\Block\Tag
 * @method Tag getTag()
 * @method void setTag($tag)
 */
class View extends ArticleList
{
    /**
     * @var string
     */
    protected $_tag;

    /**
     * @inheritdoc
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        if ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs')) {
            $tag = $this->_getFaqsObject();

            if ($tag) {
                $breadcrumbs->addCrumb($tag->getUrlKey(), [
                    'label' => $tag->getName(),
                    'title' => $tag->getName()
                ]);
            }
        }
    }

    /**
     * @return mixed|string|null
     */
    protected function _getFaqsObject()
    {
        if (!$this->_tag) {
            $id = $this->getRequest()->getParam('id');
            if ($id) {
                $tag = $this->helperData->getObjectByParam($id, null, Data::TYPE_TAG);
                if ($tag && $tag->getId()) {
                    $this->_tag = $tag;
                    $this->setTag($tag);
                }
            }
        }

        return $this->_tag;
    }

    /**
     * Get tag detail page title
     *
     * @param bool $meta
     *
     * @return array|Phrase|string
     */
    public function getFaqsTitle($meta = false)
    {
        $faqsTitle = parent::getFaqsTitle($meta);
        $tag       = $this->_getFaqsObject();
        if (!$tag) {
            return $faqsTitle;
        }
        if ($meta) {
            if ($tag->getMetaTitle()) {
                array_push($faqsTitle, $tag->getMetaTitle());
            } else {
                array_push($faqsTitle, ucfirst($tag->getName()));
            }

            return $faqsTitle;
        }

        return ucfirst($tag->getName());
    }

    /**
     * Get limit question per category
     *
     * @return int
     */
    public function getLimit()
    {
        return ($this->helperData->getFaqsPageConfig('limit_question')) ?: 5;
    }

    /**
     * @return TagCollection|mixed
     */
    public function getTagCollection()
    {
        /** @var TagCollection $collection */
        $collection = parent::getTagCollection();

        if ($this->getIsFilter()) {
            $where = "AND `mpfa`.`name` LIKE '%" . $this->getIsFilter() . "%'";

            return $collection->getQuestionNum($where);
        }

        return $collection->getQuestionNum();
    }

    /**
     * @param int $tagId
     * @param null $where
     *
     * @return Collection
     * @throws NoSuchEntityException
     */
    public function getArticleByTag($tagId, $where = null)
    {
        if ($this->getIsFilter()) {
            $where = "AND `main_table`.`name` LIKE '%" . $this->getIsFilter() . "%'";

            return parent::getArticleByTag($tagId, $where);
        }

        return parent::getArticleByTag($tagId, $where);
    }

    /**
     * @return Data
     */
    public function getHelperData()
    {
        return $this->helperData;
    }
}
