<?php

/**
 * Hero Block Block Template.
 *
 * @param   array $block The block settings and attributes.
 * @param   string $content The block inner HTML (empty).
 * @param   bool $is_preview True during AJAX preview.
 * @param   (int|string) $post_id The post ID this block is saved to.
 */

// Create id attribute allowing for custom "anchor" value.
$id = 'hero-block-' . $block['id'];
if( !empty($block['anchor']) ) {
    $id = $block['anchor'];
}

// Create class attribute allowing for custom "className" and "align" values.
$className = 'hero-block';
if( !empty($block['className']) ) {
    $className .= ' ' . $block['className'];
}

$media_type = get_field( 'choose_media_type', $block['id'] );
$choose_image = get_field( 'choose_image', $block['id'] );
$choose_video_option = get_field( 'choose_video_option', $block['id'] );
$video_url = get_field( 'video_url', $block['id'] );
$choose_video = get_field( 'choose_video', $block['id'] );
$running_text_group = get_field( 'running_text_group', $block['id'] );
$enter_caption = get_field( 'enter_caption', $block['id'] );
$link_option = get_field( 'link_option', $block['id'] );
$custom_link = get_field( 'custom_link', $block['id'] );
$link_to_a_post = get_field( 'link_to_a_post', $block['id'] );

?>

<section id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">

    <?php if ( get_field( 'has_text_above', $block['id'] )[0] == 'yes' ) : ?>
        <div class="texts-above-hero <?php echo is_page( 'muzejs' ) ? 'full-width' : ''  ?>">
            <?php 

            $columns = get_field( 'create_column', $block['id'] );

            if ( is_array( $columns ) && count( $columns ) > 0 ) {

                foreach ( $columns as $key => $item ) {
                    echo '<div class="text-above-hero">' . $item['enter_text'];

                        if ( $item['has_cta'] ) {

                            $link_text = $item['cta_label'];
                            $url = $item['cta_link'];
                            $select_a_field = $item['cta_link_to_a_post'];

                            if ( $item['link_option'] ==  'link_to_post' ) {
                                $url = get_permalink( $select_a_field->ID );
                            }
                            echo '<a href="' . $url . '" class="cta">' . $link_text . '</a>';

                        }

                    echo '</div>';
                }

            }
            
            ?>
        </div>
        <?php 
            if ( get_field( 'show_date', $block['id'] )[0] == 'yes' ) {
                if ( is_post_type_archive( 'events' ) || is_singular( 'events' ) ) {
                    echo '<p class="date">' . get_the_date( 'd.F.Y' ) . '</p>';
                } else if ( ! is_post_type_archive( 'museum' ) ) {
                    echo '<p class="date">' . get_the_date( 'Y.m.d' ) . '</p>';
                }
            }
        ?>
    <?php endif; ?>

    <div class="hero-block__wrapper">

        <?php 
            
            if ( get_field( 'has_running_text', $block['id'] )[0] == 'yes' ) {
                $bg_color = get_field( 'running_text_bg_color', $block['id'] );
                $location = get_field( 'choose_location', $block['id'] );
                $direction = get_field( 'choose_direction', $block['id'] );
            }

            $running_texts = get_field( 'running_texts', $block['id'] );

            if ( is_array( $running_texts ) && count( $running_texts ) ) {
                $running = '<div class="marquee-wrapper" data-bg-color="' . $bg_color . '" data-direction="' . $direction . '"><div class="marquee">';

                foreach ( $running_texts as $key => $item ) {
                    $custom_link = $item['custom_url'];
                    if ( $item['link_option'] == 'existing' ) {
                        $custom_link = get_permalink( $item['from_existing']->ID );
                    }
                    if ( $item['has_link'][0] == 'yes' ) {
                        $running .= '<div><a href="' . $custom_link . '">' . $item['enter_running_text'] . '</a></div>';
                    } else {
                        $running .= '<div>' . $item['enter_running_text'] . '</div>';
                    }
                    
                }
                $running .= '</div></div>';
            }

            if ( $location == 'top' ) {
                echo $running;
            }
        ?>

        <div class="media">
            <?php 

                if ( $media_type == 'image') {
                    echo '<img 
                        src="' . $choose_image['sizes']['full-width-img'] . '" 
                        class="img-fluid" 
                        style="background-image: url(' . $choose_image['sizes']['thumbnail'] . ');"
                        alt="">
                        <div class="image-wrapper"></div>';
                } else {
                    if ( $choose_video_option == 'url') {
                        echo '<div class="video video__external" data-youtube-id="'. $video_url .'" data-show-controls="' . ( get_field( 'show_controls', $block['id'] )[0] == 'yes' ? 'yes' : '' ) . '">
                        </div>';
                    } else {
                        echo '<video ' . ( get_field( 'show_controls', $block['id'] )[0] == 'yes' ? 'controls' : '' ) . ' autoplay muted loop id="myVideo">
                                <source src="' . $choose_video['url'] . '" type="video/mp4">
                            </video>';
                    }
                }
            
            ?>

        </div>
        <?php 
            if ( $location == 'bottom' ) {
                echo $running;
            }
        ?>

        
        <?php 
            if ( is_post_type_archive( 'museum' ) ) {
                echo '<div class="media-caption">';
                    echo '<p class="date">' . get_the_date( 'd.m.Y' ) . '</p>';
                echo '</div>';
            } 
        ?>
        <?php 
            if ( get_field( 'has_caption', $block['id'] )[0] == 'yes' ) {
                echo '<div class="media-caption">';
                    echo '<p class="caption"><span>' . $enter_caption . '</span></p>';
                echo '</div>';
            }
        ?>
            
    </div>

</section>

<?php printf( "<script>window.dispatchEvent( new Event('acf/loaded/hero-block'));</script>" );