import time
try:
    from Buzzer import PassiveBuzzer 
    from LightStrip import LightStrip 
    from Displays import LCDDisplay 
    from Button import Button 
except ImportError as e:
    print(f"Error importing hardware modules: {e}")
    print("Please ensure Buzzer.py, LightStrip.py, Displays.py, and Button.py are on your Pico.")
    time.sleep(5) 
class Product:
    """Base class for all products."""
    def __init__(self, name, price):
        self.name = name
        self.price = price
    def line1(self):
        raise NotImplementedError("Subclass must implement line1()")
    def line2(self):
        raise NotImplementedError("Subclass must implement line2()")
class PackedProduct(Product):
    """Represents a pre-packaged product."""
    def __init__(self, sku, name, description, quantity, unit, price):
        super().__init__(name, price)
        self.sku = sku
        self.description = description
        self.quantity = quantity
        self.unit = unit
    def line1(self):
        return f"{self.name} {self.quantity} {self.unit}"
    def line2(self):
        return f"Price: ${self.price:.2f}"
class BulkProduct(Product):
    def __init__(self, sku, name, description, unit_price, bulk_unit='lb'):
        super().__init__(name, unit_price)
        self.sku = sku
        self.description = description
        self.unit_price = unit_price
        self.bulk_unit = bulk_unit
    def line1(self):
        return f"{self.name} (BULK)"
    def line2(self):
        return f"${self.unit_price:.2f} / {self.bulk_unit}"
class DigitalPriceDisplay:
    def __init__(self):
        self.buzzer = PassiveBuzzer(pin=15, name='Buzz')
        self.lightstrip = LightStrip(pin=2, name='Lights')
        self.display = LCDDisplay(sda=0, scl=1) 
        self.leftbutton = Button(pin=17, name='left', handler=self)
        self.rightbutton = Button(pin=16, name='right', handler=self)
        
        self.myproducts = []
        self.addProducts()
        self.index = 0 
    def addProducts(self):
        
        self.myproducts.append(PackedProduct('1234', 'Oatmeal', 'Organic oatmeal', 100, 'GM', 2.49))
        self.myproducts.append(PackedProduct('1234', 'Cereal', 'Cheerios', 10, 'Kelloggs', 4.49))
        self.myproducts.append(PackedProduct('5678', 'Milk', 'Whole Milk 1 Gal', 1, 'DairyFarm', 3.99))
        self.myproducts.append(PackedProduct('9012', 'Eggs', 'Dozen Large Eggs', 12, 'FarmFresh', 2.99))
        
        self.myproducts.append(BulkProduct('B001', 'Coffee Beans', 'Columbian Blend', 7.99, 'lb'))
        self.myproducts.append(BulkProduct('B002', 'Lentils', 'Dried Red Lentils', 3.50, 'kg'))
        # -------------------------
    def showInfo(self):
        self.display.clear() 
        currentproduct = self.myproducts[self.index]
        self.display.showText(currentproduct.line1(), 0)
        self.display.showText(currentproduct.line2(), 1) 
    
    def previousProduct(self):
        if self.index > 0:
            self.index -= 1
            self.showInfo()
            self.buzzer.beep(tone=500)
    def nextProduct(self):
        if self.index < len(self.myproducts) - 1:
            self.index += 1
            self.showInfo()
            self.buzzer.beep(tone=1000)
    def buttonPressed(self, name):
        if name == 'left':
            self.previousProduct()
        elif name == 'right':
            self.nextProduct()
    def buttonReleased(self, name):
        pass
if __name__ == '__main__':
    # Initial setup delay
    time.sleep(0.1)
    
    # Start the application
    mydisplay = DigitalPriceDisplay()
    mydisplay.showInfo()
    
    # Since this is running on the Pico, the main loop is typically handled 
    # by the Button class using interrupts, so we just need a simple sleep loop 
    # to keep the script running and responsive to button interrupts.
    print("Application started. Waiting for button presses...")
    while True:
        time.sleep(1)