import board, busio, time, digitalio

from lcd.lcd import LCD
from lcd.i2c_pcf8574_interface import I2CPCF8574Interface

# Create you own custom characters here: http://educ8s.tv/tools/lcd-character-creator/
Left_Point = (0x03,0x06,0x0C,0x1F,0x1F,0x0C,0x06,0x03)
Line = (0x00,0x00,0x00,0x1F,0x1F,0x00,0x00,0x00)
Right_Point = (0x18,0x0C,0x06,0x1F,0x1F,0x06,0x0C,0x18)

Heart_Left = (0x06,0x0F,0x1F,0x1F,0x0F,0x07,0x03,0x01)
Heart_Right = (0x0C,0x1E,0x1F,0x1F,0x1E,0x1C,0x18,0x10)
Diamond_Right = (0x10,0x18,0x1C,0x1E,0x1E,0x1C,0x18,0x10)
Diamond_Left = (0x01,0x03,0x07,0x0F,0x0F,0x07,0x03,0x01)
Spade_Left = (0x01,0x03,0x07,0x0F,0x0F,0x06,0x01,0x03)
Spade_Right = (0x10,0x18,0x1C,0x1E,0x1E,0x0C,0x10,0x18)
Club_Left = (0x01,0x03,0x01,0x0C,0x1E,0x0C,0x01,0x03)
Club_Right = (0x10,0x18,0x10,0x06,0x0F,0x06,0x10,0x18)

Info_Left = (0x03,0x03,0x00,0x07,0x03,0x03,0x03,0x03)
Info_Right = (0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x1C)


sda, scl = board.GP0, board.GP1
i2c = busio.I2C(scl, sda)
lcd = LCD(I2CPCF8574Interface(i2c, 0x27), num_rows=4, num_cols=20) #If address 0x27 does not work try 0x3F

lcd.create_char(0,Heart_Left)
lcd.create_char(1,Heart_Right)
lcd.create_char(2,Diamond_Left)
lcd.create_char(3,Diamond_Right)
lcd.create_char(4,Spade_Left)
lcd.create_char(5,Spade_Right)
lcd.create_char(6,Club_Left)
lcd.create_char(7,Club_Right)

lcd.set_cursor_pos(0,0)
lcd.print("Shuffle Smith")
lcd.set_cursor_pos(1,1)
lcd.write(0)
lcd.set_cursor_pos(1,2)
lcd.write(1)
lcd.set_cursor_pos(1,5)
lcd.write(4)
lcd.set_cursor_pos(1,6)
lcd.write(5)
lcd.set_cursor_pos(1,9)
lcd.write(2)
lcd.set_cursor_pos(1,10)
lcd.write(3)
lcd.set_cursor_pos(1,13)
lcd.write(6)
lcd.set_cursor_pos(1,14)
lcd.write(7)

time.sleep(5)
lcd.clear()
time.sleep(2)

lcd.create_char(0,Left_Point)
lcd.create_char(1,Line)
lcd.create_char(2,Right_Point)

lcd.create_char(3,Info_Left)
lcd.create_char(4,Info_Right)

option = 0
z = 0
x = 0

next_button = digitalio.DigitalInOut(board.GP15)
next_button.switch_to_input(pull=digitalio.Pull.UP)

back_button = digitalio.DigitalInOut(board.GP16)
back_button.switch_to_input(pull=digitalio.Pull.UP)


while True:
    if next_button.value == True:
        x = 0
    if back_button.value == True:
        y = 0
        
    if option == 0:
        lcd.set_cursor_pos(0,2)
        lcd.print("Full Shuffle")
        lcd.set_cursor_pos(1,0)
        lcd.write(3)
        lcd.set_cursor_pos(1,1)
        lcd.write(4)
        lcd.set_cursor_pos(1,5)
        lcd.print("Select")
        lcd.set_cursor_pos(1,14)
        lcd.write(1)
        lcd.set_cursor_pos(1,15)
        lcd.write(2)
        
    if option == 1:
        lcd.set_cursor_pos(0,2)
        lcd.print("Half Shuffle")
        lcd.set_cursor_pos(1,0)
        lcd.write(3)
        lcd.set_cursor_pos(1,1)
        lcd.write(4)
        lcd.set_cursor_pos(1,5)
        lcd.print("Select")
        lcd.set_cursor_pos(1,14)
        lcd.write(1)
        lcd.set_cursor_pos(1,15)
        lcd.write(2)
        
    if option == 2:
        lcd.set_cursor_pos(0,1)
        lcd.print("Simple Shuffle")
        lcd.set_cursor_pos(1,0)
        lcd.write(3)
        lcd.set_cursor_pos(1,1)
        lcd.write(4)
        lcd.set_cursor_pos(1,5)
        lcd.print("Select")
        lcd.set_cursor_pos(1,14)
        lcd.write(1)
        lcd.set_cursor_pos(1,15)
        lcd.write(2)
        
    if option == 3:
        lcd.set_cursor_pos(0,0)
        lcd.print("Complex Shuffle")
        lcd.set_cursor_pos(1,0)
        lcd.write(3)
        lcd.set_cursor_pos(1,1)
        lcd.write(4)
        lcd.set_cursor_pos(1,5)
        lcd.print("Select")
        lcd.set_cursor_pos(1,14)
        lcd.write(1)
        lcd.set_cursor_pos(1,15)
        lcd.write(2)
    
    #info screen for full shuffle ID 0
    if option == 4:
        lcd.set_cursor_pos(0,0)
        lcd.print("1:15min")
        lcd.set_cursor_pos(0,10)
        lcd.print("2 cuts")
        lcd.set_cursor_pos(1,0)
        lcd.write(0)
        lcd.set_cursor_pos(1,1)
        lcd.write(1)
        lcd.set_cursor_pos(1,7)
        lcd.print("3 riffles")
        
    #info screen for half shuffle ID 1
    if option == 5:
        lcd.set_cursor_pos(0,0)
        lcd.print("0:45sec")
        lcd.set_cursor_pos(0,10)
        lcd.print("1 cuts")
        lcd.set_cursor_pos(1,0)
        lcd.write(0)
        lcd.set_cursor_pos(1,1)
        lcd.write(1)
        lcd.set_cursor_pos(1,7)
        lcd.print("2 riffles")
        
    #info screen for simple shuffle ID 2
    if option == 6:
        lcd.set_cursor_pos(0,0)
        lcd.print("0:15sec")
        lcd.set_cursor_pos(0,10)
        lcd.print("0 cuts")
        lcd.set_cursor_pos(1,0)
        lcd.write(0)
        lcd.set_cursor_pos(1,1)
        lcd.write(1)
        lcd.set_cursor_pos(1,7)
        lcd.print("1 riffles")
        
    #info screen for complex shuffle ID 3
    if option == 7:
        lcd.set_cursor_pos(0,0)
        lcd.print("2:15min")
        lcd.set_cursor_pos(0,10)
        lcd.print("4 cuts")
        lcd.set_cursor_pos(1,0)
        lcd.write(0)
        lcd.set_cursor_pos(1,1)
        lcd.write(1)
        lcd.set_cursor_pos(1,7)
        lcd.print("5 riffles")
    
    
    if next_button.value == False and x == 0 and z == 0:
        x = 1
        if option == 0:
            option = 1
            lcd.clear()
        elif option == 1:
            option = 2
            lcd.clear()
        elif option == 2:
            option = 3
            lcd.clear()
        else:
            option = 0
            lcd.clear()
    

    if back_button.value == False and y == 0:
        y = 1
        if option == 0:
            option = 4
            lcd.clear()
            z = 1
        elif option == 1:
            option = 5
            lcd.clear()
            z = 1
        elif option == 2:
            option = 6
            lcd.clear()
            z = 1
        elif option == 3:
            option = 7
            lcd.clear()
            z = 1
        elif option == 4:
            option = 0
            lcd.clear()
            z = 0
        elif option == 5:
            option = 1
            lcd.clear()
            z = 0
        elif option == 6:
            option = 2
            lcd.clear()
            z = 0
        else:
            option = 3
            lcd.clear()
            z = 0
    
    time.sleep(.1)