#!/usr/bin/env python
import sys

def eprint(*args, **kwargs):
    # uncomment to enable debug...
    # print(*args, file=sys.stderr, **kwargs)
    pass

def bash_it(cmd):
    eprint("bash_it", cmd)
    result = subprocess.run(["bash", "-c", cmd], capture_output=True)
    result = result.stdout.decode("utf-8")
    return result

def read_clipboard():
    # Read the clipboard on linux
    return subprocess.check_output(["xclip", "-o", "-sel", "clipboard"], universal_newlines=True)
    # Read the clipboard on macOS
    # return subprocess.check_output(["pbpaste"], universal_newlines=True)

    # Read the clipboard on Windows
    # return subprocess.check_output(["powershell", "-command", "Get-Clipboard"], universal_newlines=True)


def write_to_clipboard(text):
    # Write the text to the clipboard on Linux
    subprocess.run(["xclip", "-selection", "clipboard"], input = text.strip().encode('utf-8'), check=True)
    # bash_it("echo " + text +  "|" + "xclip -sel c")

    # Write the text to the clipboard on macOS
    # subprocess.run(["echo", "-n", text, "|", "pbcopy"], shell=True)

    # Write the text to the clipboard on Windows
    # subprocess.run(["powershell", "-command", "Set-Clipboard", text], shell=True)



def send_notif(title, message):
    eprint(title, message)
    subprocess.Popen(['notify-send', title, message])


import subprocess

commands = ['isbn to bibtex',
     'emoji',
     'calc',
     'spellcheck',
     'nvidia rofi']


rofi_process = subprocess.Popen(['rofi', '-dmenu'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
selected_command, _ = rofi_process.communicate('\n'.join(commands))
selected_command = selected_command.strip()

if selected_command:
    send_notif("Command Execution", f'Executing command: {selected_command}')

    if selected_command == 'isbn to bibtex':
        clipboard = read_clipboard()
        isbn = bash_it(f"echo \"{clipboard}\n'Esc to Cancel'\" | rofi -dmenu")
        exit(0) if not isbn else False
        isbn = isbn.replace("\n", " ")
        eprint("isbn:", isbn)
        res  = bash_it('echo ' + isbn + '| isbn_to_bibtex.py')
        eprint(res)
        write_to_clipboard(res)
        send_notif("status", "done!: "+ res)
    elif selected_command == 'emoji':
        bash_it("rofi -modi \"emoji:rofimoji\" -show emoji")
    elif selected_command == 'calc':
        bash_it("rofi -show calc -modi calc -no-show-match -no-sort")
    elif selected_command == 'spellcheck':
        bash_it("rofi -show spell -modes \"spell:rofi-spellcheck.sh\"")
    elif selected_command == 'nvidia rofi':
        bash_it("nvidia-offload ~/.config/polybar/forest/scripts/launcher.sh")
    else:
        send_notif("Failure", "Unkown Command")
        exit(1)
else:
    eprint('No command selected')


exit(0)



