#!/usr/bin/python3

from __future__ import print_function

import argparse
from papirus import Papirus
from PIL import ImageFont, ImageDraw, Image
import sys
import os
import time

# Check EPD_SIZE is defined
EPD_SIZE=0.0
if os.path.exists('/etc/default/epd-fuse'):
    exec(open('/etc/default/epd-fuse').read())
if EPD_SIZE == 0.0:
    print("Please select your screen size by running 'papirus-config'.")
    sys.exit()

# Running as root only needed for older Raspbians without /dev/gpiomem
if not (os.path.exists('/dev/gpiomem') and os.access('/dev/gpiomem', os.R_OK | os.W_OK)):
    user = os.getuid()
    if user != 0:
        print("Please run script as root")
        sys.exit()

WHITE = 1
BLACK = 0


def getFontSize(my_papirus, printstring):
    # returns (ideal fontsize, (length of text, height of text)) that maximally
    # fills a papirus object for a given string
    fontsize = 0
    stringlength = 0
    stringwidth = 0

    maxLength = my_papirus.width
    maxHeight = my_papirus.height

    while (stringlength <= maxLength and stringwidth <= maxHeight):

        fontsize += 1
        font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', fontsize)
        size = font.getsize(printstring)
        stringlength = size[0]
        stringwidth = size[1]

    font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', fontsize-1)
    return fontsize-1, font.getsize(printstring)


def drawWords(my_papirus, printstring, fontsize, dims, partial=False):

    # initially set all white background
    image = Image.new('1', my_papirus.size, WHITE)

    # prepare for drawing
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', fontsize)

    draw.text(((my_papirus.width-dims[0])/2, (my_papirus.height/2) - (dims[1]/2)), printstring, font=font, fill=BLACK)

    my_papirus.display(image)
    if partial:
        my_papirus.partial_update()
    else:
        my_papirus.update()


p = argparse.ArgumentParser()
p.add_argument('displaytext', type=str, help="Text to display (max 40 char)")
p.add_argument('--rotation', '-r', type=int, default=0, help="Rotation one of 0, 90, 180,270")
p.add_argument('--partialupdate', '-p', action='store_true', default=False, help="Use partial instead of full update")
args = p.parse_args()

printString = args.displaytext
if len(printString) > 40:
    print('WARNING: string length is too large for single line printing, truncating at 40 chars')
    printString = printString[0:40]

my_papirus = Papirus(rotation = args.rotation)
fontsize, dims= getFontSize(my_papirus, printString)
print("Writing to Papirus....")
drawWords(my_papirus, printString, fontsize, dims, args.partialupdate)
print("Finished!")
