from PIL import Image, ImageDraw, ImageFont, ImageOps
import re
import time
#import widgets.widget as widget
import argparse
import os
import toml

import requests
from io import BytesIO

try:
    import drivers.epd7in5b
    testMode = False
except:
    testMode = True

canvas_url_black = 'http://0.0.0.0:5000/display'

mydir = os.path.dirname(os.path.abspath(__file__))

parser = argparse.ArgumentParser(description="NotiFrame 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 = drivers.epd7in5b.EPD()
    epd.init()

def render(index):
    while(True):
        print("requesting display image")
        try:
            blk = requests.get(canvas_url_black)
            #ylw = requests.get(urlylw)
            image_black = Image.open(BytesIO(blk.content))
            image_yellow = None
            #image_yellow = Image.open(BytesIO(ylw.content))
            #draw_black.rectangle(xy=((0,0), image_black.size), fill=255)
            #draw_yellow.rectangle(xy=((0,0), image_yellow.size), fill=255)
            if not testMode:
                epd.display_frame(epd.get_frame_buffer(image_black.rotate(180)),epd.get_frame_buffer(image_yellow.rotate(180)))
            else:
                image_black.save(os.path.join(mydir, 'test/imgBlackDisplayClient.bmp'))
                #image_yellow.save(os.path.join(mydir, 'test/imgYellow.bmp'))
        except:
            print("failed to load display image")
        i =+ 1
        time.sleep(20)
i = 0
render(i)