<?php
/**
 * Claim reward success view.
 *
 * @package Awesomemotive\Rewardswp\Views\Panel\Friend
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Panel\Friend;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Factories\ViewsFactory;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Helpers\IconHelper;
use Awesomemotive\Rewardswp\Views\Panel\Parts\ApplyCodeButtonView;
use function Awesomemotive\Rewardswp\Helpers\kses;


/**
 * Claim Reward Success View.
 *
 * @since 1.0.0
 */
class ClaimRewardSuccessView extends AbstractView implements ViewInterface {
	/**
	 * The icon helper instance.
	 *
	 * @since 1.0.0
	 *
	 * @var IconHelper
	 */
	private IconHelper $icon_helper;

	/**
	 * The view factory.
	 *
	 * @since 1.0.0
	 *
	 * @var ViewsFactory
	 */
	private ViewsFactory $views_factory;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param IconHelper   $icon_helper
	 * @param ViewsFactory $views_factory
	 */
	public function __construct( IconHelper $icon_helper, ViewsFactory $views_factory ) {
		$this->icon_helper   = $icon_helper;
		$this->views_factory = $views_factory;
	}

	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args {
	 *     @type string $coupon_code The coupon code generated. Default ''.
	 *     @type string $reward_id   The reward ID. Default 0
	 * }
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {
		$args = wp_parse_args(
			$args,
			[
				'coupon_code'     => '',
				'reward_id'       => 0,
				'success_message' => sprintf(
					'<p class="mb-4 text-gray-600">%s</p>',
					esc_html__( 'Your reward has been claimed! Use the coupon code below at checkout:', 'rewardswp' )
				),
			]
		);

		?>

		<?php echo wp_kses( $args['success_message'], kses() ); ?>
		<div
			x-data="copyHandler()"
		>
			<div class="flex rounded-md bg-white outline-1 -outline-offset-1 outline-gray-200">
				<input
					type="text"
					readonly
					x-ref="couponCode"
					value="<?php echo esc_attr( $args['coupon_code'] ); ?>"
					x-on:click="copyContents($refs.couponCode.value)"
					class="block min-w-0 grow px-5 py-3 text-base text-gray-900 focus:outline-none sm:text-base/6 text-ellipsis"
				>
				<div class="flex py-3 pr-5">
					<button
						x-on:click="copyContents($refs.couponCode.value)"
						class="cursor-pointer text-gray-500 hover:text-gray-600 transition-all duration-200"
						x-data="{ copied: false }"
						title="<?php esc_attr_e( 'Copy coupon code', 'rewardswp' ); ?>"
						aria-label="<?php esc_attr_e( 'Copy coupon code to clipboard', 'rewardswp' ); ?>"
					>
						<template x-if="!copied">
							<span aria-hidden="true">
								<?php $this->icon_helper->render_icon( 'copy' ); ?>
							</span>
						</template>
						<template x-if="copied">
							<span aria-hidden="true">
								<?php $this->icon_helper->render_icon( 'copied' ); ?>
							</span>
						</template>
					</button>
				</div>
			</div>

			<?php

			$this->views_factory->get_view( ApplyCodeButtonView::class )->render(
				array_filter(
					[
						'discount_code_coupon' => $args['coupon_code'],
						'reward_id'            => $args['reward_id'],
					]
				)
			);

			?>
		</div>

		<?php
	}
}
