from mfrc522 import SimpleMFRC522
import time


class RFIDReader:
    
    def __init__(self):
        self.reader = SimpleMFRC522()

    def write_to_rfid(self, text):
        """Write the given text to the RFID tag."""
        print("Now place your tag to write...")
        self.reader.write(text)
        print("Tadaaa, written")
        print("-- --")

    def read_from_rfid(self):
        """Read the text from the RFID tag."""
        print("Bring your RFID chip close to the reader...")
        id, text = self.reader.read()
        #print(f"ID: {id}")
        #print(f"Answer from RFID chip: {text}")
        return id, text

    
def main():
    rfid_reader = RFIDReader()
    
    try:
        while True:
            # Ask the user what they want to do
            action = input("Enter 'r' to read from RFID or 'w' to write to RFID: ").strip().lower()
            
            if action == 'r':
                id, text = rfid_reader.read_from_rfid()
                print(f"ID: {id}")
                print(f"Text: {text}")
            elif action == 'w':
                text_to_write = input("Enter the text to write to the RFID tag: ").strip()
                rfid_reader.write_to_rfid(text_to_write)
            else:
                print("Invalid action. Please enter 'r' to read or 'w' to write.")
            
            # Small delay to prevent bouncing issues
            #wrtime.sleep(1)
    
    except KeyboardInterrupt:
        print("Exiting RFID test.")
    
    finally:
        #GPIO.cleanup()
        print("Okay, code stopped running.")

if __name__ == "__main__":
    main()