from cuesdk import CueSdk
import time
import subprocess
global sdk
sdk = CueSdk()
sdk.connect()

global activity_flag
global control_flag
global keyboard_index
print(sdk.protocol_details)
keyboard_index = 0
print(sdk.get_devices())
print(CueSdk().get_device_count())
def get_available_leds():
    """
    Output: list of leds available to control
    Taken from a code sample of Cuesdk
    """
    leds = list()
    device_count = sdk.get_device_count()
    for device_index in range(device_count):
        led_positions = sdk.get_led_positions_by_device_index(device_index)
        leds.append(led_positions)
    return leds

def turn_off_leds(all_leds):
    """
    Input: list of leds
    Applies zero RGB values to all the leds available so the keyboard lights will shut off
    """
    
    print("turn off")
    cmnd = "powershell (New-Object -ComObject WScript.Shell).SendKeys('{NUMLOCK}')"
    status, output = subprocess.getstatusoutput(cmnd)
    print(status)
    print(output)
    for led in all_leds[keyboard_index]:
        print(led)
        all_leds[keyboard_index][led] = (0, 0, 0)
    sdk.set_led_colors_buffer_by_device_index(keyboard_index, all_leds[keyboard_index])
    sdk.set_led_colors_flush_buffer()
    print("sleep1")
    time.sleep(2)
    

    while True:
        time.sleep(4)

for i in sdk.get_devices():
    print(str(i.type))
    if str(i.type) == 'CorsairDeviceType.Keyboard':
        break
    else:
        keyboard_index += 1
print(keyboard_index)
colors = get_available_leds()
print(str(sdk.get_devices()[keyboard_index].type))
print("\nModel: " + str(sdk.get_devices()[keyboard_index]) + "\n")
print("keyboard_index :" + str(keyboard_index))
print("keyboard lights will shutdown after: " + str(60 / 60) + " minutes")
print("Checking idle")

print(colors)
turn_off_leds(colors)