<?php
if (!defined('ABSPATH')) exit;

$player_tracks = isset($player_tracks) && is_array($player_tracks) ? array_values($player_tracks) : [];
$player_variant = isset($player_variant) ? sanitize_key((string) $player_variant) : 'full';
$player_variant = in_array($player_variant, ['compact', 'full'], true) ? $player_variant : 'full';
$player_title = isset($player_title) ? (string) $player_title : '';
$player_artist = isset($player_artist) ? (string) $player_artist : '';
$player_cover_url = isset($player_cover_url) ? (string) $player_cover_url : '';
$player_show_playlist = isset($player_show_playlist) ? (bool) $player_show_playlist : ($player_variant === 'full');
$player_empty_message = isset($player_empty_message) ? (string) $player_empty_message : __('No playable audio has been attached yet.', 'blr-backline');

$placeholder_cover_url = BLR_BA_URL . 'assets/blr-ba/img/placeholder-image.png';
$player_has_cover = ($player_cover_url !== '');
$player_cover_src = $player_has_cover ? $player_cover_url : $placeholder_cover_url;

$track_count = count($player_tracks);
$playable_count = 0;
foreach ($player_tracks as $player_track) {
    if (!empty($player_track['audio_url'])) {
        $playable_count++;
    }
}

$player_uid = function_exists('wp_unique_id') ? wp_unique_id('blr-ba-player-') : ('blr-ba-player-' . wp_rand(1000, 999999));
$player_tracks_json = wp_json_encode($player_tracks, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT);
if (!is_string($player_tracks_json) || $player_tracks_json === '') {
    $player_tracks_json = '[]';
}
?>
<div class="blr-ba-audio-player blr-ba-audio-player--<?php echo esc_attr($player_variant); ?>" data-player-id="<?php echo esc_attr($player_uid); ?>" data-player-tracks="<?php echo esc_attr($player_tracks_json); ?>">

  <audio class="blr-ba-player-audio" preload="none"></audio>

  <div class="blr-ba-player-shell">
    <div class="blr-ba-player-cover-wrap<?php echo esc_attr(!$player_has_cover ? ' is-placeholder' : ''); ?>">
      <img class="blr-ba-player-cover" src="<?php echo esc_url($player_cover_src); ?>" alt="">
    </div>

    <div class="blr-ba-player-main">
      <div class="blr-ba-player-heading">
        <div class="blr-ba-player-title"><?php echo esc_html($player_title !== '' ? $player_title : __('Untitled', 'blr-backline')); ?></div>
        <div class="blr-ba-player-subtitle"><?php echo esc_html($player_artist !== '' ? $player_artist : __('—', 'blr-backline')); ?></div>
      </div>

      <div class="blr-ba-player-now-playing">
        <div class="blr-ba-player-now-title"><?php echo esc_html__('—', 'blr-backline'); ?></div>
        <div class="blr-ba-player-now-meta"><?php echo esc_html__('Select a track to begin.', 'blr-backline'); ?></div>
      </div>

      <div class="blr-ba-player-controls">
        <button type="button" class="btn btn-outline-secondary btn-sm blr-ba-player-prev" aria-label="<?php echo esc_attr__('Previous track', 'blr-backline'); ?>"><?php echo esc_html__('Prev', 'blr-backline'); ?></button>
        <button type="button" class="btn btn-primary blr-ba-player-toggle" aria-label="<?php echo esc_attr__('Play or pause', 'blr-backline'); ?>"><?php echo esc_html__('Play', 'blr-backline'); ?></button>
        <button type="button" class="btn btn-outline-secondary btn-sm blr-ba-player-next" aria-label="<?php echo esc_attr__('Next track', 'blr-backline'); ?>"><?php echo esc_html__('Next', 'blr-backline'); ?></button>
      </div>

      <div class="blr-ba-player-progress-row">
        <span class="blr-ba-player-time blr-ba-player-time-current">0:00</span>
        <input type="range" class="blr-ba-player-progress" min="0" max="1000" step="1" value="0" aria-label="<?php echo esc_attr__('Seek playback position', 'blr-backline'); ?>">
        <span class="blr-ba-player-time blr-ba-player-time-duration">0:00</span>
      </div>

      <div class="p-2">
      </div>

    </div>
  </div>

  <?php if ($player_show_playlist && $track_count > 0): ?>
    <div class="blr-ba-player-playlist list-group mt-3" role="list" aria-label="<?php echo esc_attr__('Playlist', 'blr-backline'); ?>">
      <?php foreach ($player_tracks as $index => $track): ?>
        <?php
        $track_title = trim((string) ($track['display_title'] ?? $track['title'] ?? __('Untitled Track', 'blr-backline')));
        $track_artist = (string) ($track['artist'] ?? '');
        $track_duration = (string) ($track['duration'] ?? '');
        $track_number = (string) ($track['track_number'] ?? '');
        $track_audio = (string) ($track['audio_url'] ?? '');
        ?>
        <button
          type="button"
          class="blr-ba-player-track list-group-item list-group-item-action"
          data-index="<?php echo esc_attr((string) $index); ?>"
          data-has-audio="<?php echo esc_attr($track_audio !== '' ? '1' : '0'); ?>"
          role="listitem"
        >

          <span class="blr-ba-player-track-main">
            <span class="blr-ba-player-track-title"><?php echo esc_html($track_title); ?></span>
            <span class="blr-ba-player-track-subtitle"><?php echo esc_html($track_artist !== '' ? $track_artist : ($track_audio !== '' ? __('Ready to play', 'blr-backline') : __('No audio attached', 'blr-backline'))); ?></span>
          </span>
          <span class="blr-ba-player-track-side">
            <?php if ($track_duration !== ''): ?><span class="blr-ba-player-track-duration"><?php echo esc_html($track_duration); ?></span><?php endif; ?>
          </span>
        </button>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</div>