<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Agorae
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\Agorae\Block\Seller;

/**
 * Adminhtml Agorae Block Seller Attributes
 */
class Attributes extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Webkul\Agorae\Helper\Data
     */
    protected $_helper;

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory
     */
    protected $_productAttributeCollection;

    /**
     * @var \Magento\Eav\Model\Entity\Attribute\Source\Boolean
     */
    protected $boolean;

    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $productFactory;

    /**
     * @param \Magento\Framework\View\Element\Template\Context                         $context
     * @param \Webkul\Agorae\Helper\Data                                               $helper
     * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $productAttribute
     * @param \Magento\Eav\Model\Entity\Attribute\Source\Boolean                       $boolean
     * @param \Magento\Catalog\Model\ProductFactory                                    $productFactory
     * @param array                                                                    $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Webkul\Agorae\Helper\Data $helper,
        \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $productAttribute,
        \Magento\Eav\Model\Entity\Attribute\Source\Boolean $boolean,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        array $data = []
    ) {
        $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $this->_helper = $helper;
        $this->_productAttributeCollection = $productAttribute;
        $this->boolean = $boolean;
        $this->productFactory = $productFactory;
        parent::__construct($context, $data);
    }

    /**
     * GetAgoraeSellerAttributes used to get seller attributes
     *
     * @return [boolean|object]
     */
    public function getAgoraeSellerAttributes()
    {
        $agoraeAttributes = $this->_helper->getAgoraeCustomProductAttributes();
        if (isset($agoraeAttributes['seller'])) {
            $attributes = $this->_productAttributeCollection->create()
                ->addVisibleFilter()
                ->addFieldToFilter(
                    'attribute_code',
                    ['in'=>$agoraeAttributes['seller']]
                )->load();
            if ($attributes->getSize()) {
                return $attributes;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * GetBooleanAttributeOption get boolean attribute values
     *
     * @return [array]
     */
    public function getBooleanAttributeOption()
    {
        return $this->boolean->getAllOptions();
    }

    /**
     * GetCurrentProduct get currenct product
     *
     * @return [boolean | object]
     */
    public function getCurrentProduct()
    {
        $productId = $this->getRequest()->getParam('id');
        if ($productId) {
            return $this->productFactory->create()->load($productId);
        } else {
            return false;
        }
    }
}
