<template>
    <div v-if="tts.state.value !== 'idle'" class="tts-player-bar">
        <button class="tts-nav-btn" @click="tts.prev()" title="Previous">
            <svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor"><path d="M15.707 15.707a1 1 0 01-1.414 0l-5-5a1 1 0 010-1.414l5-5a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 010 1.414zm-6 0a1 1 0 01-1.414 0l-5-5a1 1 0 010-1.414l5-5a1 1 0 011.414 1.414L5.414 10l4.293 4.293a1 1 0 010 1.414z"/></svg>
        </button>

        <button class="tts-play-btn" @click="togglePlay" :title="tts.state.value === 'playing' ? 'Pause' : 'Play'">
            <span v-if="tts.state.value === 'loading'" class="tts-bar-spinner"></span>
            <svg v-else-if="tts.state.value === 'playing'" class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M5.75 3a.75.75 0 00-.75.75v12.5a.75.75 0 001.5 0V3.75A.75.75 0 005.75 3zm8.5 0a.75.75 0 00-.75.75v12.5a.75.75 0 001.5 0V3.75a.75.75 0 00-.75-.75z" clip-rule="evenodd"/></svg>
            <svg v-else class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor"><path d="M6.3 2.841A1.5 1.5 0 004 4.11V15.89a1.5 1.5 0 002.3 1.269l9.344-5.89a1.5 1.5 0 000-2.538L6.3 2.84z"/></svg>
        </button>

        <button class="tts-nav-btn" @click="tts.next()" title="Next">
            <svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor"><path d="M4.293 15.707a1 1 0 010-1.414L8.586 10 4.293 5.707a1 1 0 011.414-1.414l5 5a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0zm6 0a1 1 0 010-1.414L14.586 10l-4.293-4.293a1 1 0 011.414-1.414l5 5a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0z"/></svg>
        </button>

        <div class="tts-progress" @click="onSeek">
            <div class="tts-progress-fill" :style="{ width: (tts.progress.value * 100) + '%' }"></div>
        </div>

        <span class="tts-time">{{ formatTime(tts.currentTime.value) }} / {{ formatTime(tts.duration.value) }}</span>

        <span class="tts-snippet">{{ tts.currentTrack.value?.snippet || '' }}</span>

        <button class="tts-close-btn" @click="tts.stop()" title="Close">
            <svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor"><path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"/></svg>
        </button>
    </div>
</template>

<script setup lang="ts">
import { useTtsPlayer } from '../composables/useTtsPlayer';

const tts = useTtsPlayer();

function togglePlay() {
    if (tts.state.value === 'playing') tts.pause();
    else if (tts.state.value === 'paused') tts.resume();
}

function onSeek(e: MouseEvent) {
    const el = e.currentTarget as HTMLElement;
    const fraction = e.offsetX / el.clientWidth;
    tts.seek(Math.max(0, Math.min(1, fraction)));
}

function formatTime(secs: number): string {
    if (!secs || !isFinite(secs)) return '0:00';
    const m = Math.floor(secs / 60);
    const s = Math.floor(secs % 60);
    return `${m}:${s.toString().padStart(2, '0')}`;
}
</script>

<style scoped>
.tts-player-bar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 15;
    height: 40px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 12px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    animation: slideDown 0.2s ease-out;
}
@keyframes slideDown { from { transform: translateY(-100%); } to { transform: translateY(0); } }

.tts-nav-btn, .tts-play-btn, .tts-close-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    padding: 4px;
    display: flex;
    align-items: center;
    transition: color 0.15s;
}
.tts-nav-btn:hover, .tts-play-btn:hover, .tts-close-btn:hover { color: var(--text); }
.tts-play-btn { color: var(--accent); }
.tts-play-btn:hover { color: var(--text); }

.tts-progress {
    flex: 1;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
    cursor: pointer;
    position: relative;
    min-width: 60px;
}
.tts-progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 2px;
    transition: width 0.1s linear;
}

.tts-time {
    font-size: 0.72rem;
    color: var(--text-dim);
    white-space: nowrap;
    min-width: 60px;
}

.tts-snippet {
    font-size: 0.72rem;
    color: var(--text-dim);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 200px;
}

.tts-bar-spinner {
    width: 14px;
    height: 14px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.w-4 { width: 16px; height: 16px; }

@media (max-width: 639px) {
    .tts-snippet { display: none; }
    .tts-time { min-width: 50px; font-size: 0.68rem; }
}
</style>
