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

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Webkul\Agorae\Model\MpfavouritesellerRepository;
use Webkul\Agorae\Helper\Data;

class SendMailToFollowers extends \Magento\Customer\Controller\AbstractAccount
{
    /**
     * @var PageFactory
     */
    private $resultPageFactory;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @var Webkul\Agorae\Helper\Data
     */
    private $helperData;

    /**
     * @var Webkul\Agorae\Model\MpfavouritesellerRepository
     */
    private $mpFavouritesellerRepository;

    /**
     * @param Context                         $context
     * @param MpfavouritesellerRepository     $mpFavouritesellerRepository
     * @param \Magento\Customer\Model\Session $customerSession
     * @param Data                            $helperData
     * @param PageFactory                     $resultPageFactory
     */
    public function __construct(
        Context $context,
        MpfavouritesellerRepository $mpFavouritesellerRepository,
        \Magento\Customer\Model\Session $customerSession,
        Data $helperData,
        PageFactory $resultPageFactory
    ) {
        $this->mpFavouritesellerRepository = $mpFavouritesellerRepository;
        $this->_customerSession = $customerSession;
        $this->helperData = $helperData;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * SendMailToFollowers class
     */
    public function execute()
    {
        $fields = $this->getRequest()->getParams();
        $sellerCollection = $this->helperData->getCustomerData($fields['seller-id']);
        $email = $sellerCollection->getEmail();
        $name = $sellerCollection->getName();
        $followersCollection = $this->mpFavouritesellerRepository
            ->getCustomersCollectionBySellerId($fields['seller-id']);
        foreach ($followersCollection as $follower) {
            $customer = $this->helperData->getCustomerData($follower->getCustomerId());
            $senderInfo = [];
            $receiverInfo = [];
            $receiverInfo = [
                'name' => $customer->getName(),
                'email' => $customer->getEmail(),
            ];
            $senderInfo = [
                'name' => $name,
                'email' => $email,
            ];
            $emailTempVariables = [];
            $emailTempVariables['myvar1'] = $customer->getName();
            $emailTempVariables['myvar2'] = strip_tags($fields['message']);
            $emailTempVariables['myvar3'] = $fields['subject'];
            $this->helperData->notifyFollowers(
                $emailTempVariables,
                $senderInfo,
                $receiverInfo
            );
        }
    }
}
