<?php
/**
 * Webkul Software.
 * 
 * PHP version 7.0+
 *
 * @category  Webkul
 * @package   Webkul_MobikulCore
 * @author    Webkul <support@webkul.com>
 * @copyright 2010-2019 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html ASL Licence
 * @link      https://store.webkul.com/license.html
 */
namespace Webkul\MobikulCore\Block\Adminhtml\Notification\Edit\Tab;

use Magento\Store\Model\Store;

/**
 * Class product Grid
 */
class productGrid extends \Magento\Backend\Block\Widget\Grid\Extended
{

    protected $type;
    protected $status;
    protected $visibility;
    protected $setsFactory;
    protected $moduleManager;
    protected $productFactory;
    protected $websiteFactory;
    protected $customCollectionNotification;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setsFactory
     * @param \Webkul\MobikulCore\Block\Adminhtml\Notification\Edit\Tab\CustomCollectionNotification $customCollectionNotification
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     * @param \Magento\Catalog\Model\Product\Type $type
     * @param \Magento\Catalog\Model\Product\Attribute\Source\Status $status
     * @param \Magento\Catalog\Model\Product\Visibility $visibility
     * @param \Magento\Framework\Module\Manager $moduleManager
     * @param array $data = [] 
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Store\Model\WebsiteFactory $websiteFactory,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setsFactory,
        \Webkul\MobikulCore\Block\Adminhtml\Notification\Edit\Tab\CustomCollectionNotification $customCollectionNotification,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Catalog\Model\Product\Type $type,
        \Magento\Catalog\Model\Product\Attribute\Source\Status $status,
        \Magento\Catalog\Model\Product\Visibility $visibility,
        \Magento\Framework\Module\Manager $moduleManager,
        array $data = []
    ) {
        $this->websiteFactory = $websiteFactory;
        $this->setsFactory = $setsFactory;
        $this->productFactory = $productFactory;
        $this->type = $type;
        $this->status = $status;
        $this->visibility = $visibility;
        $this->moduleManager = $moduleManager;
        $this->customCollectionNotification = $customCollectionNotification;
        parent::__construct($context, $backendHelper, $data);
    }

    /**
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setUseAjax(true);
        $this->setId("productGrid");
        $this->setDefaultDir("DESC");
        $this->setDefaultSort("entity_id");
        $this->setSaveParametersInSession(true);
        $this->setVarNameFilter("product_filter");
    }

    /**
     * Function to get store
     *
     * @return int
     */
    protected function _getStore()
    {
        $storeId = (int)$this->getRequest()->getParam("store", 0);
        return $this->_storeManager->getStore($storeId);
    }

    /**
     * Function to prepare collection
     *
     * @return object
     */
    protected function _prepareCollection()
    {
        $store = $this->_getStore();
        $collection = $this->productFactory->create()->getCollection()
            ->addAttributeToSelect("sku")
            ->addAttributeToSelect("name")
            ->addAttributeToSelect("attribute_set_id")
            ->addAttributeToSelect("type_id")
            ->setStore($store);
        if ($this->moduleManager->isEnabled("Magento_CatalogInventory")) {
            $collection->joinField("qty", "cataloginventory_stock_item", "qty", "product_id=entity_id", "{{table}}.stock_id=1", "left");
        }
        if ($store->getId()) {
            $collection->addStoreFilter($store);
            $collection->joinAttribute("name", "catalog_product/name", "entity_id", null, "inner", Store::DEFAULT_STORE_ID);
            $collection->joinAttribute("custom_name", "catalog_product/name", "entity_id", null, "inner", $store->getId());
            $collection->joinAttribute("status", "catalog_product/status", "entity_id", null, "inner", $store->getId());
            $collection->joinAttribute("visibility", "catalog_product/visibility", "entity_id", null, "inner", $store->getId());
            $collection->joinAttribute("price", "catalog_product/price", "entity_id", null, "left", $store->getId());
        } else {
            $collection->addAttributeToSelect("price");
            $collection->joinAttribute("status", "catalog_product/status", "entity_id", null, "inner");
            $collection->joinAttribute("visibility", "catalog_product/visibility", "entity_id", null, "inner");
        }
        $this->setCollection($collection);
        $this->getCollection()->addWebsiteNamesToResult();
        parent::_prepareCollection();
        return $this;
    }

    /**
     * Function to add filter to column
     *
     * @return array
     */
    protected function _addColumnFilterToCollection($column)
    {
        if ($this->getCollection()) {
            if ($column->getId() == "websites") {
                $this->getCollection()->joinField("websites", "catalog_product_website", "website_id", "product_id=entity_id", null, "left");
            }
        }
        return parent::_addColumnFilterToCollection($column);
    }

    /**
     * Function to prepare columns
     *
     * @return columns
     */
    protected function _prepareColumns()
    {
        $this->addColumn(
            "in_category",
            [
                "type"              => "checkbox",
                "name"              => "in_category",
                "align"             => "center",
                "index"             => "entity_id",
                "values"            => $this->_getSelectedProducts(),
                "header_css_class"  => "a-center"
            ]
        );

        $this->addColumn(
            "entity_id", 
            [
                "type"              => "number",
                "index"             => "entity_id",
                "header"            => __("ID"),
                "header_css_class"  => "col-id",
                "column_css_class"  => "col-id"
            ]
        );

        $this->addColumn(
            "name",
            [
                "index"             => "name",
                "class"             => "xxx",
                "header"            => __("Name")
            ]
        );

        $store = $this->_getStore();
        if ($store->getId()) {
            $this->addColumn(
                "custom_name",
                [
                    "index"             => "custom_name",
                    "header"            => __("Name in %1", $store->getName()),
                    "header_css_class"  => "col-name",
                    "column_css_class"  => "col-name"
                ]
            );
        }

        $this->addColumn(
            "sku",
            [
                "index"             => "sku",
                "header"            => __("SKU")
            ]
        );

        $store = $this->_getStore();
        $this->addColumn(
            "price",
            [
                "type"              => "price",
                "index"             => "price",
                "header"            => __("Price"),
                "currency_code"     => $store->getBaseCurrency()->getCode(),
                "header_css_class"  => "col-price",
                "column_css_class"  => "col-price"
            ]
        );

        $this->addColumn(
            "status",
            [
                "type"              => "options",
                "index"             => "status",
                "header"            => __("Status"),
                "options"           => $this->status->getOptionArray()
            ]
        );

        $block = $this->getLayout()->getBlock("grid.bottom.links");
        if ($block) {
            $this->setChild("grid.bottom.links", $block);
        }
        return parent::_prepareColumns();
    }

    /**
     * Function to get grid url
     *
     * @return string
     */
    public function getGridUrl()
    {
        return $this->getUrl("mobikul/notification/getproductgridhtml", ["_current" => true]);
    }

    /**
     * Function to get sekected products
     *
     * @return array
     */
    protected function _getSelectedProducts()
    {
        $productIds = [];
        $notificationData = $this->customCollectionNotification->getNotificationData();
        if (count($notificationData) > 0) {
            $filterData = unserialize($notificationData["filter_data"]);
            if (is_string($filterData)) {
                $productIds = explode(",", $filterData);
            }
            return $productIds;
        } else {
            return $productIds;
        }
    }

}