"""
Class for grabbing joystick signals
"""
from controls.controlsbase import ControlsBase
import pygame
import pyjoystick
from pyjoystick.pygame import Key, Joystick, run_event_loop

class JoystickControls(ControlsBase):
    def __init__(self):
        mngr = pyjoystick.ThreadEventManager(event_loop=run_event_loop,
                                     handle_key_event=self.handle_key_event)
        mngr.start()
    
    def handle_key_event(self, key):
        if key.keytype != Key.HAT:
            if key.value != pygame.K_P:
                return

        if key.value == Key.HAT_UP:
            self.up()
        elif key.value == Key.HAT_DOWN:
            self.down()
        if key.value == Key.HAT_LEFT:
            self.left()
        elif key.value == Key.HAT_RIGHT:
            self.right()
        elif key.value == Key.KEY_P:
            self.pause()
    
    def left(self):
        print("Joystick moved left")
    
    def right(self):
        print("Joystick moved right")
    
    def up(self):
        print("Joystick moved up")
    
    def down(self):
        print("Joystick moved down")
    
    def pause(self):
        print("Paused")