<?php
/**
 * Shortcode for Responsive Text Block element.
 *
 * @package woodmart
 */

if ( ! defined( 'WOODMART_THEME_DIR' ) ) {
	exit( 'No direct script access allowed' );
}

if ( ! function_exists( 'woodmart_shortcode_responsive_text_block' ) ) {
	/**
	 * Shortcode to display responsive text block.
	 *
	 * @param array  $atts Shortcode attributes.
	 * @param string $content Shortcode content.
	 *
	 * @return string
	 */
	function woodmart_shortcode_responsive_text_block( $atts, $content ) {
		$text_wrapper_class = apply_filters( 'vc_shortcodes_css_class', '', '', $atts );

		$atts = shortcode_atts(
			array(
				'text'              => 'Title',
				'font'              => 'primary',
				'font_weight'       => '',
				'content_width'     => '100',
				'color_scheme'      => '',
				'color'             => '',
				'size'              => 'default',
				'align'             => 'center',
				'text_font_size'    => '',
				'inline'            => 'no',

				// Old size
				'desktop_text_size' => '',
				'tablet_text_size'  => '',
				'mobile_text_size'  => '',

				'woodmart_css_id'   => '',
				'css_animation'     => 'none',
				'el_class'          => '',
				'css'               => '',
			),
			$atts
		);

		extract( $atts ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract

		if ( ! $woodmart_css_id ) {
			$woodmart_css_id = uniqid();
		}

		$text_id    = 'wd-' . $woodmart_css_id;
		$style_attr = '';

		$text_wrapper_class .= ' color-scheme-' . $color_scheme;
		$text_wrapper_class .= ' text-' . $align;
		$text_wrapper_class .= 'yes' === $inline ? ' inline-element' : '';
		$text_wrapper_class .= woodmart_get_css_animation( $css_animation );

		if ( $content_width && 'custom' !== $content_width && '100' !== $content_width ) {
			$style_attr         .= ' style="--wd-max-width: ' . $content_width . '%;"';
			$text_wrapper_class .= ' wd-width-enabled';
		} elseif ( 'custom' === $content_width ) {
			$text_wrapper_class .= ' wd-width-custom';
		}

		$text_class  = ' font-' . $font;
		$text_class .= ' wd-font-weight-' . $font_weight;
		$text_class .= ' ' . woodmart_get_new_size_classes( 'text', $size, 'title' );

		if ( function_exists( 'vc_shortcode_custom_css_class' ) ) {
			$text_wrapper_class .= ' ' . vc_shortcode_custom_css_class( $css );
		}

		if ( '' !== $el_class ) {
			$text_wrapper_class .= ' ' . $el_class;
		}

		ob_start();

		woodmart_enqueue_inline_style( 'responsive-text' );
		?>
			<div id="<?php echo esc_attr( $text_id ); ?>" class="wd-text-block-wrapper wd-wpb<?php echo esc_attr( $text_wrapper_class ); ?>"<?php echo wp_kses( $style_attr, true ); ?>>
				<div class="woodmart-title-container woodmart-text-block reset-last-child<?php echo esc_attr( $text_class ); ?>">
					<?php echo do_shortcode( $content ); ?>
				</div>

				<?php
				if ( ( 'custom' === $size && ! $text_font_size ) || ( 'custom' === $color_scheme && ! woodmart_is_css_encode( $color ) ) ) {
					$css = '';

					if ( $desktop_text_size || $color ) {
						$css .= '#' . esc_attr( $text_id ) . ' .woodmart-text-block  {';
						if ( $desktop_text_size ) {
							$css .= 'font-size: ' . esc_attr( $desktop_text_size ) . 'px;';
							$css .= 'line-height: ' . esc_attr( (int) $desktop_text_size + 10 ) . 'px;';
						}

						if ( $color ) {
							$css .= 'color: ' . esc_attr( $color ) . ';';
						}
						$css .= '}';
					}

					if ( function_exists( 'woodmart_responsive_text_size_css' ) ) {
						if ( $tablet_text_size ) {
							$css .= '@media (max-width: 1199px) {';
							$css .= woodmart_responsive_text_size_css( $text_id, 'woodmart-text-block', $tablet_text_size, 'return' );
							$css .= '}';
						}

						if ( $mobile_text_size ) {
							$css .= '@media (max-width: 767px) {';
							$css .= woodmart_responsive_text_size_css( $text_id, 'woodmart-text-block', $mobile_text_size, 'return' );
							$css .= '}';
						}
					}

					wp_add_inline_style( 'woodmart-inline-css', $css );
				}
				?>
			</div>
		<?php
		$output = ob_get_contents();
		ob_end_clean();

		return $output;
	}
}
