<?php

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

use Calio\Widgets\PostTimelineHelper as Helper;
use Elementor\Group_Control_Image_Size;

/**
 * Template Name: Default
 */

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

$postId = get_the_ID();
$imageSize = sanitize_html_class((string) ($settings['image_size'] ?? 'medium'));
$imageClass = " attachment-{$imageSize} size-{$imageSize}";
$titleTag = Helper::eael_validate_html_tag((string) ($settings['title_tag'] ?? 'h2'));
$dateData = Helper::get_timeline_date_data($settings, $postId);
$showDateBubble = !empty($dateData['label']) && (($dateData['mode'] ?? 'bubble') === 'bubble');
$headerMeta = Helper::render_timeline_meta($settings, 'before_title', $postId);
$footerMeta = Helper::render_timeline_meta($settings, 'after_excerpt', $postId);
$badgeHtml = Helper::render_timeline_badge($settings, $postId);
$readMoreHtml = Helper::render_timeline_read_more($settings);

$postTimelineImageUrl = Group_Control_Image_Size::get_attachment_image_src(get_post_thumbnail_id($postId), 'image', $settings);
if (empty($postTimelineImageUrl)) {
    $fallback = $settings['eael_post_carousel_fallback_img_all']['url'] ?? '';
    $postTimelineImageUrl = is_string($fallback) ? $fallback : '';
}

$nofollow = in_array((string) ($settings['timeline_link_nofollow'] ?? ''), ['true', 'yes', '1'], true);
$targetBlank = in_array((string) ($settings['timeline_link_target_blank'] ?? ''), ['true', 'yes', '1'], true);
$relValues = [];
if ($nofollow) {
    $relValues[] = 'nofollow';
}
if ($targetBlank) {
    $relValues[] = 'noopener';
    $relValues[] = 'noreferrer';
}
$showImage = (($settings['eael_show_image'] ?? 'yes') === 'yes') && $postTimelineImageUrl !== '';
$showExcerpt = !empty($settings['eael_show_excerpt']);
$showTitle = !empty($settings['eael_show_title']);
$isHidden = !empty($GLOBALS['calio_post_timeline_hidden']);
$timelineClasses = 'eael-timeline-post eael-timeline-column calio-post-timeline-item';
if ($isHidden) {
    $timelineClasses .= ' is-hidden';
}

echo '<article class="' . esc_attr($timelineClasses) . '"' . ($isHidden ? ' hidden' : '') . '>';
echo '<div class="eael-timeline-bullet"></div>';
if ($showDateBubble) {
    echo '<time datetime="' . esc_attr((string) ($dateData['datetime'] ?? '')) . '">' . esc_html((string) ($dateData['label'] ?? '')) . '</time>';
}
echo '<div class="eael-timeline-post-inner">';
echo '<a class="eael-timeline-post-link" href="' . esc_url(get_the_permalink()) . '" title="' . esc_attr(get_the_title()) . '"';
if ($relValues !== []) {
    echo ' rel="' . esc_attr(implode(' ', array_unique($relValues))) . '"';
}
if ($targetBlank) {
    echo ' target="' . esc_attr('_blank') . '"';
}
echo '>';
echo '<div class="eael-timeline-post-image ' . esc_attr($imageClass) . '"' . ($showImage ? ' style="background-image: url(' . esc_url($postTimelineImageUrl) . ');"' : '') . '></div>';

if ($showExcerpt) {
    echo '<div class="eael-timeline-post-excerpt">';

    if (empty($settings['eael_excerpt_length'])) {
        $content = strip_shortcodes(get_the_excerpt() ? get_the_excerpt() : (string) get_the_content());
        echo '<p>' . wp_kses($content, Helper::eael_allowed_tags()) . '</p>';
    } else {
        $content = wp_trim_words(
            strip_shortcodes(get_the_excerpt() ? get_the_excerpt() : (string) get_the_content()),
            (int) $settings['eael_excerpt_length'],
            sanitize_text_field((string) ($settings['expanison_indicator'] ?? '...'))
        );
        echo '<p>' . esc_html($content) . '</p>';
    }

    if ($footerMeta !== '') {
        echo wp_kses($footerMeta, Helper::eael_allowed_tags());
    }

    if ($readMoreHtml !== '') {
        echo wp_kses($readMoreHtml, Helper::eael_allowed_tags());
    }

    echo '</div>';
}

if ($showTitle) {
    $titleHtml = '<div class="eael-timeline-post-title">'
        . ($badgeHtml !== '' ? $badgeHtml : '')
        . ($headerMeta !== '' ? $headerMeta : '')
        . '<' . $titleTag . ' class="eael-timeline-post-title-text">' . esc_html(get_the_title()) . '</' . $titleTag . '>'
        . (!$showExcerpt && $footerMeta !== '' ? $footerMeta : '')
        . (!$showExcerpt && $readMoreHtml !== '' ? $readMoreHtml : '')
        . '</div>';

    echo wp_kses($titleHtml, Helper::eael_allowed_tags());
} elseif ($headerMeta !== '' || $badgeHtml !== '' || (!$showExcerpt && $readMoreHtml !== '')) {
    echo wp_kses('<div class="eael-timeline-post-title">' . $badgeHtml . $headerMeta . (!$showExcerpt ? $readMoreHtml : '') . '</div>', Helper::eael_allowed_tags());
}

echo '</a>';
echo '</div>';
echo '</article>';
