<?php
/**
 * Credorax Payments For WooCommerce
 * https://www.credorax.com/
 *
 * @category Credorax
 * @package  WC_Credorax
 * @author   Girit-Interactive (https://www.girit-tech.com/)
 */

 if (! defined('ABSPATH')) {
     exit; // Exit if accessed directly.
 }

/**
 * Credorax Credorax request factory model.
 */
class WC_Credorax_Model_Request_Factory
{

    /**
     * Set of requests.
     *
     * @var array
     */
    private $invokableClasses = [
        WC_Credorax_Model_Request_AbstractPayment::PAYMENT_AUTH_METHOD => WC_Credorax_Model_Request_Payment_Auth::class,
        WC_Credorax_Model_Request_AbstractPayment::PAYMENT_AUTH_TOKENIZATION_METHOD => WC_Credorax_Model_Request_Payment_AuthTokenization::class,
        WC_Credorax_Model_Request_AbstractPayment::PAYMENT_AUTH_USE_TOKEN_METHOD => WC_Credorax_Model_Request_Payment_AuthUseToken::class,
        WC_Credorax_Model_Request_AbstractPayment::PAYMENT_SALE_METHOD => WC_Credorax_Model_Request_Payment_Sale::class,
        WC_Credorax_Model_Request_AbstractPayment::PAYMENT_SALE_TOKENIZATION_METHOD => WC_Credorax_Model_Request_Payment_SaleTokenization::class,
        WC_Credorax_Model_Request_AbstractPayment::PAYMENT_SALE_USE_TOKEN_METHOD => WC_Credorax_Model_Request_Payment_SaleUseToken::class,
        WC_Credorax_Model_Request_AbstractGateway::GATEWAY_CAPTURE_METHOD => WC_Credorax_Model_Request_Gateway_Capture::class,
        WC_Credorax_Model_Request_AbstractGateway::GATEWAY_REFUND_METHOD => WC_Credorax_Model_Request_Gateway_Refund::class,
        WC_Credorax_Model_Request_AbstractGateway::GATEWAY_VOID_METHOD => WC_Credorax_Model_Request_Gateway_Cancel::class,
    ];

    /**
     * Create request model.
     *
     * @param string         $method
     * @param WC_Order|null  $payment
     * @param float          $amount
     *
     * @return WC_Credorax_Model_RequestInterface
     * @throws Exception
     */
    public function create($method, WC_Order $order = null, $amount = 0.0)
    {
        $className = !empty($this->invokableClasses[$method])
            ? $this->invokableClasses[$method]
            : null;

        if ($className === null) {
            throw new \Exception(__('%s method is not supported.', 'woocommerce-gateway-credorax'));
        }

        $model = new $className($order, $amount);

        if (!$model instanceof WC_Credorax_Model_RequestInterface) {
            throw new \Exception(
                __(sprintf('%s doesn\'t implement WC_Credorax_Model_RequestInterface', $className), 'woocommerce-gateway-credorax')
            );
        }

        return $model;
    }
}
