<?php
/**
 * Apply code button view component.
 *
 * @package Awesomemotive\Rewardswp\Views\Panel\Parts
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Panel\Parts;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Controllers\ClaimController;
use Awesomemotive\Rewardswp\Interfaces\IntegrationInterface;
use Awesomemotive\Rewardswp\Interfaces\IntegrationWithCouponInterface;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Models\Reward;
use function Awesomemotive\Rewardswp\Helpers\get_reward;


/**
 * Apply Code Button View.
 *
 * @since 1.0.0
 */
class ApplyCodeButtonView extends AbstractView implements ViewInterface {
	/**
	 * Integration instance.
	 *
	 * @since 1.0.0
	 *
	 * @var IntegrationInterface
	 */
	private IntegrationInterface $integration;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param IntegrationInterface|IntegrationWithCouponInterface $integration
	 */
	public function __construct( IntegrationInterface $integration ) {
		$this->integration = $integration;
	}

	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args {
	 *     @type string $discount_code_coupon The reward coupon code. Default ''.
	 * }
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {
		$args = wp_parse_args(
			$args,
			[
				'discount_code_coupon' => '',
				'reward_id'            => 0,
			]
		);

		$reward = get_reward( absint( $args['reward_id'] ) );

		$button_text         = is_a( $reward, Reward::class ) && 'free_product' === $reward->reward_type
			? esc_html__( 'Add product to cart', 'rewardswp' )
			: esc_html__( 'Apply coupon', 'rewardswp' );
		$button_text_applied = is_a( $reward, Reward::class ) && 'free_product' === $reward->reward_type
			? esc_html__( 'Product added to cart', 'rewardswp' )
			: esc_html__( 'Coupon applied', 'rewardswp' );

		?>

		<div
			x-data="couponUtils( {
				nonce: '<?php echo esc_attr( wp_create_nonce( ClaimController::AJAX_NONCE ) ); ?>',
				buttonText: '<?php echo esc_html( $button_text ); ?>',
				rewardId: <?php echo absint( $args['reward_id'] ); ?>,
				appliedButtonText: '<?php echo esc_html( $button_text_applied ); ?>',
				isApplied: Boolean( <?php echo absint( $this->integration->is_coupon_applied( $args['discount_code_coupon'] ?? '' ) ); ?> )
			} )"
			@reset-buttons.window="if ( $event.detail.except !== $el ) isApplied = false;"
		>
			<button
				id="copy-link"
				class="w-full px-4 py-3 mt-5 font-bold text-white transition-all duration-300 ease-out rounded-md bg-brand hover:bg-brand-hover focus:outline-none focus:ring-2 focus:ring-brand-600 focus:ring-opacity-50 shrink-0 min-w-[120px] grid grid-cols-1 grid-rows-1 [grid-template-areas:'stack'] place-items-center opacity-50 cursor-not-allowed"
				x-on:click="applyCoupon( '<?php echo esc_attr( $args['discount_code_coupon'] ); ?>' )"
				x-bind:disabled="isApplying || isApplied"
				x-bind:class="{
					'opacity-50 cursor-not-allowed': isApplying || isApplied,
					'opacity-100 cursor-pointer': !isApplying && ! isApplied
				}"
			>
				<span
					class="[grid-area:stack] flex items-center justify-center transition-opacity duration-200"
					x-bind:class="{ 'invisible': isApplying, 'visible': !isApplying }"
					x-text="isApplied ? appliedButtonText : buttonText"
				></span>
				<svg
					class="animate-spin h-5 w-5 [grid-area:stack] transition-opacity duration-200"
					x-bind:class="{ 'invisible': ! isApplying, 'visible': isApplying }"
					xmlns="http://www.w3.org/2000/svg"
					fill="none"
					viewBox="0 0 24 24"
				>
					<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
					<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
				</svg>
			</button>
			<div x-show="hasErrors" class="text-sm sm:ml-4 mt-2 sm:mt-0 text-red-500"><?php esc_html_e( 'An error occurred. Please reload the page and try again.', 'rewardswp' ); ?></div>
		</div>

		<?php
	}
}
