<?php
/**
 * PayPal Gateway Widget - Render Functions
 *
 * @package GGM_Commerce
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Render the PayPal gateway widget premium content.
 *
 * @param \Elementor\Widget_Base $widget The widget instance.
 * @return void
 */
function ggmc_paypal_gateway_render_premium_content( $widget ) {
	$settings = $widget->get_settings_for_display();
	$gateway  = ggm_commerce_gateways()->get_gateway( 'paypal' );

	// Check if gateway is configured.
	if ( ! $gateway || ! $gateway->is_configured() ) {
		if ( ggmc_is_editor_mode() ) {
			?>
			<div class="ggmc-paypal-container ggmc-not-configured" style="padding: 20px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 4px;">
				<p style="margin: 0; color: #856404;">
					<strong><?php esc_html_e( 'PayPal Not Configured', 'ggm-commerce' ); ?></strong><br>
					<?php esc_html_e( 'Please configure your PayPal API credentials in GGM Commerce → Settings → Payment.', 'ggm-commerce' ); ?>
				</p>
			</div>
			<?php
		}
		return;
	}

	$button_style = $gateway->get_button_style();
	?>
	<div class="ggmc-paypal-container">
		<?php if ( 'yes' === $settings['show_title'] ) : ?>
			<h4 class="ggmc-paypal-title"><?php echo esc_html( $settings['title_text'] ); ?></h4>
		<?php endif; ?>

		<?php if ( 'yes' === $settings['show_tagline'] ) : ?>
			<p class="ggmc-paypal-tagline" style="font-size: 13px; color: #6c757d; margin-bottom: 15px;">
				<?php esc_html_e( 'The safer, easier way to pay', 'ggm-commerce' ); ?>
			</p>
		<?php endif; ?>

		<div id="ggmc-paypal-button-container"
			 data-layout="<?php echo esc_attr( $button_style['layout'] ); ?>"
			 data-color="<?php echo esc_attr( $button_style['color'] ); ?>"
			 data-shape="<?php echo esc_attr( $button_style['shape'] ); ?>"
			 data-label="<?php echo esc_attr( $button_style['label'] ); ?>">
			<!-- PayPal Smart Payment Buttons will be rendered here by JavaScript -->
		</div>
		<div id="ggmc-paypal-payment-message" class="ggmc-payment-message" style="display: none;"></div>
	</div>
	<?php

	// Enqueue PayPal scripts.
	ggmc_paypal_gateway_enqueue_scripts( $gateway );
}

/**
 * Enqueue PayPal JavaScript.
 *
 * @param GGM_Commerce_Gateway_PayPal $gateway Gateway instance.
 * @return void
 */
function ggmc_paypal_gateway_enqueue_scripts( $gateway ) {
	// Only enqueue on frontend, not in editor.
	if ( ggmc_is_editor_mode() ) {
		return;
	}

	$client_id        = $gateway->get_client_id();
	$currency         = strtoupper( ggm_commerce_gateways()->get_gateway( 'paypal' )->get_currency() );
	$disabled_funding = $gateway->get_disabled_funding();

	// Build SDK URL parameters.
	$sdk_params = array(
		'client-id' => $client_id,
		'currency'  => $currency,
		'intent'    => 'capture',
	);

	// Only add disable-funding if there are sources to disable.
	if ( ! empty( $disabled_funding ) ) {
		$sdk_params['disable-funding'] = implode( ',', $disabled_funding );
	}

	$sdk_url = add_query_arg( $sdk_params, 'https://www.paypal.com/sdk/js' );

	wp_enqueue_script( 'paypal-sdk', $sdk_url, array(), null, true );

	wp_enqueue_script(
		'ggmc-paypal-payment',
		plugins_url( 'assets/js/paypal-payment.js', dirname( __DIR__ ) . '/goose-commerce.php' ),
		array( 'jquery', 'paypal-sdk' ),
		defined( 'GGM_COMMERCE_VERSION' ) ? GGM_COMMERCE_VERSION : '1.0.0',
		true
	);

	wp_localize_script( 'ggmc-paypal-payment', 'ggmcPayPal', $gateway->get_js_data() );
}
