#!/usr/bin/python3
import pulsectl
import subprocess
import os

# Valid sinks contain these strings
sink_whitelist = {
        # hdmi for hdmi audio, pnp for speakers
        "pnp": " Speakers",
        "logitech": " Wired",
        "steelseries": " Wireless",
        "bluez": " bluetooth" 
}

sink_list = []
sink_objects = []

# Create pulse object
with pulsectl.Pulse() as pulse:

    # Get current sink
    current_sink = pulse.server_info().default_sink_name
    print(current_sink)

    # Iterate through sinks
    for sink in pulse.sink_list():
        # Iterate through whitelist entries
        for key,value in sink_whitelist.items():
            # See if the whitelist entry is in the sink name
            if key.lower() in sink.name.lower():
                # Append the sink to the main sink list
                sink_list.append(sink.name)
                sink_objects.append(sink)
    
    print(sink_list)

    # Get index for current sink
    try:
        current_index = sink_list.index(current_sink)
    except:
        current_index = 0

    # Wrap index value
    current_index+=1
    if (current_index > len(sink_list)-1): 
        current_index = 0
    print("New index:", current_index)

    # Iterate through sinks again to get object
    for sink in pulse.sink_list():
        if sink.name == sink_list[current_index]:
            pulse.default_set(sink)
            for key,value in sink_whitelist.items():
                if key.lower() in sink.name.lower():
                    print(value)
                    replace_id = "--replace-file=" + os.path.expanduser("~/.cache/replace-id")
                    subprocess.run(['notify-send.sh', replace_id, value])


