<?php
/**
 * Template part for displaying an Advertising Banner.
 *
 * Both Free and Pro versions use repeater data passed via $args.
 * Free: limited to 1 item. Pro: unlimited items with extra fields.
 *
 * @package Blog_Over
 */

if (!defined('ABSPATH')) {
    exit;
}

// All data comes from $args (repeater item)
if (empty($args) || !is_array($args)) {
    return;
}

$ad_title       = isset($args['ad_title'])           ? $args['ad_title']           : '';
$ad_title_align = isset($args['ad_title_alignment']) && !empty($args['ad_title_alignment']) ? $args['ad_title_alignment'] : 'center';
$ad_description = isset($args['ad_description'])     ? $args['ad_description']     : '';
$banner_layout  = isset($args['banner_layout'])      ? $args['banner_layout']      : 'classic';
$ad_type        = isset($args['ad_type'])            ? $args['ad_type']            : 'image';
$image          = isset($args['desktop_image'])      ? $args['desktop_image']      : '';
$mobile_img     = isset($args['mobile_image'])       ? $args['mobile_image']       : '';
$custom_html    = isset($args['custom_html'])        ? $args['custom_html']        : '';
$link           = isset($args['banner_link'])        ? $args['banner_link']        : '';
$new_tab        = isset($args['new_tab']) && $args['new_tab'] ? true : false;
$btn_text       = isset($args['btn_text'])           ? $args['btn_text']           : '';
$btn_link       = isset($args['btn_link'])           ? $args['btn_link']           : '';
$btn_align      = isset($args['btn_alignment']) && !empty($args['btn_alignment']) ? $args['btn_alignment'] : 'center';

// Pro-only fields (gracefully ignored if not set)
$badge_label    = isset($args['badge_label'])        ? $args['badge_label']        : '';
$closeable      = isset($args['closeable']) && $args['closeable'] ? true : false;

// Dimensions (per-banner)
$max_width     = isset($args['banner_max_width']) ? absint($args['banner_max_width']) : 100;
$banner_height = isset($args['banner_height'])    ? absint($args['banner_height'])    : 0;

// Build inline style for image dimensions
$img_style = '';
if ($max_width > 0 && $max_width < 100) {
    $img_style .= 'max-width:' . $max_width . '%;';
}
if ($banner_height > 0) {
    $img_style .= 'height:' . $banner_height . 'px;';
}

// Button link falls back to the banner link
if (empty($btn_link) && !empty($link)) {
    $btn_link = $link;
}

$target_attr = $new_tab ? ' target="_blank" rel="noopener noreferrer"' : '';

// Nothing to render for image type without an image (unless in Customizer preview)
if ($ad_type === 'image' && empty($image) && !is_customize_preview()) {
    return;
}

// Build banner wrapper classes
$banner_classes = array('blog-over-ad-banner', 'banner-' . $banner_layout);
?>

<div class="<?php echo esc_attr(implode(' ', $banner_classes)); ?>">

    <?php if ($closeable) : ?>
        <button type="button" class="blog-over-ad-close" aria-label="<?php esc_attr_e('Close advertisement', 'blog-over'); ?>">
            <span aria-hidden="true">&times;</span>
        </button>
    <?php endif; ?>

    <?php if (!empty($badge_label)) : ?>
        <span class="blog-over-ad-badge"><?php echo esc_html($badge_label); ?></span>
    <?php endif; ?>

    <?php if ($ad_type === 'custom_html' && !empty($custom_html)) : ?>
        <!-- Custom HTML / AdSense -->
        <div class="blog-over-ad-custom-html">
            <?php echo $custom_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- user-controlled ad code (AdSense etc.) ?>
        </div>
    <?php else : ?>
        <!-- Image Banner -->
        
        <div class="blog-over-ad-media">
            <?php if (!empty($link)) : ?>
                <a href="<?php echo esc_url($link); ?>"<?php echo $target_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- hardcoded safe attributes ?> class="blog-over-ad-banner-link">
            <?php endif; ?>

            <?php if (!empty($image)) : ?>
                <img src="<?php echo esc_url($image); ?>"
                     alt="<?php echo esc_attr(!empty($ad_title) ? $ad_title : __('Advertisement', 'blog-over')); ?>"
                     class="blog-over-ad-banner-img<?php echo !empty($mobile_img) ? ' blog-over-ad-desktop-only' : ''; ?>"
                     <?php echo $img_style ? 'style="' . esc_attr($img_style) . '"' : ''; ?>
                     loading="lazy">
            <?php endif; ?>

            <?php if (!empty($mobile_img)) : ?>
                <img src="<?php echo esc_url($mobile_img); ?>"
                     alt="<?php echo esc_attr(!empty($ad_title) ? $ad_title : __('Advertisement', 'blog-over')); ?>"
                     class="blog-over-ad-banner-img blog-over-ad-mobile-only"
                     loading="lazy">
            <?php endif; ?>

            <?php if (!empty($link)) : ?>
                </a>
            <?php endif; ?>
        </div>
        
        <?php if ((!empty($ad_title) && $ad_title !== 'Default Banner') || !empty($ad_description) || !empty($btn_text)) : ?>
        <div class="blog-over-ad-content ad-title-align-<?php echo esc_attr($ad_title_align); ?> ad-btn-align-<?php echo esc_attr($btn_align); ?>">
            <?php if (!empty($ad_title) && $ad_title !== 'Default Banner') : ?>
                <h4 class="blog-over-ad-title"<?php echo !empty($ad_title_align) ? ' style="text-align: ' . esc_attr($ad_title_align) . ';"' : ''; ?>><?php echo esc_html($ad_title); ?></h4>
            <?php endif; ?>

            <?php if (!empty($ad_description)) : ?>
                <p class="blog-over-ad-description"<?php echo !empty($ad_title_align) ? ' style="text-align: ' . esc_attr($ad_title_align) . ';"' : ''; ?>><?php echo esc_html($ad_description); ?></p>
            <?php endif; ?>
            
            <?php if (!empty($btn_text)) : ?>
                <div class="blog-over-ad-banner-btn-wrap"<?php echo !empty($btn_align) ? ' style="text-align: ' . esc_attr($btn_align) . ';"' : ''; ?>>
                    <a href="<?php echo esc_url($btn_link); ?>"
                       class="blog-over-ad-banner-btn"
                       <?php echo $target_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>><?php echo esc_html($btn_text); ?></a>
                </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>

    <?php endif; ?>
</div>
