<template>
  <div class="w-80 bg-white border-r border-gray-200 h-full overflow-hidden flex flex-col">
    <!-- Header -->
    <div class="p-4 border-b border-gray-200">
      <h2 class="text-lg font-semibold text-gray-900">Your Tracks</h2>
      <p class="text-sm text-gray-500">{{ tracks.length }} total tracks</p>
    </div>
    
    <!-- Track List -->
    <div class="flex-1 overflow-y-auto">
      <div v-if="tracks.length === 0" class="p-4 text-center text-gray-500">
        No tracks uploaded yet
      </div>
      
      <div v-else class="space-y-1 p-2">
        <div
          v-for="(track, index) in tracks"
          :key="index"
          @click="selectTrack(index)"
          :class="[
            'p-3 rounded-lg cursor-pointer transition-colors border-l-4',
            selectedTrackIndex === index 
              ? 'bg-blue-50 border-blue-500' 
              : 'bg-gray-50 hover:bg-gray-100 border-gray-300'
          ]"
        >
          <!-- Track Header -->
          <div class="flex items-center justify-between mb-2">
            <div class="flex items-center">
              <div 
                class="w-4 h-4 rounded-full mr-2"
                :style="{ backgroundColor: getTrackColor(index) }"
              ></div>
              <div class="font-medium text-gray-900 truncate">
                {{ formatTrackName(track.name) }}
              </div>
            </div>
          </div>
          
          <!-- Track Stats -->
          <div class="grid grid-cols-2 gap-2 text-xs text-gray-600">
            <div>
              <span class="text-gray-500">Distance:</span>
              <span class="font-medium ml-1">{{ formatDistance(track.stats?.distance || 0) }}</span>
            </div>
            <div>
              <span class="text-gray-500">Duration:</span>
              <span class="font-medium ml-1">{{ formatDuration(track.stats?.duration || 0) }}</span>
            </div>
            <div>
              <span class="text-gray-500">Speed:</span>
              <span class="font-medium ml-1">{{ track.stats?.avgSpeed || 0 }} km/h</span>
            </div>
            <div>
              <span class="text-gray-500">Elevation:</span>
              <span class="font-medium ml-1">{{ track.stats?.elevationGain || 0 }}m</span>
            </div>
          </div>
          
          <!-- Date -->
          <div class="mt-2 text-xs text-gray-500">
            {{ formatDate(track.stats?.startTime) }}
          </div>
        </div>
      </div>
    </div>
    
    <!-- Sticky Upload Button at Bottom -->
    <div class="p-4 border-t border-gray-200 bg-white">
      <FileUpload @files-uploaded="$emit('files-uploaded', $event)" />
    </div>
  </div>
</template>

<script>
import FileUpload from './FileUpload.vue'

export default {
  name: 'TrackList',
  components: {
    FileUpload
  },
  props: {
    tracks: {
      type: Array,
      default: () => []
    },
    selectedTrackIndex: {
      type: Number,
      default: null
    }
  },
  emits: ['track-selected', 'files-uploaded'],
  methods: {
    selectTrack(index) {
      this.$emit('track-selected', index)
    },
    
    getTrackColor(index) {
      const colors = [
        '#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7',
        '#DDA0DD', '#98D8C8', '#F7DC6F', '#BB8FCE', '#85C1E9'
      ]
      return colors[index % colors.length]
    },
    
    formatTrackName(name) {
      if (!name) return 'Unnamed Track'
      // If name is a date format, try to make it more readable
      if (name.includes(' at ')) {
        return name
      }
      // Truncate very long names
      return name.length > 25 ? name.substring(0, 25) + '...' : name
    },
    
    formatDistance(distance) {
      const km = distance / 1000
      return km < 10 ? km.toFixed(2) + ' km' : km.toFixed(1) + ' km'
    },
    
    formatDuration(duration) {
      if (!duration) return '0m'
      const hours = Math.floor(duration / 3600)
      const minutes = Math.floor((duration % 3600) / 60)
      
      if (hours > 0) {
        return `${hours}h ${minutes}m`
      }
      return `${minutes}m`
    },
    
    formatDate(dateString) {
      if (!dateString) return 'Unknown date'
      const date = new Date(dateString)
      return date.toLocaleDateString('en-US', {
        month: 'short',
        day: 'numeric',
        year: 'numeric'
      })
    }
  }
}
</script>