#####################################################################################################################################################
######################################################################## INFO #######################################################################
#####################################################################################################################################################

"""
    A Python class implementing KeyboardCapture, the standard keyboard-interrupt poller.
    Works transparently on Windows and Posix (Linux, Mac OS X).
    Doesn't work with IDLE.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
"""

#####################################################################################################################################################
###################################################################### IMPORTS ######################################################################
#####################################################################################################################################################

# Needed imports
import os
from typing import List, Optional
from typing_extensions import Self

# Additional imports if on Windows
if os.name == "nt":
    import msvcrt

# Additional imports if on Linux or MacOS
else:
    import sys
    import termios
    import atexit
    from select import select

#####################################################################################################################################################
###################################################################### CLASSES ######################################################################
#####################################################################################################################################################

class KeyboardCapture:

    """
        A class to capture keyboard inputs.
        This class provides methods that act differently depending on the OS.
    """

    #############################################################################################################################################
    #                                                                CONSTRUCTOR                                                                #
    #############################################################################################################################################

    def __init__ ( self: Self
                 ) ->    Self:
        
        """
            Creates a KeyboardCapture object that you can call to do various keyboard things.
            In:
                * self: A reference to the current object being created (should not be provided by the user).
            Out:
                * self: The current object being created.
        """

        # Windows
        if os.name == "nt":
            pass

        # Linux or MacOS
        else:

            # Save the terminal settings
            self.fd = sys.stdin.fileno()
            self.new_term = termios.tcgetattr(self.fd)
            self.old_term = termios.tcgetattr(self.fd)

            # New terminal setting unbuffered
            self.new_term[3] = (self.new_term[3] & ~termios.ICANON & ~termios.ECHO)
            termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term)

            # Support normal-terminal reset at exit
            atexit.register(self.set_normal_term)

    #############################################################################################################################################
    #                                                               PUBLIC METHODS                                                              #
    #############################################################################################################################################

    def set_normal_term ( self: Self
                        ) ->    None:
        
        """
            Resets to normal terminal.
            On Windows this is a no-op.
            In:
                * self: A reference to the current object (should not be provided by the user).
            Out:
                * None.
        """

        # Windows
        if os.name == "nt":
            pass

        # Linux or MacOS
        else:

            # Reset the terminal settings
            termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)

    #############################################################################################################################################

    def get_char ( self:          Self,
                   accepted_keys: List[str] = []
                 ) ->             Optional[str]:
        
        """
            Returns a keyboard character after was_hit() has been called.
            Should not be called in the same program as get_arrow().
            In:
                * self:           A reference to the current object (should not be provided by the user).
                * accepted_chars: A list of acceptable keys (if set and key pressed out of this filter then returns None).
            Out:
                * result: A string of length 1.
        """

        # Check if a key was hit
        if self.was_hit():
            
            # Get the character depending on the OS
            if os.name == "nt":
                key = msvcrt.getch().decode("utf-8")
            else:
                key = sys.stdin.read(1)
            
            # If the key is valid, we return it
            if key in accepted_keys:
                return key
        
        # If no key was hit or the key is not valid, we return None
        return None
    
    #############################################################################################################################################

    def get_arrow ( self: Self
                  ) ->    int:
        
        """
            Returns an arrow-key code after was_hit() has been called.
            Codes are {0: Up, 1: Right, 2: Down, 3: Left}.
            Should not be called in the same program as get_char().
            In:
                * self: A reference to the current object (should not be provided by the user).
            Out:
                * result: An integer between 0 and 3, corresponding to the arrow key pressed.
        """

        # Windows
        if os.name == "nt":

            # Get the key and skip 0xE0
            msvcrt.getch()
            c = msvcrt.getch()

            # Set the arrow key code
            vals = [72, 77, 80, 75]

        # Linux or MacOS
        else:
            
            # Get the key
            c = sys.stdin.read(3)[2]

            # Set the arrow key code
            vals = [65, 67, 66, 68]

        # Return the arrow key code
        return vals.index(ord(c.decode("utf-8")))

    #############################################################################################################################################

    def was_hit ( self: Self
                ) ->    bool:
        
        """
            Returns True if keyboard character was hit, False otherwise.
            In:
                * self: A reference to the current object (should not be provided by the user).
            Out:
                * result: A boolean.
        """

        # Windows
        if os.name == "nt":

            # Check if a key was hit
            return msvcrt.kbhit()

        # Linux or MacOS
        else:

            # Check if a key was hit
            dr, dw, de = select([sys.stdin], [], [], 0)
            return dr != []

#####################################################################################################################################################
####################################################################### TESTS #######################################################################
#####################################################################################################################################################

if __name__ == "__main__":

    # Create a KeyboardCapture object
    kb = KeyboardCapture()

    # Ask for a few keys
    print("Captured keys are k m o and q to exit")
    state = "wait"
    while True:

        # Waiting state
        if state == "wait":
            print("Waiting for input")
            state = "get"

        # Getting state
        c = kb.get_char(["k", "m", "o", "q"])
        if c is not None:

            # If the key is q, we exit
            if c == "q":
                break
                
            # If the key is another accepted one, we go to the waiting state
            print("key captured:", c)
            state = "wait"

    # Reset the terminal
    kb.set_normal_term()

#####################################################################################################################################################
#####################################################################################################################################################