<?php
/**
 * Icon_Box View file
 *
 * @category   View
 * @package    CodexShaper_Framework
 * @author     CodexShaper <info@codexshaper.com>
 * @license    https://www.gnu.org/licenses/gpl-2.0.html
 * @link       https://codexshaper.com
 * @since      1.0.0
 */

use Elementor\Icons_Manager;
use Elementor\Utils;

// Exit if access directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit();
}
$has_link = ! empty( $settings['icon_link']['url'] );
$html_tag = $has_link ? 'a' : 'span';

$instance->add_render_attribute( 'icon', 'class', 'csmf--icon' );

if ( ! empty( $settings['hover_animation'] ) ) {
	$instance->add_render_attribute( 'icon', 'class', 'elementor-animation-' . $settings['hover_animation'] );
}

$has_icon    = ! empty( $settings['selected_icon']['value'] );
$has_content = ! Utils::is_empty( $settings['title_text'] ) || ! Utils::is_empty( $settings['description_text'] ) || ! Utils::is_empty( $settings['enable_button'] );

if ( ! $has_icon && ! $has_content ) {
	return;
}

if ( $has_link ) {
	$instance->add_link_attributes( 'icon_link', $settings['icon_link'] );
	$instance->add_render_attribute( 'icon', 'tabindex', '-1' );
	if ( ! empty( $settings['title_text'] ) ) {
		$instance->add_render_attribute( 'icon', 'aria-label', $settings['title_text'] );
	}
}

if ( ! isset( $settings['icon'] ) && ! Icons_Manager::is_migration_allowed() ) {
	// Add old default.
	$settings['icon'] = 'fa fa-star';
}

if ( ! empty( $settings['icon'] ) ) {
	$instance->add_render_attribute( 'i', 'class', $settings['icon'] );
	$instance->add_render_attribute( 'i', 'aria-hidden', 'true' );
}

$instance->add_render_attribute( 'description_text', 'class', 'csmf--icon-box-description' );

$migrated  = isset( $settings['__fa4_migrated']['selected_icon'] );
$is_new    = ! isset( $settings['icon'] ) && Icons_Manager::is_migration_allowed();
$icon_html = sprintf(
	'<div class="csmf--icon-box-icon">
        <%1$s %2$s %3$s>
            %4$s
        </%1$s>
    </div>',
	Utils::validate_html_tag( $html_tag ),
	$instance->get_render_attribute_string( 'icon_link' ),
	$instance->get_render_attribute_string( 'icon' ),
	( $is_new || $migrated ) ? Icons_Manager::try_get_icon_html( $settings['selected_icon'], array( 'aria-hidden' => 'true' ) )
		: ( ! empty( $settings['icon'] ) ? sprintf( '<i %s></i>', $instance->get_render_attribute_string( 'i' ) ) : '' )
);

$description = $settings['description_text'];
$title_html  = sprintf(
	! Utils::is_empty( $settings['title_text'] ) ? sprintf(
		'<%1$s class="csmf--icon-box-title">
            <%2$s %3$s %4$s>
                %5$s
            </%2$s>
        </%1$s>',
		Utils::validate_html_tag( $settings['title_size'] ),
		Utils::validate_html_tag( $html_tag ),
		$instance->get_render_attribute_string( 'icon_link' ),
		$instance->get_render_attribute_string( 'title_text' ),
		$settings['title_text']
	) : '',
);

$description_html = sprintf(
	! Utils::is_empty( $settings['description_text'] ) ? sprintf(
		'<%1$s %2$s>
            %3$s
        </%1$s>',
		Utils::validate_html_tag( 'div' ),
		$instance->get_render_attribute_string( 'description_text' ),
		$description
	) : '',
);

$badge_position = ! empty( $settings['icon_box_badge_position'] ) ? 'csmf--position-' . esc_attr( $settings['icon_box_badge_position'] ) : 'csmf--position-top-right';
$badge_title    = ! empty( $settings['icon_box_badge_title'] ) ? $settings['icon_box_badge_title'] : '';

$badge = sprintf(
	'<div class="csmf--icon-box-badge %s">
        <span class="csmf--badge">%s</span>
    </div>',
	$badge_position,
	$badge_title,
);

?>
<div class="csmf--icon-box-wrapper">
	<?php
	if ( $settings['enable_badge'] ) :
		Utils::print_unescaped_internal_string( $badge );
	endif;
	?>
	<?php
	if ( $has_icon && 'before' === $settings['icon_position'] ) :
		Utils::print_unescaped_internal_string( $icon_html );
	endif;
	?>
	<?php if ( $has_content ) : ?>
		<div class="csmf--icon-box-content">
			<?php Utils::print_unescaped_internal_string( $title_html ); ?>
			<?php
			if ( $has_icon && 'after_title' === $settings['icon_position'] ) :
				Utils::print_unescaped_internal_string( $icon_html );
			endif;
			?>
			<?php
			Utils::print_unescaped_internal_string( $description_html );
			if ( 'yes' === $settings['enable_button'] ) :
				Utils::print_unescaped_internal_string( $button );
			endif;
			?>
		</div>
		<?php
	endif;
	if ( $has_icon && 'after' === $settings['icon_position'] ) :
		Utils::print_unescaped_internal_string( $icon_html );
	endif;
	?>
</div>
