import sys
import threading
from threading import Thread
import keyboard
import time
import menu_key_def as KEYDEF

class class_menu_key_thread(Thread):  
    def __init__(self, parent = None) : 
        Thread.__init__(self)
        self.key_left = False
        self.key_right = False
        self.key_up = False
        self.key_down = False
        self.key_enter = False
        self.key_reset = False
        self.stop_request = False
        
        self.key_left_count = 0
        self.key_right_count = 0      
        self.key_up_count = 0
        self.key_down_count = 0    
        self.key_enter_count = 0
        self.key_reset_count = 0
        self.key_event = threading.Event()

    def menu_wait_key(self, timeout) :
        self.key_event.wait(timeout)
        self.key_event.clear()
        
        if self.key_enter :
            self.key_enter = False
            return KEYDEF.MENU_PACK_KEY_ENTER_OR_RIGHT
        if self.key_reset :
            self.key_reset = False
            return KEYDEF.MENU_PACK_KEY_ESCAPE
        if self.key_up :
            self.key_up = False
            return KEYDEF.MENU_PACK_KEY_UP
        if self.key_down :
            self.key_down = False
            return KEYDEF.MENU_PACK_KEY_DOWN     
        if self.key_left :
            self.key_left = False
            return KEYDEF.MENU_PACK_KEY_LEFT
        if self.key_right :
            self.key_right = False
            return KEYDEF.MENU_PACK_KEY_RIGHT

        return KEYDEF.MENU_PACK_KEY_NULL
    
    def thread_stop(self) :
        self.stop_request = True
        return 
    
    def key_up_press(self) :
        self.key_up_count += 1
        self.key_up = True
        self.key_event.set()
    
    def key_down_press(self) :
        self.key_down_count += 1
        self.key_down = True
        self.key_event.set()
        
    def key_enter_press(self) :
        self.key_enter_count += 1
        self.key_enter = True
        self.key_event.set()
        
    def key_reset_press(self) :
        self.key_reset_count += 1
        self.key_reset = True
        self.key_event.set()
        
    def key_right_press(self) :
        self.key_right_count += 1
        self.key_right = True
        self.key_event.set()

    def key_left_press(self) :
        self.key_left_count += 1
        self.key_left = True
        self.key_event.set()
             
    def run(self) :
        keyboard.add_hotkey('ctrl + right', self.key_left_press, args=(), suppress=False)
        keyboard.add_hotkey('ctrl + left', self.key_right_press, args=(), suppress=False)
        keyboard.add_hotkey('ctrl + up', self.key_up_press, args=(), suppress=False)
        keyboard.add_hotkey('ctrl + down', self.key_down_press, args=(), suppress=False)
        keyboard.add_hotkey('ctrl + enter', self.key_enter_press, args=(), suppress=False)
        keyboard.add_hotkey('ctrl + q', self.key_reset_press, args=(), suppress=False)

        while self.stop_request == False:
            time.sleep(0.5)
