import { Component, Input, Output, EventEmitter } from '@angular/core'
import { NgFor } from '@angular/common'

@Component({
  selector: 'app-stream-cards',
  standalone: true,
  imports: [NgFor],
  templateUrl: './stream-cards2.component.html',
  styleUrls: ['./stream-cards2.component.scss']
})
export class StreamCardsComponent {

  @Input() streams: string[] = []
  @Input() streamTitles: Record<string, string> = {}
  @Input() streamThumbnails: Record<string, string> = {}
  @Input() isLive!: (stream: string) => boolean
  @Input() startTime!: (stream: string) => string
  @Input() streamStartTimes: Record<string, string> = {}
@Input() nowTimestamp!: number

getCountdown(stream: string): string {
  const start = this.streamStartTimes[stream]
  if (!start) return ''

  const diff = new Date(start).getTime() - this.nowTimestamp

  if (diff <= 0) return 'Starting...'

  const totalSeconds = Math.floor(diff / 1000)
  const h = Math.floor(totalSeconds / 3600)
  const m = Math.floor((totalSeconds % 3600) / 60)
  const s = totalSeconds % 60

  const pad = (n: number) => n.toString().padStart(2, '0')

  return `Starts in ${pad(h)}:${pad(m)}:${pad(s)}`
}

  @Output() streamSelected = new EventEmitter<string>()

  select(stream: string) {
    this.streamSelected.emit(stream)
  }
}