<?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_ProductAttachments
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\ProductAttachments\Model\ResourceModel\Log\Grid;

use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
use Magento\Framework\App\RequestInterface;
use Mageplaza\ProductAttachments\Model\ResourceModel\Log;
use Mageplaza\ProductAttachments\Helper\Data as HelperData;
use Psr\Log\LoggerInterface as Logger;
use Zend_Db_Expr;

/**
 * Class Collection
 * @package Mageplaza\ProductAttachments\Model\ResourceModel\Log\Grid
 */
class Collection extends SearchResult
{
    /**
     * @var RequestInterface
     */
    protected $request;

    /**
     * @var HelperData
     */
    protected $_helperData;

    /**
     * Collection constructor.
     *
     * @param RequestInterface $request
     * @param HelperData $_helperData
     * @param EntityFactory $entityFactory
     * @param Logger $logger
     * @param FetchStrategy $fetchStrategy
     * @param EventManager $eventManager
     * @param string $mainTable
     * @param string $resourceModel
     *
     * @throws LocalizedException
     */
    public function __construct(
        RequestInterface $request,
        HelperData $_helperData,
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'mageplaza_productattachments_log',
        $resourceModel = Log::class
    ) {
        $this->request     = $request;
        $this->_helperData = $_helperData;
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }

    /**
     * @return $this
     */
    protected function _initSelect()
    {
        parent::_initSelect();

        $this->addFileLabelName();
        $this->addCustomerName();
        $this->addProductName();

        return $this;
    }

    /**
     * @param array|string $field
     * @param null $condition
     *
     * @return SearchResult
     */
    public function addFieldToFilter($field, $condition = null)
    {
        switch ($field) {
            case 'customer_name':
                if (strtolower($condition['like']) === '%guest%') {
                    return parent::addFieldToFilter('main_table.customer_id', ['eq' => 0]);
                } else {
                    return parent::addFieldToFilter(
                        ['firstname', 'lastname'],
                        [$condition, $condition]
                    );
                }
            case 'name':
                $field = 'mpf.name';
                break;
            case 'label':
                $field = 'mpf.label';
                break;
            case 'product_name':
                $field = 'v1.value';
                break;
            case 'created_at':
                $field = 'main_table.created_at';
                break;
            case 'file_action':
                $field = 'main_table.file_action';
                break;
            case 'customer_group_filter':
                $field = 'main_table.customer_group';
                break;
            case 'store_filter':
                $field = 'main_table.store_id';
                break;
            default:
                break;
        }

        return parent::addFieldToFilter($field, $condition);
    }

    /**
     * @return $this
     */
    public function addFileLabelName()
    {
        $this->getSelect()->joinLeft(
            ['mpf' => $this->getTable('mageplaza_productattachments_file')],
            'main_table.file_id = mpf.file_id',
            ['name' => 'name', 'label' => 'label', 'group' => 'group']
        );

        return $this;
    }

    /**
     * @return $this
     */
    public function updateWhereCreatedAt()
    {
        $createdAtValue = $this->request->getParam('filters')['created_at'] ?? null;
        if ($createdAtValue) {
            $wherePart = $this->getSelect()->getPart(\Magento\Framework\DB\Select::WHERE);
            foreach ($wherePart as $key => $condition) {
                if (strpos($condition, 'created_at') !== false) {
                    unset($wherePart[$key]);
                }
            }
            $this->getSelect()->setPart(\Magento\Framework\DB\Select::WHERE, $wherePart);

            $from = $createdAtValue['from'] ?? null;
            $to   = $createdAtValue['to'] ?? null;
            if ($from) {
                $dateFrom = \DateTime::createFromFormat('n/d/Y', $from)->format('Y-m-d H:i:s');
                $this->getSelect()->where(new Zend_Db_Expr('main_table.created_at >= ?'), $dateFrom);
            }
            if ($to) {
                $dateTo = \DateTime::createFromFormat('n/d/Y', $to)->format('Y-m-d H:i:s');
                $this->getSelect()->where(new Zend_Db_Expr('main_table.created_at <= ?'), $dateTo);
            }
        }

        return $this;
    }

    /**
     * @return $this
     */
    public function addCustomerName()
    {
        $this->getSelect()->joinLeft(
            ['ce' => $this->getTable('customer_entity')],
            'main_table.customer_id = ce.entity_id',
            ['firstname', 'lastname']
        )->columns([
            'customer_name' => new Zend_Db_Expr("CONCAT(`ce`.`firstname`,' ',`ce`.`lastname`)")
        ]);

        return $this;
    }

    /**
     * @return $this
     */
    public function addProductName()
    {
        $productIdRow          = ($this->_helperData->getEdition() === 'Enterprise' || $this->_helperData->getEdition() === 'B2B')
            ? 'row_id'
            : 'entity_id';
        $eavAttributeTable     = $this->getTable('eav_attribute');
        $eavAttributeTypeTable = $this->getTable('eav_entity_type');
        $this->getSelect()->joinLeft(
            ['cpe' => $this->getTable('catalog_product_entity')],
            'main_table.product_id = cpe.' . $productIdRow,
            [$productIdRow => 'cpe.' . $productIdRow]
        )->joinLeft(
            ['v1' => $this->getTable('catalog_product_entity_varchar')],
            'cpe.' . $productIdRow . ' = v1.' . $productIdRow . ' AND v1.store_id = 0 AND v1.attribute_id =
                  (SELECT attribute_id
                   FROM ' . $eavAttributeTable . "
                   WHERE attribute_code = 'name'
                     AND entity_type_id =
                       (SELECT entity_type_id
                        FROM " . $eavAttributeTypeTable . "
                        WHERE entity_type_code = 'catalog_product'))",
            ['product_name' => 'v1.value']
        );

        return $this;
    }

    /**
     * @param string $field
     * @param string $direction
     *
     * @return SearchResult
     */
    public function setOrder($field, $direction = self::SORT_ORDER_DESC)
    {
        if ($field === 'created_at') {
            $field = 'main_table.created_at';
        }

        if ($field === 'group') {
            $field = 'mpf.group';
        }

        $this->updateWhereCreatedAt();

        return parent::setOrder($field, $direction);
    }
}
