<?php
/**
 * Image_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\Utils;

// Exit if access directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit();
}
$has_image   = ! empty( $settings['image']['url'] );
$has_content = ! Utils::is_empty( $settings['title_text'] ) || ! Utils::is_empty( $settings['description_text'] );

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

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

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


$image_size   = $settings['thumbnail_size'] ?? 'full';
$image        = $settings['image'] ?? null;
$image_id     = $image['id'] ?? null;
$is_lazy_load = 'yes' === $settings['lazy_load'];
$size_image   = $instance->get_size_image(
	image_id: $image_id,
	size: $image_size,
	is_custom_lazy: $is_lazy_load,
	attributes: array(
		'alt'          => 'Image',
		'class'        => 'featured-image elementor-animation-' . $settings['hover_animation'],
		'fallback_url' => $image_id > 0 ? '' : $image['url'],
	)
);

$html = '<div class="csmf--image-box-wrapper">';

if ( 'yes' === $settings['enable_badge'] && ! empty( $settings['image_box_badge_title'] ) ) {
	$html .= $badge;
}

if ( ! empty( $settings['image_link']['url'] ) ) {
	$instance->add_link_attributes( 'image_link', $settings['image_link'] );
}

if ( 'yes' === $settings['is_enable_title_link'] && ( ! empty( $settings['image_link']['url'] ) || ! empty( $settings['title_link']['url'] ) ) ) {
	$instance->add_link_attributes( 'title_link', '' === $settings['title_link']['url'] ? $settings['image_link'] : $settings['title_link'] );
}

if ( $has_image && 'before' === $settings['image_position'] ) {

	$image_html = wp_kses_post( $size_image );

	if ( 'yes' === $settings['is_enable_image_link'] && ! empty( $settings['image_link']['url'] ) ) {
		$image_html = '<a ' . $instance->get_render_attribute_string( 'image_link' ) . ' tabindex="-1">' . $image_html . '</a>';
	}

	$html .= '<figure class="csmf--image-box-img">' . $image_html . '</figure>';
}

if ( $has_content ) {
	$html .= '<div class="csmf--image-box-content">';

	if ( ! Utils::is_empty( $settings['title_text'] ) ) {
		$instance->add_render_attribute( 'title_text', 'class', 'csmf--image-box-title' );

		$title_html = $settings['title_text'];

		if ( 'yes' === $settings['is_enable_title_link'] && ( ! empty( $settings['image_link']['url'] ) || ! empty( $settings['title_link']['url'] ) ) ) {
			$title_html = '<a ' . $instance->get_render_attribute_string( 'title_link' ) . '>' . $title_html . '</a>';
		}

		$html .= sprintf( '<%1$s %2$s>%3$s</%1$s>', Utils::validate_html_tag( $settings['title_size'] ), $instance->get_render_attribute_string( 'title_text' ), $title_html );
	}

	if ( $has_image && 'after_title' === $settings['image_position'] ) {

		$image_html = wp_kses_post( $size_image );

		if ( 'yes' === $settings['is_enable_image_link'] && ! empty( $settings['image_link']['url'] ) ) {
			$image_html = '<a ' . $instance->get_render_attribute_string( 'image_link' ) . ' tabindex="-1">' . $image_html . '</a>';
		}

		$html .= '<figure class="csmf--image-box-img">' . $image_html . '</figure>';
	}

	if ( ! Utils::is_empty( $settings['description_text'] ) ) {
		$instance->add_render_attribute( 'description_text', 'class', 'csmf--image-box-description' );

		$html .= sprintf( '<p %1$s>%2$s</p>', $instance->get_render_attribute_string( 'description_text' ), $settings['description_text'] );
	}

	$html .= '</div>';
}

if ( $has_image && 'after' === $settings['image_position'] ) {

	$image_html = wp_kses_post( $size_image );

	if ( 'yes' === $settings['is_enable_image_link'] && ! empty( $settings['image_link']['url'] ) ) {
		$image_html = '<a ' . $instance->get_render_attribute_string( 'image_link' ) . ' tabindex="-1">' . $image_html . '</a>';
	}

	$html .= '<figure class="csmf--image-box-img">' . $image_html . '</figure>';
}

if ( 'yes' === $settings['enable_button'] ) {
	$html .= csmf_view_render( 'button.content', compact( 'settings', 'instance' ) );

}

$html .= '</div>';

Utils::print_unescaped_internal_string( $html );
