<?php

namespace Oney\ThreeByFour\Plugin\Payment\Helper;

use Magento\Payment\Model\MethodInterface;
use Oney\ThreeByFour\Model\Api\Marketing\BusinessTransactions;
use Oney\ThreeByFour\Api\PaymentMethodListInterface;
use Psr\Log\LoggerInterface;

/**
 * Payment Helper Data Plugin
 * 
 * Plugin for Magento payment helper data.
 * Extends payment methods with ThreeByFour business transactions.
 * 
 * @package Oney\ThreeByFour\Plugin\Payment\Helper
 * @version 1.0.0
 * @author Oney
 * @since 1.0.0
 */
class DataPlugin
{
    /**
     * Business transactions service
     * 
     * @var BusinessTransactions
     * @version 1.0.0
     */
    protected $_businessTransactions;
    
    /**
     * Logger instance
     * 
     * @var LoggerInterface
     * @version 1.0.0
     */
    protected $logger;
    
    /**
     * Payment method list interface
     * 
     * @var PaymentMethodListInterface
     * @version 1.0.0
     */
    protected $paymentMethodList;

    /**
     * Constructor
     * 
     * @param BusinessTransactions $businessTransactions Business transactions service
     * @param LoggerInterface $logger Logger instance
     * @param PaymentMethodListInterface $paymentMethodList Payment method list interface
     * @version 1.0.0
     */
    public function __construct(
        BusinessTransactions $businessTransactions,
        LoggerInterface $logger,
        PaymentMethodListInterface $paymentMethodList
    ) {
        $this->_businessTransactions = $businessTransactions;
        $this->logger = $logger;
        $this->paymentMethodList = $paymentMethodList;
    }

    /**
     * After get payment methods
     * 
     * Extends payment methods list with ThreeByFour business transactions.
     * 
     * @param \Magento\Payment\Helper\Data $subject Payment helper data
     * @param array $result Original payment methods
     * @return array Extended payment methods
     * @version 1.0.0
     */
    public function afterGetPaymentMethods(\Magento\Payment\Helper\Data $subject, $result): array
    {
        return array_merge($this->_businessTransactions->getBusinessTransactions(), $result);
    }

    /**
     * Around get method instance
     * 
     * Modifies getMethodInstance() call to handle ThreeByFour payment methods.
     * 
     * @param \Magento\Payment\Helper\Data $subject Payment helper data
     * @param callable $proceed Original method callable
     * @param string $code Payment method code
     * @return MethodInterface Payment method instance
     * @throws \Magento\Framework\Exception\LocalizedException
     * @SuppressWarnings(PMD.UnusedFormalParameter)
     * @version 1.0.0
     */
    public function aroundGetMethodInstance(\Magento\Payment\Helper\Data $subject, callable $proceed, string $code): MethodInterface
    {
        $this->logger->info('[INFO] [PLUGIN] Payment method instance requested: ', [
            'code' => $code
        ]);
        if (false === strpos($code, 'threebyfour_')) return $proceed($code);
        return $this->paymentMethodList->getPaymentMethod($code);
    }
}
