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

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Customer;
use Magento\Catalog\Model\Product;

/**
 * Webkul Agorae Sendmail controller
 */
class Sendmail extends Action
{
    /**
     * @var Session
     */
    public $_customerSession;

    /**
     * @var Customer
     */
    private $customer;

    /**
     * @var Product
     */
    private $product;
    /**
     * @var Webkul\Agorae\Helper\Data
     */
    private $_helperAgorae;

    /**
     * @var Webkul\Marketplace\Helper\Data
     */
    private $_helperMarketplace;

    /**
     * @var Webkul\Marketplace\Helper\Email
     */
    private $_helperMarketplaceEmail;

    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;

    /**
     * @var \Magento\Framework\App\ObjectManager
     */
    protected $objectManager;

    /**
     * @param Context                                   $context
     * @param Session                                   $customerSession
     * @param Webkul\Agorae\Helper\Data                 $agoraeHelper
     * @param \Magento\Framework\Module\Manager         $moduleManager
     * @param Customer                                  $customer
     * @param Product                                   $product
     */
    public function __construct(
        Context $context,
        Session $customerSession,
        Customer $customer,
        \Webkul\Agorae\Helper\Data $agoraeHelper,
        \Magento\Framework\Module\Manager $moduleManager,
        Product $product
    ) {
        $this->objectManager            = \Magento\Framework\App\ObjectManager::getInstance();
        $this->customer                 = $customer;
        $this->product                  = $product;
        $this->_helperAgorae            = $agoraeHelper;
        $this->_customerSession         = $customerSession;
        $this->moduleManager            = $moduleManager;
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $this->_helperMarketplace = $$this->objectManager->create(\Webkul\Marketplace\Helper\Data::class);
            $this->_helperMarketplaceEmail = $$this->objectManager->create(\Webkul\Marketplace\Helper\Email::class);
        }
        parent::__construct($context);
    }

    /**
     * Sendmail to Seller action.
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     */
    public function execute()
    {
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            if (!$this->_helperMarketplace->getSellerProfileDisplayFlag()) {
                $this->getRequest()->initForward();
                $this->getRequest()->setActionName('noroute');
                $this->getRequest()->setDispatched(false);
    
                return false;
            }
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            $data = $this->getRequest()->getParams();
    
            if ($data['seller-id']) {
                $this->_eventManager->dispatch(
                    'mp_send_querymail',
                    [$data]
                );
                if ($this->_customerSession->isLoggedIn()) {
                    $buyerName = $this->_customerSession->getCustomer()->getName();
                    $buyerEmail = $this->_customerSession->getCustomer()->getEmail();
                } else {
                    $buyerEmail = $data['email'];
                    $buyerName = $data['name'];
                    if (strlen($buyerName) < 2) {
                        $buyerName = 'Guest';
                    }
                }
                $emailTemplateVariables = [];
                $senderInfo = [];
                $receiverInfo = [];
                $seller = $this->customer->load($data['seller-id']);
                $emailTemplateVariables['myvar1'] = $seller->getName();
                $sellerEmail = $seller->getEmail();
                if (!isset($data['product-id'])) {
                    $data['product-id'] = 0;
                } else {
                    $emailTemplateVariables['myvar3'] = $this->product->load(
                        $data['product-id']
                    )->getName();
                }
                $emailTemplateVariables['myvar4'] = $data['ask'];
                $emailTemplateVariables['myvar6'] = $data['subject'];
                $emailTemplateVariables['myvar5'] = $buyerEmail;
                $senderInfo = [
                    'name' => $buyerName,
                    'email' => $buyerEmail,
                ];
                $receiverInfo = [
                    'name' => $seller->getName(),
                    'email' => $sellerEmail,
                ];
                $this->_helperMarketplaceEmail
                    ->sendQuerypartnerEmail(
                        $data,
                        $emailTemplateVariables,
                        $senderInfo,
                        $receiverInfo
                    );
            }
            $this->messageManager->addSuccess(
                __('Message has been successfully sent')
            );
            return $resultRedirect->setPath(
                'marketplace/seller/collection',
                ['shop' => $data['shop_url']]
            );
        }
    }
}
