# -*- coding: utf-8 -*-
"""Ejemplo 10: Reproduccion de stream desde URL."""

import sys
import time
from basskit.dlls.bass import bass
from basskit.constantes.bass import BASS_STREAM_STATUS, BASS_ACTIVE_PLAYING, BASS_ACTIVE_STALLED


def main():
	print("=== Streaming desde URL ===\n")

	url = b"http://stream.radioparadise.com/aac-320"
	if len(sys.argv) > 1:
		url = sys.argv[1].encode("utf-8")

	bass.BASS_Init(-1, 44100, 0, 0, None)
	print(f"Conectando a: {url.decode()}")

	stream = bass.BASS_StreamCreateURL(url, 0, BASS_STREAM_STATUS, None, None)
	if stream == 0:
		print(f"Error: {bass.BASS_ErrorGetCode()}")
		bass.BASS_Free()
		return

	bass.BASS_ChannelPlay(stream, 0)
	print("Reproduciendo stream...\n")
	print("Presiona Ctrl+C para detener.\n")

	try:
		while True:
			estado = bass.BASS_ChannelIsActive(stream)
			if estado == BASS_ACTIVE_PLAYING:
				estado_str = "Reproduciendo"
			elif estado == BASS_ACTIVE_STALLED:
				estado_str = "Buffering..."
			else:
				break
			print(f"\r  Estado: {estado_str}  ", end="", flush=True)
			time.sleep(1)
	except KeyboardInterrupt:
		print("\n\nDetenido por el usuario.")

	bass.BASS_StreamFree(stream)
	bass.BASS_Free()
	print("Fin del ejemplo.")


if __name__ == "__main__":
	main()
