from PIL import Image, ImageDraw
import epaper
# BLACK  = 0x000000   #   0000  BGR
# WHITE  = 0xffffff   #   0001
# GREEN  = 0x00ff00   #   0010
# BLUE   = 0xff0000   #   0011
# RED    = 0x0000ff   #   0100
# YELLOW = 0x00ffff   #   0101
# ORANGE = 0x0080ff   #   0110

class ImageShowError(Exception):
    pass


class ImageShow:
    epd = epaper.epaper("epd7in3f").EPD()

    def __init__(self) -> None:
        self.epd.init()
        self.epd.Clear()


    def show_image(self, image: Image) -> None:
        if image.size != (self.epd.width, self.epd.height):
            raise ImageShowError("Image does not match screen size")

        self.__init__()
        # possibly include a blank image to clear screen
        print("Displaying image")
        self.epd.display(self.epd.getbuffer(image))
        self.epd.sleep()
        

    def draw_sample_image(self):
        Himage = Image.new('RGB', (self.epd.width, self.epd.height), self.epd.WHITE)  # 255: clear the frame
        draw = ImageDraw.Draw(Himage)
        # draw.text((5, 0), 'hello world', font = font18, fill = self.epd.RED)
        # draw.text((5, 20), '7.3inch e-Paper (F)', font = font24, fill = self.epd.YELLOW)
        # draw.text((5, 45), u'微雪电子', font = font40, fill = self.epd.GREEN)
        # draw.text((5, 85), u'微雪电子', font = font40, fill = self.epd.BLUE)
        # draw.text((5, 125), u'微雪电子', font = font40, fill = self.epd.ORANGE)

        draw.line((5, 170, 80, 245), fill = self.epd.BLUE)
        draw.line((80, 170, 5, 245), fill = self.epd.ORANGE)
        draw.rectangle((5, 170, 80, 245), outline = self.epd.BLACK)
        draw.rectangle((90, 170, 165, 245), fill = self.epd.GREEN)
        draw.arc((5, 250, 80, 325), 0, 360, fill = self.epd.RED)
        draw.chord((90, 250, 165, 325), 0, 360, fill = self.epd.YELLOW)
        self.epd.display(self.epd.getbuffer(Himage))