<?php

use EVPB\Tools\Helper;
use EVPB\Tools\ToCSS;
use EVPB\Tools\AdvancedTab;

$prefix = 'evbText';
$id = wp_unique_id( "$prefix-" );

if( !function_exists( 'getTextColorCSS' ) ) {
	function getTextColorCSS( $c ) {
		$c = $c ?? [];
		$type		= $c['type'] ?? 'color';
		$color		= $c['color'] ?? null;
		$gradient	= $c['gradient'] ?? null;
		$image		= $c['image'] ?? [];

		if ( 'color' === $type ) {
			return ! empty( $color ) ? "
				color: $color;
				background: transparent;
				-webkit-background-clip: unset;
				background-clip: unset;
				-webkit-text-fill-color: unset;
			" : '';
		}

		$url		= $image['url'] ?? null;
		$position	= $image['position'] ?? 'center center';
		$size		= $image['size'] ?? 'cover';
		$repeat		= $image['repeat'] ?? 'no-repeat';

		$backgroundValue = ( 'image' === $type && ! empty( $url ) )
			? "url( $url ) $position / $size $repeat"
			: ( $gradient ?? null );

		return ! empty( $backgroundValue ) ? "
			color: transparent;
			-webkit-text-fill-color: transparent;
			background: $backgroundValue;
			-webkit-background-clip: text;
			background-clip: text;
		" : '';
	}
}
if( !function_exists( 'evbTextDeviceCSS' ) ) {
	function evbTextDeviceCSS( $attributes, $id, $device ) {
		$prefix = 'evbText';
		extract( $attributes );

		return "
			#$id {". ToCSS::hasCSS( 'text-align', $styles['alignment'][$device] ?? '' ) ."}
			#$id .$prefix * {". ToCSS::hasCSS( 'text-align', $styles['textAlign'][$device] ?? '' ) ."}
		";
	}
}
if( !function_exists( 'evbTextStyle' ) ) {
	function evbTextStyle( $attributes, $id ) {
		$prefix = 'evbText';
		extract( $attributes );

		// Selectors
		$mainSl = "#$id";
		$textSl = "$mainSl .$prefix";
		$tagSl = "$textSl *";

		$styles = ToCSS::typoCSS( $styles['typography'], $tagSl ) . 
			evbTextDeviceCSS( $attributes, $id, 'desktop' ) ."
			$tagSl {".
				getTextColorCSS( $styles['color']['normal'] ) .
				ToCSS::hasCSS( 'text-shadow', $styles['shadow']['normal'] ?? '' ) ."
			}
			$textSl:hover *{".
				getTextColorCSS( $styles['color']['hover'] ) .
				ToCSS::hasCSS( 'text-shadow', $styles['shadow']['hover'] ) ."
			}".

			Helper::TAB_BREAKPOINT ."{".
				evbTextDeviceCSS( $attributes, $id, 'tablet' ) ."
			}".

			Helper::MOB_BREAKPOINT ."{".
				evbTextDeviceCSS( $attributes, $id, 'mobile' ) ."
			}
		";

		return $styles;
	}
}

extract( $attributes ?? [] );

if( isset( $advanced['animation']['type'] ) && !empty( $advanced['animation']['type'] ) ) {
	wp_enqueue_style( 'aos' );
	wp_enqueue_script( 'aos' );
}
?>
<div
	<?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>
	id='<?php echo esc_attr( $id ); ?>'
	<?php echo wp_kses_data( AdvancedTab::animationProps( $advanced['animation'] ?? null ) ); ?>
>
	<style>
		<?php echo AdvancedTab::generateCSS( "#$id", "#$id .$prefix", $advanced ); ?>
		<?php echo html_entity_decode( esc_html( evbTextStyle( $attributes, $id ) ) ); ?>
	</style>

	<div <?php echo AdvancedTab::getAdvancedAttributes( $advanced['attributes'] ?? [], [ 'class' => $prefix ] ); ?>>
		<<?php echo esc_attr( $tag ); ?>>
			<?php echo wp_kses( $text, Helper::allowedHTML() ); ?>
		</<?php echo esc_attr( $tag ); ?>>
	</div>
</div>