# -*- coding: utf-8 -*-
"""Ejemplo 11: Dispositivos WASAPI y sus capacidades."""

import ctypes
from basskit.dlls.basswasapi import basswasapi
from basskit.estructuras.addons import BASS_WASAPI_DEVICEINFO
from basskit.constantes.addons import (
	BASS_WASAPI_TYPE_SPEAKERS, BASS_WASAPI_TYPE_HEADPHONES,
	BASS_WASAPI_TYPE_MICROPHONE, BASS_WASAPI_TYPE_HDMI,
	BASS_WASAPI_TYPE_SPDIF, BASS_WASAPI_TYPE_HEADSET,
)

TIPOS = {
	0: "Red", 1: "Altavoces", 2: "Linea", 3: "Auriculares",
	4: "Microfono", 5: "Headset", 6: "Handset", 7: "Digital",
	8: "SPDIF", 9: "HDMI", 10: "Desconocido",
}


def main():
	print("=== Dispositivos WASAPI ===\n")

	v = basswasapi.BASS_WASAPI_GetVersion()
	print(f"BASSWASAPI version: {(v>>24)&0xFF}.{(v>>16)&0xFF}.{(v>>8)&0xFF}.{v&0xFF}\n")

	info = BASS_WASAPI_DEVICEINFO()
	i = 0
	salida = []
	entrada = []

	while basswasapi.BASS_WASAPI_GetDeviceInfo(i, ctypes.byref(info)):
		nombre = info.name.decode("utf-8", errors="replace") if info.name else "?"
		tipo = TIPOS.get(info.type, "?")
		es_entrada = bool(info.flags & 16)
		es_loopback = bool(info.flags & 8)
		habilitado = bool(info.flags & 1)
		defecto = bool(info.flags & 2)

		entry = {
			"indice": i, "nombre": nombre, "tipo": tipo,
			"freq": info.mixfreq, "canales": info.mixchans,
			"habilitado": habilitado, "defecto": defecto,
			"loopback": es_loopback,
		}

		if es_entrada:
			entrada.append(entry)
		else:
			salida.append(entry)
		i += 1

	print(f"Dispositivos de SALIDA ({len(salida)}):")
	for d in salida:
		marca = " [DEFECTO]" if d["defecto"] else ""
		loop = " (loopback)" if d["loopback"] else ""
		hab = "Si" if d["habilitado"] else "No"
		print(f"  [{d['indice']}] {d['nombre']}{marca}{loop}")
		print(f"      Tipo: {d['tipo']} | Freq: {d['freq']} Hz | Ch: {d['canales']} | Hab: {hab}")

	print(f"\nDispositivos de ENTRADA ({len(entrada)}):")
	for d in entrada:
		marca = " [DEFECTO]" if d["defecto"] else ""
		hab = "Si" if d["habilitado"] else "No"
		print(f"  [{d['indice']}] {d['nombre']}{marca}")
		print(f"      Tipo: {d['tipo']} | Freq: {d['freq']} Hz | Ch: {d['canales']} | Hab: {hab}")

	print(f"\nTotal: {i} dispositivo(s)")


if __name__ == "__main__":
	main()
