<?php

namespace Oney\ThreeByFour\Plugin\Sales\Model;

use Magento\Sales\Api\Data\OrderInterface;
use Oney\ThreeByFour\Api\Payment\ConfirmInterface;

/**
 * Order Plugin
 * 
 * Plugin for Magento order model.
 * Modifies order behavior for ThreeByFour payment methods.
 * 
 * @package Oney\ThreeByFour\Plugin\Sales\Model
 * @version 1.0.0
 * @author Oney
 * @since 1.0.0
 */
class OrderPlugin
{
    /**
     * Payment confirmation service
     * 
     * @var ConfirmInterface
     * @version 1.0.0
     */
    private $_confirmService;

    /**
     * Constructor
     * 
     * @param ConfirmInterface $confirmService Payment confirmation service
     * @version 1.0.0
     */
    public function __construct(
        ConfirmInterface $confirmService
    ) {
        $this->_confirmService = $confirmService;
    }

    /**
     * After can ship
     * 
     * Modifies order shipping capability for ThreeByFour payment methods.
     * 
     * @param OrderInterface $subject Order interface
     * @param bool $result Original result
     * @return bool Modified result
     * @version 1.0.0
     */
    public function afterCanShip(OrderInterface $subject, $result): bool
    {
        if(preg_match("/^threebyfour/", $subject->getPayment()->getMethod(), $test) &&
            $subject->getStatus() != 'processing'
        ) {
            return false;
        }
        return $result;
    }

    /**
     * After can invoice
     * 
     * Modifies order invoicing capability for ThreeByFour payment methods.
     * 
     * @param OrderInterface $subject Order interface
     * @param bool $result Original result
     * @return bool Modified result
     * @version 1.0.0
     */
    public function afterCanInvoice(OrderInterface $subject, $result): bool
    {
        if(preg_match("/^threebyfour/", $subject->getPayment()->getMethod(), $test)
        ) {
            return false;
        }
        return $result;
    }
}
