<?php
/**
 * Block for Quote list at customer end.
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

namespace Magetop\Quotesystem\Block;

use Magetop\Quotesystem\Model\ResourceModel\Quotes\CollectionFactory;
use Magento\Framework\Pricing\Helper\Data;
use Magetop\Quotesystem\Model\Quotes;
use Magetop\Quotesystem\Helper\Data as Helper;

class Buyerquotes extends \Magento\Framework\View\Element\Template
{
    /**
     * @var customerSession
     */
    protected $_customerSession;
    /**
     * @var quoteCollectionFacory
     */
    protected $_quoteCollectionFactory;
    /**
     * @var quoteCollection
     */
    protected $_quoteCollection;
    /**
     * @var pricingHelper
     */
    protected $_pricingHelper;
    /**
     * @var \Magento\Framework\Url\EncoderInterface
     */
    protected $_urlEncoder;
    /**
     * @var Magetop\Quoetsystem\Model\Quotes
     */
    protected $_quotesModel;

    /**
     * @var $nameClass
     */
    public $nameClass = "fa fa-sort";

    /**
     * @var $quantityClass
     */
    public $quantityClass = "fa fa-sort";

    /**
     * @var $priceClass
     */
    public $priceClass = "fa fa-sort";

    /**
     * @var Helper
     */
    protected $helper;

    /**
     * @var Context
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;

    /**
     * @var \Magento\Framework\App\Response\Http
     */
    protected $response;

    /**
     * Construct
     *
     * @param MagentoCustomerModelSession                       $customerSession
     * @param MagentoCatalogBlockProductContext                 $context
     * @param CollectionFactory                                 $quotesCollectionFactory
     * @param Data                                              $pricingHelper
     * @param MagentoFrameworkUrlEncoderInterface               $urlEncoder
     * @param Quotes                                            $quotesModel
     * @param Helper                                            $helper
     * @param \Magento\Framework\Json\Helper\Data               $jsonHelper
     * @param \Magento\Framework\App\Response\RedirectInterface $redirect
     * @param \Magento\Framework\App\Response\Http              $response
     * @param [type]                                            $data
     */
    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Catalog\Block\Product\Context $context,
        CollectionFactory $quotesCollectionFactory,
        Data $pricingHelper,
        \Magento\Framework\Url\EncoderInterface $urlEncoder,
        Quotes $quotesModel,
        Helper $helper,
        \Magento\Framework\Json\Helper\Data $jsonHelper,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\App\Response\Http $response,
        array $data = []
    ) {
        $this->_customerSession = $customerSession;
        $this->_quoteCollectionFactory = $quotesCollectionFactory;
        $this->_pricingHelper = $pricingHelper;
        $this->_urlEncoder = $urlEncoder;
        $this->_quotesModel = $quotesModel;
        $this->_storeManager =  $context->getStoreManager();
        $this->helper = $helper;
        $this->jsonHelper = $jsonHelper;
        $this->redirect = $redirect;
        $this->response = $response;
        parent::__construct($context, $data);
    }
    /**
     * Prepare layout
     *
     * @return $this
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if ($this->getQuotesCollection()->getData() || $this->getRequest()->getParams('status')) {
            $pager = $this->getLayout()->createBlock(
                \Magento\Theme\Block\Html\Pager ::class,
                'quotesystem.pager'
            )
                ->setCollection(
                    $this->getQuotesCollection()
                );
            $this->setChild('pager', $pager);
            $this->getQuotesCollection()->load();
        } else {
            $this->redirect->redirect($this->response, 'quotesystem/buyerquote/');
        }
        return $this;
    }
    /**
     * Get pager Html
     *
     * @return string
     */
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }
    /**
     * Customer Id by customer session
     *
     * @return int
     */
    public function getCustomerId()
    {
        return $this->_customerSession->getCustomerId();
    }
    /**
     * Get Collection of quotes
     *
     * @return collection
     */
    public function getQuotesCollection()
    {
        $attribute = $this->attributeIdOfProductName();
        $filter = '';
        $filterStatus = 0;
        $filterSorting = '';
        $quoteId = $this->_customerSession->getSelectedQuoteId();
        $data = $this->getRequest()->getParams();
        if ($proName = $this->getRequest()->getParam('s')) {
            $filter = $proName != "" ? $proName : "";
        }
        if (isset($data['status'])) {
            $filterStatus = $data['status'] != "" ? $data['status'] : 0;
        }
        if (isset($data['sortingStatus'])) {
            $filterSorting = $data['sortingStatus'];
        }
        if (!$this->_quoteCollection) {
            $collection = $this->_quoteCollectionFactory
                ->create()
                ->addFieldToSelect('*')
                ->addFieldToFilter(
                    'customer_id',
                    $this->getCustomerId()
                );
            if (!empty($filter)) {
                $collection->addFieldToFilter('product_name', ['like' => '%'.$filter.'%']);
            }
            if ($filterStatus != 0) {
                $collection->addFieldToFilter('status', $filterStatus);
            }
            $collection->addFieldToFilter('quote_id', $quoteId);
            $collection->getSelect()
                ->joinLeft(
                    ["cpev" => $collection->getTable("catalog_product_entity_varchar")],
                    "cpev.row_id = main_table.product_id AND cpev.attribute_id = ". $attribute."",
                    ["prod_name" => "cpev.value",'pro_id' => '']
                )
                ->joinLeft(
                    ["css" => $collection->getTable("cataloginventory_stock_status")],
                    "css.product_id = main_table.product_id",
                    ["stockstatus" => "css.stock_status"]
                );
            $collection->getSelect()->group("main_table.entity_id");
            $collection = $this->getCollectionBySortingOrder($collection, $filterSorting);
            $this->getClassNameBySortOrder($filterSorting);
            $this->_quoteCollection = $collection;
        }

        return $this->_quoteCollection;
    }

    /**
     * GetClassNameBySortOrder
     *
     * @param string $filterSorting
     */
    protected function getClassNameBySortOrder($filterSorting)
    {
        if ($filterSorting == 'productNameInc') {
            $this->nameClass = "fa fa-sort-asc";
        } elseif ($filterSorting == 'productNameDesc') {
            $this->nameClass = "fa fa-sort-desc";
        } elseif ($filterSorting == 'productQuantityInc') {
            $this->quantityClass = "fa fa-sort-asc";
        } elseif ($filterSorting == 'productQuantityDesc') {
            $this->quantityClass = "fa fa-sort-desc";
        } elseif ($filterSorting == 'productPriceInc') {
            $this->priceClass = "fa fa-sort-asc";
        } elseif ($filterSorting == 'productPriceDesc') {
            $this->priceClass = "fa fa-sort-desc";
        }
    }
    /**
     * Get formatted price by currency
     *
     * @param string $price
     * @return format price string
     */
    public function getFormattedPrice($price)
    {
        $fromCurrency = $this->helper->getCurrentCurrencyCode();
        $toCurrency = $this->helper->getBaseCurrencyCode();
        $amount = $this->helper->getmtconvertCurrency($fromCurrency, $toCurrency, $price);
        return $this->_pricingHelper
            ->currency($amount, true, false);
    }

    /**
     * GetCollectionBySortingOrder
     *
     * @param object $collection
     * @param string $filterSorting
     * @return $collection
     */
    protected function getCollectionBySortingOrder($collection, $filterSorting)
    {
        if ($filterSorting == 'productNameInc') {
            $collection = $collection->setOrder('product_name', 'ASC');
            return $collection;
        } elseif ($filterSorting == 'productNameDesc') {
            $collection = $collection->setOrder('product_name', 'DESC');
            return $collection;
        } elseif ($filterSorting == 'productQuantityInc') {
            $collection = $collection->setOrder('quote_qty', 'ASC');
            return $collection;
        } elseif ($filterSorting == 'productQuantityDesc') {
            $collection = $collection->setOrder('quote_qty', 'DESC');
            return $collection;
        } elseif ($filterSorting == 'productPriceInc') {
            $collection = $collection->setOrder('quote_price', 'ASC');
            return $collection;
        } elseif ($filterSorting == 'productPriceDesc') {
            $collection = $collection->setOrder('quote_price', 'DESC');
            return $collection;
        } else {
             $collection = $collection->setOrder('created_at', 'DESC');
             return $collection;
        }
    }

    /**
     * Attribute Id of product name
     */
    protected function attributeIdOfProductName()
    {
        $object = \Magento\Framework\App\ObjectManager::getInstance();
        $attribute = $object->get(\Magento\Eav\Api\AttributeRepositoryInterface ::class);
        $attributeId = $attribute->get('catalog_product', 'name')->getId();
        return $attributeId;
    }

    /**
     * Use to get current url.
     */
    public function getCurrentUrl()
    {
        // Give the current url of recently viewed page
        return $this->_urlBuilder->getCurrentUrl();
    }

    /**
     * Check whether a quote is sold or not?
     *
     * @param  int $quoteStatus
     * @return boolean
     */
    public function quoteStatusIsNotSold($quoteStatus)
    {
        if ($quoteStatus!=\Magetop\Quotesystem\Model\Quotes::STATUS_SOLD) {
            return true;
        }
        return false;
    }

    /**
     * GetIsSecure
     */
    public function getIsSecure()
    {
        return $this->getRequest()->isSecure();
    }

    /**
     * Get Configuration Value
     *
     * @return $values
     */
    public function getConfigValue()
    {
        return $this->helper->getConfigValues();
    }

    /**
     * Get Base URL
     */
    public function getBaseUrl()
    {
        return $this->getBaseUrl();
    }

    /**
     * Get Helper
     *
     * @return Magetop\QuoteSystem\Helper\Data
     */
    public function getHelper()
    {
        return $this->helper;
    }

    /**
     * Get Json Helper
     *
     * @return Magento\Framework\Json\Helper\Data
     */
    public function getJsonHelper()
    {
        return $this->jsonHelper;
    }

    /**
     * Get Quote Id
     */
    public function getQuoteId()
    {
        return $this->_customerSession->getSelectedQuoteId();
    }
}
