<?php
/**
 * Sharing 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\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Settings\Options\Referrals\RewardsOption;
use Awesomemotive\Rewardswp\Settings\Options\Referrals\SharingOption;
use function Awesomemotive\Rewardswp\Helpers\kses;
use function Awesomemotive\Rewardswp\Helpers\tag_attr;
use function Awesomemotive\Rewardswp\Helpers\get_current_member;
use function Awesomemotive\Rewardswp\Helpers\get_option_value;
use function Awesomemotive\Rewardswp\Helpers\get_referral_reward_display_title;
use function Awesomemotive\Rewardswp\Helpers\get_user_first_name;
use function Awesomemotive\Rewardswp\Helpers\is_rewardswp_admin_page;
use function Awesomemotive\Rewardswp\Helpers\replace_placeholders;

class SharingView extends AbstractView implements ViewInterface {
	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {

		/**
		 * Allow plugins to override the settings before rendering.
		 *
		 * @since 1.0.0
		 *
		 * @param array $sharing_options
		 */
		$sharing_options = apply_filters( 'rewardswp_pre_render_sharing_options', get_option_value( SharingOption::class ) );

		// Get advocate data.
		$advocate              = get_current_member();
		$advocate_referral_url = $advocate
			? $advocate->get_referral_url()
			: home_url();

		$is_admin = is_rewardswp_admin_page();

		ob_start();

		if ( $is_admin || ! empty( $sharing_options['x'] ) ) {

			$x_share_url = sprintf(
				'https://twitter.com/intent/tweet?url=%1$s&text=%2$s',
				rawurlencode( $advocate_referral_url ),
				rawurlencode(
					replace_placeholders(
						$sharing_options['x_message'],
						[
							'site_name' => esc_html( get_bloginfo( 'name' ) ),
						]
					)
				)
			);

			?>

			<a
				href="<?php echo esc_url( $x_share_url ); ?>"
				target="_blank"
				rel="noopener noreferrer"
				class="p-2 text-gray-600 hover:text-gray-500 transition-colors duration-200"
				<?php tag_attr( 'x-bind:hidden', $is_admin ? "!sharing.includes('x')" : '' ); ?>
			>
				<svg class="w-6 h-6" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
					<path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/>
				</svg>
			</a>

			<?php

		}

		if ( $is_admin || ! empty( $sharing_options['facebook'] ) ) {

			$fb_share_url = sprintf(
				'https://www.facebook.com/sharer/sharer.php?u=%s',
				rawurlencode( $advocate_referral_url )
			);

			?>

			<a
				href="<?php echo esc_url( $fb_share_url ); ?>"
				target="_blank"
				rel="noopener noreferrer"
				class="p-2 text-gray-600 hover:text-gray-500 transition-colors duration-200"
				<?php tag_attr( 'x-bind:hidden', $is_admin ? "!sharing.includes('facebook')" : '' ); ?>
			>
				<svg class="w-6 h-6" fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
					<path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"/>
				</svg>
			</a>

			<?php

		}

		if ( $is_admin || ! empty( $sharing_options['email'] ) ) {

			$rewards = get_option_value( RewardsOption::class );

			// Get reward name for email placeholder using shared helper.
			$friend_reward = $rewards['friend'] ?? [];
			$reward_name   = get_referral_reward_display_title( $friend_reward );
			if ( '' === $reward_name ) {
				$reward_name = esc_html__( 'reward', 'rewardswp' );
			}

			$email_share_url = sprintf(
				'mailto:?subject=%1$s&body=%2$s%%0A%%0A',
				rawurlencode(
					wp_specialchars_decode(
						sanitize_text_field( $sharing_options['email_subject'] ),
						ENT_QUOTES
					)
				),
				rawurlencode(
					replace_placeholders(
						sanitize_textarea_field(
							$sharing_options['email_message']
						),
						[
							'site_name'     => wp_specialchars_decode( esc_html( get_bloginfo( 'name' ) ), ENT_QUOTES ),
							'reward_name'   => $reward_name,
							'advocate_name' => is_null( $advocate )
								? get_user_first_name( get_current_user_id() )
								: $advocate->get_first_name(),
							'referral_link' => $advocate_referral_url,
						]
					)
				)
			);

			?>

			<!--email_off-->

			<a
				href="<?php echo esc_url( $email_share_url ); ?>"
				class="p-2 text-gray-600 hover:text-gray-500 transition-colors duration-200"
				<?php tag_attr( 'x-bind:hidden', $is_admin ? "!sharing.includes('email')" : '' ); ?>
			>
				<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
					<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path>
				</svg>
			</a>
			<!--/email_off-->

			<?php
		}

		$output = ob_get_clean();

		if ( ! empty( $output ) ) {
			echo wp_kses(
				sprintf( '<div class="flex justify-center space-x-4 mt-5">%s</div>', $output ),
				kses()
			);
		}
	}
}
