<?php
/**
 * Launcher view for rewards panel.
 *
 * @package Awesomemotive\Rewardswp\Views\Panel
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Panel;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Helpers\IconHelper;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Settings\Options\RewardsWidget\LauncherTextOption;
use Awesomemotive\Rewardswp\Settings\Options\RewardsWidget\LauncherDisplayOption;
use Awesomemotive\Rewardswp\Factories\OptionsFactory;
use function Awesomemotive\Rewardswp\Helpers\array_to_json;
use function Awesomemotive\Rewardswp\Helpers\get_option_value;
use function Awesomemotive\Rewardswp\Helpers\is_rewardswp_admin_page;
use function Awesomemotive\Rewardswp\Helpers\tag_attr;

class LauncherView extends AbstractView implements ViewInterface {
	/**
	 * Icon helper instance.
	 *
	 * @since 1.0.0
	 * @var IconHelper
	 */
	private IconHelper $icon_helper;

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

	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args Any additional data to pass to the template, usually used for replacement.
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {

		$launcher_display = get_option_value( LauncherDisplayOption::class );
		$is_admin         = is_rewardswp_admin_page();

		?>

		<template id="rewards-launcher-template">
			<div
				<?php
					tag_attr(
						'x-data',
						sprintf(
							'launcher(%s)',
							array_to_json(
								[
									'display'    => $is_admin
										? null
										: esc_js( $launcher_display ),
									'buttonText' => $is_admin
										? null
										: esc_js( get_option_value( LauncherTextOption::class ) ),
								]
							)
						)
					);
				?>
				class="flex justify-end"
				x-bind:hidden="!isVisible"
				x-on:show-launcher.window="show"
			>
				<button
					x-cloak
					x-on:click="toggleRewardsPanel"
					id="giftIcon"
					class="flex items-center justify-center gap-3 text-white shadow-lg cursor-pointer bg-brand hover:bg-brand-hover focus:outline-none focus:ring-2 focus:ring-brand focus:ring-offset-2"
					x-bind:class="'icon_only' === <?php echo esc_js( $is_admin ? 'launcher.display' : 'display' ); ?> ? 'p-4 rounded-full' : 'px-6 py-4 rounded-full'"
					aria-label="<?php esc_html_e( 'Toggle Rewards', 'rewardswp' ); ?>"
				>
					<?php if ( $is_admin || $launcher_display !== 'text_only' ) : ?>
						<span
							aria-hidden="true"
							<?php tag_attr( ':hidden', $is_admin ? "'text_only' === launcher.display" : '' ); ?>
						>
							<?php $this->icon_helper->render_icon( 'gift', '', 'bold' ); ?>
						</span>
					<?php endif; ?>

					<?php if ( $is_admin || $launcher_display !== 'icon_only' ) : ?>
						<span
							class="text-base"
							<?php tag_attr( 'x-text', esc_js( $is_admin ? 'launcher.buttonText' : 'buttonText' ) ); ?>
							<?php tag_attr( ':hidden', $is_admin ? "'icon_only' === launcher.display" : '' ); ?>
						></span>
					<?php endif; ?>
				</button>
			</div>
		</template>

		<?php
	}
}
