<?php
namespace EasyInvoice;

use EasyInvoice\Interfaces\PaymentGatewayInterface;

class PaymentGatewayManager {
    /**
     * Registered payment gateways
     * 
     * @var array
     */
    private $gateways = [];

    /**
     * Register a payment gateway
     * 
     * @param PaymentGatewayInterface $gateway
     */
    public function registerGateway(PaymentGatewayInterface $gateway): void {
        $this->gateways[$gateway->getName()] = $gateway;
    }

    /**
     * Get all registered payment gateways
     * 
     * @return array
     */
    public function getGateways(): array {
        return $this->gateways;
    }
    
    /**
     * Get gateway display name (custom or default)
     * 
     * @param string $gateway_id
     * @return string
     */
    public function getGatewayDisplayName(string $gateway_id): string {
        // Check for custom display name first
        $custom_name = get_option('easy_invoice_gateway_display_name_' . $gateway_id, '');
        if (!empty($custom_name)) {
            return $custom_name;
        }
        
        // Fall back to gateway's default title
        if (isset($this->gateways[$gateway_id])) {
            $gateway = $this->gateways[$gateway_id];
            if (method_exists($gateway, 'getTitle')) {
                return $gateway->getTitle();
            }
        }
        
        // Final fallback
        return ucfirst(str_replace('_', ' ', $gateway_id));
    }

    /**
     * Get enabled payment gateways
     * 
     * @return array
     */
    public function getEnabledGateways(): array {
        $enabled_gateways_unsorted = array_filter($this->gateways, function($gateway) {
            /** @var PaymentGatewayInterface $gateway */
            return $gateway->isEnabled();
        });

        $gateway_order = get_option('easy_invoice_payment_gateway_order', array());
        
        $sorted_gateways = array();

        if (!empty($gateway_order)) {
            foreach ($gateway_order as $gateway_id) {
                if (isset($enabled_gateways_unsorted[$gateway_id])) {
                    $sorted_gateways[$gateway_id] = $enabled_gateways_unsorted[$gateway_id];
                    unset($enabled_gateways_unsorted[$gateway_id]); // Remove from unsorted list
                }
            }
        }

        // Append any remaining enabled gateways that were not in the order array
        // (e.g., newly activated gateways not yet ordered)
        // These will be appended in their original registration order relative to each other.
        $sorted_gateways = array_merge($sorted_gateways, $enabled_gateways_unsorted);

        return $sorted_gateways;
    }

    /**
     * Get a specific payment gateway
     * 
     * @param string $name
     * @return PaymentGatewayInterface|null
     */
    public function getGateway(string $name): ?PaymentGatewayInterface {
        return $this->gateways[$name] ?? null;
    }

    /**
     * Initialize all payment gateways
     */
    public function initGateways(): void {
        foreach ($this->gateways as $gateway) {
            $gateway->init();
        }
    }
} 