from machine import I2C, Pin
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd  # Asume que i2c_lcd es parte de tu biblioteca lcd_api
from keypad_utp import KeyPad
from time import sleep

# Configuración inicial de I2C y LCD
i2c = I2C(scl=Pin(22), sda=Pin(21))

lcd = I2cLcd(i2c, 0x27, 2, 16)
LEDR = Pin(26,Pin.OUT)
LEDG = Pin(25,Pin.OUT)

Teclas = [
    '1', '2', '3', 'A',
    '4', '5', '6', 'B',
    '7', '8', '9', 'C',
    '*', '0', '#', 'D',
]

keypad = KeyPad(r1=19, r2=18, r3=5, r4=4, c1=13, c2=12, c3=14, c4=27, keys=Teclas)
keypad.start()

#password = 4743
#pos = 0
#intentos = 0

def INICIO():
    lcd.move_to(0, 0) 
    lcd.putstr("!Un bonito dia")

    lcd.move_to(0, 1) 
    lcd.putstr("   para un Shot!")

INICIO()

try:
    while True:
        key = keypad.get_key()

        if key:
            if key == 'A':
                lcd.clear()
                lcd.move_to(0, 0) 
                lcd.putstr("Que shot deseas?")
                lcd.move_to(0, 1) 
                lcd.putstr("   1   2   3 ")
            
            if key== '1':
                lcd.clear()
                lcd.move_to(0, 0) 
                lcd.putstr("    Opcion 1")
                lcd.move_to(0, 1) 
                lcd.putstr("  AGUARDIENTICO")
                
                if key == 'A':
                   lcd.clear()
                   lcd.move_to(0, 0) 
                   lcd.putstr("Adicionar otra")
                   lcd.move_to(0, 1) 
                   lcd.putstr("   bebida?")

            if key== '2':
                lcd.clear()
                lcd.move_to(0, 0) 
                lcd.putstr("    Opcion 2")
                lcd.move_to(0, 1) 
                lcd.putstr("    RONCITO")

            if key== '3':
                lcd.clear()
                lcd.move_to(0, 0) 
                lcd.putstr("    Opcion 3")
                lcd.move_to(0, 1) 
                lcd.putstr("    FOUR LOKO")
                

                
#
 #               if pos == 0:
  #                  d0 = key
 #               if pos == 1:
  #                  d1 = key
   #             if pos == 2:
    #                d2 = key
#                if pos == 3:
#                    d3 = key
 #               if pos == 4:
 #                   d4 = key
 #                   if key == '*':
 #                       lcd.clear()
 #                       pos = 0
 #               if pos == 4:
#                  d4 = key
#                    if keypad.get_key() == '#':
#                        lcd.clear()
#                        break
#                pos = pos + 1
#                
#                if key == '*':
#                    lcd.clear()
#                    pos = 0
#            
#            if pos > 4:
#                user = d0 + d1 + d2 + d3
#                userINGRE = int(user)
#                sleep(0.2)
#                lcd.clear()
#
#                if userINGRE == password:
#                    lcd.move_to(0, 0)
#                    lcd.putstr("    >> OK <<    ")
#                    BIPGREEN()
#
#                if userINGRE != password:
#                    lcd.move_to(0, 0)
#                    lcd.putstr("    ERROR!!!    ")
#                    intentos = intentos + 1
#                    BIPRED()
#
#                if intentos >= 3:
#                    lcd.move_to(0, 0)
#                    lcd.putstr("> User blocked <")

#                    LEDR.value(1)
#                   sleep(1000)
#                    LEDR.value(0)
                
#                sleep(0.2)
#               lcd.clear()
#                pos = 0

        sleep(0.1)
finally:
    keypad.stop()