#!/usr/bin/python3

import sys
import os
import signal
import subprocess
import time
import tty,termios
import re
import json
from pathlib import Path
import runner_lib as runner


bindings={
    "Show playlist"     : "l",
    "Launch [0-x] cmd"  : "0-x",
    "Previous command"  : "p",
    "Next command"      : "o",
    "New command"       : "a",
    "Edit command"      : "e",
    "Delete command"    : "d",
    "Load playlist"     : "L",
    "Save playlist"     : "S",
    "Save as new"      : "A",
    "New playlist"      : "N",
    "Command help"      : "H",
    "Kill process Id"   : "K",
    "Edit Laser Id"     : "i",
    "Edit Laser Scene"  : "s",
    "Information"       : "I",
    "Help"              : "h",
    "Quit"              : "q",

}


## Init user contact



# Main Loop
runner.action_info()
runner.action_help()
print("\n\nLoad a playlist? [Y/n]: ")
if "y" == runner.inkey() :
  runner.action_loadPlaylist()

while True:
    # Fuck zombies
    runner._killBill()
    runner._ok("> Next Action?")
    k = runner.inkey()

    if bindings["Next command"] == k:
        runner.action_changeCommand( 1 )
        runner.action_runCommand()
    elif bindings["Previous command"] == k:
        runner.action_changeCommand( -1 )
        runner.action_runCommand()
    elif re.match( r'^\d+$',k):
        runner.action_match(k)
        runner.action_runCommand()
    elif bindings["New command"] == k:
        runner.action_newCommand()
        continue
    elif bindings["Show playlist"] == k:
        runner.action_listAll()
        continue
    elif bindings["Delete command"] == k:
        runner.action_deleteCommand()
        continue
    elif bindings["Edit command"] == k:
      runner.action_listAll()
      runner.action_edit()
      continue
    elif bindings["Load playlist"] == k:
      if runner.action_loadPlaylist():
        runner.action_listAll()
      continue
    elif bindings["Save playlist"] == k:
      runner.action_savePlaylist()
      continue
    elif bindings["Save as new"] == k:
      runner.action_savePlaylist()
      continue
    elif bindings["New playlist"] == k:
      runner.action_newPlaylist()
      continue
    elif bindings["Command help"] == k:
      runner.action_commandHelp()
      continue
    elif bindings["Edit Laser Id"] == k:
      runner.action_laserId()
      continue
    elif bindings["Edit Laser Scene"] == k:
      runner.action_laserScene()
      continue
    elif bindings["Kill process Id"] == k:
      runner.action_killPid()
      continue
    elif bindings["Help"] == k:
        runner.action_help()
        continue
    elif bindings["Information"] == k:
        runner.action_info()
        continue
    elif bindings["Quit"] == k:
        runner.action_quit()
    else:
        runner._err("Unexpected key:{}".format(k))
        continue




