<?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\Observer;

use Magento\Framework\Event\ObserverInterface;
use Webkul\Agorae\Model\MpfavouritesellerRepository;
use Webkul\Agorae\Helper\Data;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Category;

/**
 * Webkul Agorae FavMpApproveProductObserver Observer
 */
class FavMpApproveProductObserver implements ObserverInterface
{
    /**
     * @var eventManager
     */
    protected $_eventManager;

    /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @var Session
     */
    protected $_customerSession;

    /**
     * @var Webkul\Agorae\Model\MpfavouritesellerRepository
     */
    protected $_mpFavouritesellerRepository;

    /**
     * @var Magento\Catalog\Model\Product
     */
    protected $_productMageModel;

    /**
     * @var Magento\Catalog\Model\Category
     */
    protected $_categoryMageModel;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var Webkul\Marketplace\Helper\Data
     */
    protected $_mpHelper;

    /**
     * @var Magento\Framework\Pricing\Helper\Data
     */
    protected $_priceMageHelper;

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

    /**
     * @param \Magento\Framework\Event\Manager           $eventManager
     * @param \Magento\Customer\Model\Session            $customerSession
     * @param MpfavouritesellerRepository                $mpFavouritesellerRepository
     * @param Data                                       $helperData
     * @param Product                                    $productMageModel
     * @param Category                                   $categoryMageModel
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\Pricing\Helper\Data     $priceMageHelper
     * @param \Magento\Framework\Module\Manager          $moduleManager
     */
    public function __construct(
        \Magento\Framework\Event\Manager $eventManager,
        \Magento\Customer\Model\Session $customerSession,
        MpfavouritesellerRepository $mpFavouritesellerRepository,
        Data $helperData,
        Product $productMageModel,
        Category $categoryMageModel,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Pricing\Helper\Data $priceMageHelper,
        \Magento\Framework\Module\Manager $moduleManager
    ) {
    
        $this->_eventManager = $eventManager;
        $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $this->_customerSession = $customerSession;
        $this->_mpFavouritesellerRepository = $mpFavouritesellerRepository;
        $this->_helperData = $helperData;
        $this->_productMageModel = $productMageModel;
        $this->_categoryMageModel = $categoryMageModel;
        $this->_storeManager = $storeManager;
        $this->_priceMageHelper = $priceMageHelper;
        $this->moduleManager = $moduleManager;
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $this->_mpHelper = $this->_objectManager->create(\Webkul\Marketplace\Helper\Data::class);
        }
    }

    /**
     * MpFavouriteSeller product approve event handler.
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $mpProducts = $observer->getProduct();
        $seller = $observer->getSeller();
        $productId = $mpProducts->getMageproductId();
        $collection = $followersCollection = $this->_mpFavouritesellerRepository
                        ->getCustomersCollectionBySellerId($seller->getId());

        $seller = $this->_helperData->getCustomerData($seller->getId());
        $product = $this->_productMageModel->load($productId);
        $categoryIds = $product->getCategoryIds();
        if (!empty($categoryIds)) {
            $firstCategoryId = $categoryIds[0];
        } else {
            $firstCategoryId='';
        }
        
        $_category = $this->_categoryMageModel->load($firstCategoryId);
        $productUrl = $this->_storeManager->getStore()
                    ->getBaseUrl().$product->getUrlPath();
        if (!empty($collection)) {
            foreach ($collection as $follower) {
                $customer = $this->_helperData->getCustomerData($follower->getCustomerId());
                $emailTempVariables = [];
                if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
                    $userList = $this->_mpHelper
                            ->getSellerDataBySellerId($seller->getId());
                    foreach ($userList as $user) {
                        $shopUrl = $user->getShopUrl();
                    }

                    $senderInfo = [];
                    $receiverInfo = [];
    
                    $receiverInfo = [
                        'name' => $customer->getName(),
                        'email' => $customer->getEmail(),
                    ];
                    $senderInfo = [
                        'name' => $seller->getName(),
                        'email' =>$seller->getEmail(),
                    ];
    
                    $emailTempVariables['myvar1'] = $customer->getName();
                    $emailTempVariables['myvar2'] = $shopUrl;
                    $emailTempVariables['myvar3'] = $productUrl;
                    $emailTempVariables['myvar4'] = $product->getName();
                    $emailTempVariables['myvar5'] = $_category->getName();
                    $emailTempVariables['myvar6'] = $product->getDescription();
                    $emailTempVariables['myvar7'] = $this->_priceMageHelper
                                                    ->currency(
                                                        $product->getPrice(),
                                                        true,
                                                        false
                                                    );
    
                    $this->_helperData->followersNotifyProductApprove(
                        $emailTempVariables,
                        $senderInfo,
                        $receiverInfo
                    );
                }
            }
        }
    }
}
