<?php

namespace Sibs\Sibs\Model\Order\Email\Sender;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Sales\Model\Order\Email\SenderBuilderFactory;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Magento\Sales\Model\ResourceModel\Order as OrderResource;
use Magento\Sales\Model\Order\Address\Renderer;
use Magento\Payment\Helper\Data as PaymentHelper;
use Psr\Log\LoggerInterface;
use Sibs\Sibs\Model\SibsConstants;
use Sibs\Sibs\Model\SibsOrder;
use Sibs\Sibs\Utils\SibsLog;

/**
 * Class OrderSender
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class SibsOrderSender extends OrderSender
{
    /**
     * @param \Magento\Sales\Model\Order\Email\Container\Template $templateContainer
     * @param \Magento\Sales\Model\Order\Email\Container\OrderIdentity $identityContainer
     * @param \Magento\Sales\Model\Order\Email\SenderBuilderFactory $senderBuilderFactory
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Magento\Payment\Helper\Data $paymentHelper
     * @param \Magento\Sales\Model\ResourceModel\Order $orderResource
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig
     * @param \Magento\Sales\Model\Order\Address\Renderer $addressRenderer
     * @param \Magento\Framework\Event\ManagerInterface $eventManager
     */
    public function __construct(
        Template $templateContainer,
        OrderIdentity $identityContainer,
        SenderBuilderFactory $senderBuilderFactory,
        LoggerInterface $logger,
        Renderer $addressRenderer,
        PaymentHelper $paymentHelper,
        OrderResource $orderResource,
        ScopeConfigInterface $globalConfig,
        ManagerInterface $eventManager
    ) {
        parent::__construct(
            $templateContainer,
            $identityContainer,
            $senderBuilderFactory,
            $logger,
            $addressRenderer,
            $paymentHelper,
            $orderResource,
            $globalConfig,
            $eventManager
        );
    }

    /**
     * Sends order email to the customer.
     *
     * Email will be sent immediately in two cases:
     *
     * - if asynchronous email sending is disabled in global settings
     * - if $forceSyncMode parameter is set to TRUE
     *
     * Otherwise, email will be sent later during running of
     * corresponding cron job.
     *
     * @param Order $order
     * @param bool $forceSyncMode
     * @return bool
     */
    public function send(Order $order, $forceSyncMode = false): bool
    {
        if (! $this->identityContainer->isEnabled())
            return false;

        $orderRealId = $order->getRealOrderId();
        SibsLog::info("SENDMAIL - Order Real Id: $orderRealId");

        $payment = $order->getPayment();
        $method = $payment->getMethodInstance();
        $paymentMethodCode = $method->getCode();

        SibsLog::info('SENDMAIL - Order Payment Method Code: ' . $paymentMethodCode);

        if (in_array($paymentMethodCode, SibsConstants::SIBS_PAYMENTS)) {
            $sibsOrder = new SibsOrder($order->getId());
            $orderStatus = $sibsOrder->getStatus();
            SibsLog::info('SENDMAIL - Order Status: ' . $orderStatus);
            if (in_array($orderStatus, SibsConstants::SIBS_SEND_MAIL_STATUSES)) {
                SibsLog::info('SENDMAIL - (Will Send a SIBS email)');
                return parent::send($order, $forceSyncMode);
            }

            SibsLog::warn('SENDMAIL - (Will not Send any email)');
            return true;
        } else {
            // it means that the payment method is not from SIBS
            SibsLog::info('SENDMAIL - (Will Send other payment email)');
            return parent::send($order, $forceSyncMode);
        }
    }

    /**
     * Get payment info block as html
     *
     * @param Order $order
     * @return string
     */
    protected function getPaymentHtml(Order $order): string
    {
        $regularPaymentInfo = parent::getPaymentHtml($order);

        $payment = $order->getPayment();
        $method = $payment->getMethodInstance();
        $paymentMethod = $method->getPaymentMethod();

        if (in_array($paymentMethod, SibsConstants::SIBS_PAYMENTS)) {
            $sibsOrder = new SibsOrder($order->getId());
            $sibsPaymentInfo = $method->getPaymentEmailHTML($sibsOrder);

            return $regularPaymentInfo . $sibsPaymentInfo;
        }

        return $regularPaymentInfo;
    }
}
