<?php
/**
 * Single Artist Video (adaptado al layout nuevo)
 *
 * @package far_loop
 */

get_header();

// === Campos ===
$vimeo_id      = get_field('vimeo_id');
$trailer_id    = get_field('trailer_id');
$options       = get_field('video_options');                // array: p.ej. ['private','premiere']
$cover         = get_field('cover_image');                  // array ACF imagen
$authors       = get_field('author');                       // rel/array
$gallery       = get_field('gallery');                      // post obj / ID
$year          = get_field('year');
$duration      = get_field('duration');                     // repeater; en el layout nuevo lo dejaban sin render directo
$format_tech   = get_field('format_and_technical');
$credits       = get_field('credits');
$lead_desc     = get_field('lead_description');
$full_desc     = get_field('full_description');
$link          = get_field('extra_content_link'); $link = is_array($link) ? ( $link[0] ?? [] ) : [];
$stills        = get_field('stills');                       // galería de imágenes
?>

<div class="u_pt-5">
  <div class="l_container u_mv-5 wrapper-profile">

    <!-- Título + autores + (opcional) galería/ciudad -->
    <div class="page-title u_mb-3 u_mb-md-4">
      <h1 class="page-title__alpha-title"><?php the_title(); ?></h1>
      <h2 class="info-card__value"><?php echo get_authors_string( $authors ); ?></h2>

      <?php if ( !empty($gallery) ) :
        $gid   = is_object($gallery) ? (int)$gallery->ID : (int)$gallery;
        $gaddr = $gid ? get_field('address', $gid) : [];
        $gcity = !empty($gaddr[0]['city']) ? $gaddr[0]['city'] : '';
      ?>
        <h3 class="page-title__gamma-title birth-date">
          <?php echo esc_html( get_the_title($gid) ); ?><?php echo $gcity ? ', ' . esc_html($gcity) : ''; ?>
        </h3>
      <?php endif; ?>
    </div>

    <!-- Bloque vídeo / imagen (con control de privado) -->
    <div class="u_mb-5 wrapper-artist-video">
      <?php if ( is_array($options) && in_array('private', $options, true) && ! is_user_logged_in() ) : ?>

        <figure class="cover-image">
          <?php
            if ( ! empty($cover) && !empty($cover['url']) ) {
              $src     = $cover['url'];
              $caption = $cover['caption'] ?? '';
            } else {
              $src     = get_template_directory_uri() . '/img/horizontal-placeholder.svg';
              $caption = 'placeholder';
            }
          ?>
          <img src="<?php echo esc_url($src); ?>" alt="<?php echo esc_attr($caption ?: get_the_title()); ?>">
          <?php if ( $caption ) : ?>
            <figcaption class="cover-image__caption"><?php echo esc_html($caption); ?></figcaption>
          <?php endif; ?>
        </figure>

        <p class="u_mt-min1 u_mb-0 u_text-italic eta">&mdash;
          <a href="<?php echo esc_url( wp_login_url( get_permalink() ) ); ?>" class="u_bd-bottom-dotted">Log in</a>
          to watch the artist video if you have been given an access
        </p>

      <?php else : ?>

        <?php if ( ! empty($vimeo_id) ) : ?>

          <?php video_player_module( $vimeo_id ); ?>

        <?php else :
          // Fallback: primer still válido, si no, cover, si no, placeholder
          $first_still = null;
          if ( ! empty($stills) && is_array($stills) ) {
            foreach ( $stills as $s ) {
              if ( ! empty($s['url']) ) { $first_still = $s; break; }
            }
          }
        ?>
          <?php if ( $first_still ) : ?>
            <figure class="cover-image cover-image--still">
              <img src="<?php echo esc_url($first_still['url']); ?>"
                   alt="<?php echo esc_attr( ! empty($first_still['caption']) ? $first_still['caption'] : 'Still' ); ?>">
              <?php if ( ! empty($first_still['caption']) ) : ?>
                <figcaption class="cover-image__caption"><?php echo esc_html($first_still['caption']); ?></figcaption>
              <?php endif; ?>
            </figure>
          <?php elseif ( ! empty($cover) && !empty($cover['url']) ) : ?>
            <figure class="cover-image">
              <img src="<?php echo esc_url($cover['url']); ?>" alt="<?php echo esc_attr($cover['caption'] ?? ''); ?>">
              <?php if ( ! empty($cover['caption']) ) : ?>
                <figcaption class="cover-image__caption"><?php echo esc_html($cover['caption']); ?></figcaption>
              <?php endif; ?>
            </figure>
          <?php else : ?>
            <figure class="cover-image">
              <img src="<?php echo esc_url( get_template_directory_uri() . '/img/horizontal-placeholder.svg' ); ?>" alt="placeholder">
              <figcaption class="cover-image__caption">placeholder</figcaption>
            </figure>
          <?php endif; ?>

        <?php endif; ?>
      <?php endif; ?>
    </div>

    <!-- Cols: infocard + texto -->
    <div data-sticky-sidebar-grid class="grid u_mh-min1p5">
      <div data-sticky-sidebar class="col--sm--three--seventh col--md--two--fifth u_ph-1p5">

        <?php if ( is_array($options) && in_array('premiere', $options, true) ) : ?>
          <div class="premiere-mark">Première</div>
        <?php endif; ?>

        <?php get_template_part( 'template-parts/infocard-artist-video' ); ?>
      </div>

      <div class="col--sm--four--seventh col--md--three--fifth u_ph-1p5">

        <?php if ( ! empty( $lead_desc ) ) : ?>
          <p class="lead-description u_mb-2 h1"><?php echo $lead_desc; ?></p>
        <?php endif; ?>

        <div class="wysiwyg">
          <?php echo $full_desc; ?>
        </div>

        <?php award_list_module(); ?>

        <?php if ( ! empty( $trailer_id ) ) : ?>
          <h3 class="section-heading delta u_mt-5"><span class="u_text-underline">Trailer</span></h3>
          <?php video_player_module( $trailer_id ); ?>
        <?php endif; ?>

        <?php if ( ! empty( $link['url'] ) ) : ?>
          <div class="u_mt-4">
            <a href="<?php echo esc_url($link['url']); ?>" class="u_bd-bottom-dotted">
              <?php echo ! empty($link['link_text']) ? esc_html($link['link_text']) : esc_html( remove_http( $link['url'] ) ); ?>
            </a>
          </div>
        <?php endif; ?>

      </div>
    </div>

    
    <?php 
    $video_intro = get_field('video_introduction');

    if ( ! empty($video_intro) ) : ?>
      <h3 class="section-heading delta u_mt-5"><span class="u_text-underline">Introduction</span></h3>
      <div class="videocloop-introduction">
        <?php echo wp_kses_post($video_intro); ?>
      </div>
    <?php endif; ?>







  </div>

  <!-- Stills en archive-grid -->
  <div class="l_container l_container--wider u_mt-5">
    <?php if ( ! empty($stills) ) : ?>
      <h3 class="section-heading delta"><span class="u_text-underline">Stills</span></h3>

      <div class="archive-grid">
        <?php foreach ( $stills as $still ) :
          if ( empty($still['url']) ) continue; ?>
          <div class="archive-item">
            <div class="archive-new-thumb">
              <div class="archive-thumb__image-container still-image" data-pswp-image="data-pswp-image">
                <img src="<?php echo esc_url( $still['url'] ); ?>" alt="Still">
              </div>
            </div>
          </div>
        <?php endforeach; ?>
      </div>
    <?php endif; ?>
  </div>

  <?php
    related_artist_profile();
    related_artist_videos_module();
  ?>

  <!-- PhotoSwipe (igual que en el layout nuevo) -->
  <div class="pswp">
    <div class="pswp__bg"></div>
    <div class="pswp__scroll-wrap">
      <div class="pswp__container">
        <div class="pswp__item"></div>
        <div class="pswp__item"></div>
        <div class="pswp__item"></div>
      </div>
      <div class="pswp__ui pswp__ui--hidden">
        <div class="pswp__top-bar">
          <div class="pswp__counter"></div>
          <div class="pswp__button pswp__button--close"></div>
          <div class="pswp__button pswp__button--fs"></div>
          <div class="pswp__button pswp__button--zoom"></div>
          <div class="pswp__preloader">
            <div class="pswp__preloader__icn">
              <div class="pswp__preloader__cut">
                <div class="pswp__preloader__donut"></div>
              </div>
            </div>
          </div>
        </div>
        <button class="pswp__button pswp__button--arrow--left"></button>
        <button class="pswp__button pswp__button--arrow--right"></button>
        <div class="pswp__caption">
          <div class="pswp__caption__center"></div>
        </div>
      </div>
    </div>
  </div>

</div>

<?php get_footer();