#!/usr/bin/env python3

import subprocess
import os


d = subprocess.check_output(f'rofi -sep "|" -dmenu -i -p "MPD Menu" -font "mononoki Nerd Font 11" -location 2 -columns 1 -yoffset 40 -width 15 -hide-scrollbar -line-padding 4 -padding 20 -lines 4 <<< "Playlist Selection|Song Selection|Options|Cancel"', shell=True)
d = d.decode("utf-8").strip()
if d=="Cancel":
    pass
elif d=="Options":
    init = subprocess.check_output("mpc")
    init = init.decode("utf-8")

    rep = init.split("repeat: ")[1].split(" ")[0]
    single = init.split("single: ")[1].split(" ")[0]
    rand = init.split("random: ")[1].split(" ")[0]
    d = subprocess.check_output(f'rofi -sep "|" -dmenu -i -p "MPD Menu" -font "mononoki Nerd Font 11" -location 2 -columns 1 -yoffset 40 -width 15 -hide-scrollbar -line-padding 4 -padding 20 -lines 4 <<< "Toggle Repeat ({rep.capitalize()})|Toggle Single ({single.capitalize()})|Toggle Random ({rand.capitalize()})|Cancel"', shell=True)

    d = d.decode("utf-8").strip()
    print(d)

    if d=="Cancel":
        pass
    elif d==f"Toggle Repeat ({rep.capitalize()})" :
        os.system("mpc repeat")
    elif d== f"Toggle Single ({single.capitalize()})":
        os.system("mpc single")
    elif d==f"Toggle Random ({single.capitalize()})":
        os.system("mpc random")
elif d=="Playlist Selection":
    inp = subprocess.check_output('mpc lsplaylists', shell=True).decode("utf-8").strip().split("\n")
    ins = ""
    for i  in inp:
        ins += i + "|"
    d = subprocess.check_output(f'rofi -sep "|" -dmenu -i -p "MPD Menu" -font "mononoki Nerd Font 11" -location 2 -columns 1 -yoffset 40 -width 15 -hide-scrollbar -line-padding 4 -padding 20 -lines 4 <<< "{ins}Cancel"', shell=True).decode("utf-8").strip()
    if d=="Cancel":
        pass
    else:
        os.system("mpc clear")
        os.system(f"mpc load \"{d}\"")
        os.system("mpc play")
elif d=="Song Selection":
    inp = subprocess.check_output('mpc playlist', shell=True).decode("utf-8").strip().split("\n")
    ins = ""
    for i  in inp:
        ins += i + "|"
    d = subprocess.check_output(f'rofi -sep "|" -dmenu -i -p "MPD Menu" -font "mononoki Nerd Font 11" -location 2 -columns 1 -yoffset 40 -width 15 -hide-scrollbar -line-padding 4 -padding 20 -lines 4 <<< "{ins}Cancel"', shell=True).decode("utf-8").strip()
    if d=="Cancel":
        pass
    else:
       os.system(f"mpc play {inp.index(d)+1}")
