<?php
/**
 * Mirasvit
 *
 * This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
 * Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
 * If you wish to customize this module for your needs.
 * Please refer to http://www.magentocommerce.com for more information.
 *
 * @category  Mirasvit
 * @package   mirasvit/module-rma
 * @version   2.2.28
 * @copyright Copyright (C) 2023 Mirasvit (https://mirasvit.com/)
 */



namespace Mirasvit\Rma\Helper;

use Mirasvit\Rma\Model\Config as Config;
use Mirasvit\Rma\Model\ResourceModel\Rma\CollectionFactory as RmaCollectionFactory;
use Mirasvit\Rma\Api\Service\Rule\RuleManagementInterface;
use Mirasvit\Rma\Api\Service\Rma\RmaManagementInterface;
use Mirasvit\Rma\Helper\Rma\Event;
use Magento\Framework\App\Helper\Context;

class Ruleevent extends \Magento\Framework\App\Helper\AbstractHelper
{
    private $rmaCollectionFactory;

    private $rmaMail;

    private $rmaManagement;

    private $eventHelper;

    private $ruleManagement;

    private $context;

    /**
     * Ruleevent constructor.
     *
     * @param \Mirasvit\Rma\Api\Service\Rule\RuleManagementInterface $ruleManagement
     * @param \Mirasvit\Rma\Api\Service\Rma\RmaManagementInterface   $rmaManagement
     * @param Rma\Event                                              $eventHelper
     * @param Mail                                                   $rmaMail
     * @param \Magento\Framework\App\Helper\Context                  $context
     */
    public function __construct(
        RmaCollectionFactory $rmaCollectionFactory,
        RuleManagementInterface $ruleManagement,
        RmaManagementInterface $rmaManagement,
        Event $eventHelper,
        Mail $rmaMail,
        Context $context
    ) {
        $this->rmaCollectionFactory = $rmaCollectionFactory;
        $this->ruleManagement       = $ruleManagement;
        $this->rmaManagement        = $rmaManagement;
        $this->eventHelper          = $eventHelper;
        $this->rmaMail              = $rmaMail;
        $this->context              = $context;

        parent::__construct($context);
    }

    /**
     * @var array
     */
    protected $sentEmails = [];

    /**
     * @var array
     */
    protected $processedEvents = [];

    /**
     * @param string                  $eventType
     * @param \Mirasvit\Rma\Model\Rma $rma
     *
     * @return void
     */
    public function newEvent($eventType, $rma): void
    {
        $key = $eventType . $rma->getId();
        if (isset($this->processedEvents[$key])) {
            return;
        } else {
            $this->processedEvents[$key] = true;
        }

        $this->sentEmails = [];
        $rules            = $this->ruleManagement->getEventRules($eventType);
        /** @var \Mirasvit\Rma\Model\Rule $rule */
        foreach ($rules as $rule) {
            $rule->afterLoad();
            if (!$rule->validate($rma)) {
                continue;
            }
            $this->processRule($rule, $rma);
            if ($rule->getIsStopProcessing()) {
                break;
            }
        }
    }

    /**
     * @param string $eventType
     *
     * @return void
     */
    public function newEventCheck($eventType): void
    {
        $rmas = $this->rmaCollectionFactory->create()
            ->setPageSize(500);

        $rules = $this->ruleManagement->getEventRules($eventType);
        foreach ($rmas as $rma) {
            foreach ($rules as $rule) {
                $rule->afterLoad();
                if (!$rule->validate($rma)) {
                    continue;
                }
                $this->processRule($rule, $rma);
                if ($rule->getIsStopProcessing()) {
                    break;
                }
            }
        }
    }

    /**
     * @param \Mirasvit\Rma\Model\Rule $rule
     * @param \Mirasvit\Rma\Model\Rma  $rma
     *
     * @return void
     */
    protected function processRule($rule, $rma): void
    {
        $statusChanged = 0;
        /* set attributes **/
        if ($rule->getStatusId()) {
            $rma->setStatusId($rule->getStatusId());
            $statusChanged = 1;
        }
        if ($rule->getUserId()) {
            $rma->setUserId($rule->getUserId());
        }
        if ($rule->getIsResolved()) {
            $rma->setIsResolved($rule->getIsResolved());
        }

        $rma->save();
        if ($statusChanged) {
            $this->eventHelper->onRmaStatusChange($rma);
        }

        /* send notifications **/
        if ($rule->getIsSendOwner()) {
            if ($user = $this->rmaManagement->getUser($rma)) {
                /** @var \Magento\User\Model\User $user */
                $this->_sendEventNotification($user->getEmail(), $user->getName(), $rule, $rma);
            }
        }
        if ($rule->getIsSendUser()) {
            if ($customer = $this->rmaManagement->getCustomer($rma)) {
                /** @var \Magento\Customer\Model\Customer $customer */
                $this->_sendEventNotification($customer->getEmail(), $customer->getName(), $rule, $rma);
            }
        }
        if ($otherEmail = explode(',', $rule->getOtherEmail())) {
            $this->_sendEventNotification($otherEmail, '', $rule, $rma);
        }
    }

    /**
     * @param array|string             $email
     * @param string                   $name
     * @param \Mirasvit\Rma\Model\Rule $rule
     * @param \Mirasvit\Rma\Model\Rma  $rma
     *
     * @return void
     */
    protected function _sendEventNotification($email, $name, $rule, $rma): void
    {
        if (!is_array($this->sentEmails) || !in_array($email, $this->sentEmails)) {
            $this->rmaMail->sendNotificationRule($email, $name, $rule, $rma);
            $this->sentEmails[] = $email;
        }
    }
}
