<?php
/**
 * Discount code view for reward types.
 *
 * @package Awesomemotive\Rewardswp\Views\Panel\RewardTypes
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Panel\RewardTypes;

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

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

class DiscountCodeView 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
	 *
	 * @see IconHelper For the icon names.
	 *
	 * @param array $args {
	 *     @type string $discount_code_instructions The instructions on how to use the reward. Default ''.
	 *     @type string $discount_code_notice       A notice with details about the reward expiration. Default ''.
	 *     @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_instructions' => '',
				'discount_code_notice'       => '',
				'discount_code_coupon'       => '',
				'reward_id'                  => '',
			]
		);

		?>

		<p class="mb-4 text-gray-600"><?php echo wp_kses( $args['discount_code_instructions'], kses() ); ?></p>
		<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="discountCode"
					value="<?php echo esc_attr( $args['discount_code_coupon'] ); ?>"
					x-on:click="copyContents($refs.discountCode.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.discountCode.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 discount code', 'rewardswp' ); ?>"
						aria-label="<?php esc_attr_e( 'Copy discount 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>
		</div>
		<p class="text-sm text-gray-500 mt-3"><?php echo esc_html( $args['discount_code_notice'] ); ?></p>

		<?php $this->views_factory->get_view( ApplyCodeButtonView::class )->render( $args ); ?>

		<?php
	}
}
