from PIL import Image, ImageDraw, ImageFont, ImageOps
import re
import time
import widget
import argparse
try:
    import epd7in5b
    testMode = False
except:
    testMode = True


parser = argparse.ArgumentParser(description="EinkPaper display")
parser.add_argument('-t', '--test', help='enable test mode', action='store_true')
args = parser.parse_args()
if args.test:
    testMode = True

print("RUNNING IN TESTMODE: "+str(testMode))

if not testMode:
    epd = epd7in5b.EPD()
    epd.init()

width = 640
height = 384
cwidth = int(round(width/3))
cheight = int(round(height/3))

image_yellow = Image.new('1', (width, height), 255)    # 255: clear the frame
draw_yellow = ImageDraw.Draw(image_yellow)
image_black = Image.new('1', (width, height), 255)    # 255: clear the frame
draw_black = ImageDraw.Draw(image_black)

#str = """abcdefghijkmnop test test test hello Current temperature: 17C, Feels like: 20C Traffic is light. Should take 21 minutes to get to work. BREAKING NEWS: news headline goes here. To do: 1) Fix MHDoorLock 2) Work on waveshare epaper device"""
str1 = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
str2 = """Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
str3 = """But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"""
strList = []
strList.append(str1)
strList.append(str2)
strList.append(str3)

def initWidgets():
    widgetList = []
    widgetList.append(widget.TextWidget(cwidth, cheight, 0, 0, 1, 2, str1))
    widgetList.append(widget.TextWidget(cwidth, cheight, 1, 0, 2, 1, str2))
    #widgetList.append(widget.ImageWidget(cwidth, cheight, 2, 1, 1, 1, "mono", "faces.jpg"))
    widgetList.append(widget.ImageWidget(cwidth, cheight, 1, 1, 2, 2, "mono", "img1.jpg"))
    widgetList.append(widget.ImageWidget(cwidth, cheight, 0, 2, 1, 1, "mono", "img1.jpg"))
    return widgetList

def drawWidget(w):
    coordX = w.cellX*cwidth
    coordY = w.cellY*cheight
    image_black.paste(w.image_black, (coordX, coordY))
    image_yellow.paste(w.image_yellow, (coordX, coordY))

def render(index):
    for widg in widgetList:
        drawWidget(widg)
    if not testMode:
        print("pushing to display frame")
        epd.display_frame(epd.get_frame_buffer(image_black),epd.get_frame_buffer(image_yellow))
        print("done")
    else:
        print("saving canvas images")
        image_black.save("imgBlack.bmp")
        image_yellow.save("imgYellow.bmp")
        print("done")

widgetList = initWidgets()

i = 0
while(True):
    render(i)
    i = (i + 1)%3
    time.sleep(10)
