"""Mockup of multi-camera parameter display.

Designed for 1920x720 area.
"""
import sys
import tkinter as tk
import select
import tty
import termios


class ParamGrid(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.create_grid()

    def create_grid(self):
        #super().configure(padding=(50,50,50,50))
        super().config(bg='black')
        #print(super().config());

        labelfont = ('helvetica', 38, 'bold')
        bg_color = 'black'
        fg_color = 'white'
        self.cameras_label = tk.Label(self, text='Cameras', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=33, anchor='e')
        self.cameras_value = tk.Label(self, text='1-3', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=33, anchor='w')
        self.wb_label = tk.Label(self, text='WB', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=8, anchor='e')
        self.wb_value = tk.Label(self, text='6500', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=8, anchor='w')
        self.shutter_label = tk.Label(self, text='SHUTTER', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=9, anchor='e')
        self.shutter_value = tk.Label(self, text='1/100', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=7, anchor='w')
        self.fstop_label = tk.Label(self, text='F', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=8, anchor='e')
        self.fstop_value = tk.Label(self, text='4.5', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=8, anchor='w')
        self.iso_label = tk.Label(self, text='ISO', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=8, anchor='e')
        self.iso_value = tk.Label(self, text='2000', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=8, anchor='w')
        self.enc_label = tk.Label(self, text='ENC', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=8, anchor='e')
        self.enc_value = tk.Label(self, text='ProRes 422 LT', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=24, anchor='w')
        self.fps_label = tk.Label(self, text='FPS', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=8, anchor='e')
        self.fps_value = tk.Label(self, text='59.94', font=labelfont, bg=bg_color, fg=fg_color, height=2, width=24, anchor='w')
        self.status_standby_label = tk.Label(self, text='STANDBY', font=labelfont, bg='green', fg='white', height=2, width=16)
        self.status_recording_label = tk.Label(self, text='RECORDING', font=labelfont, bg='red', fg='white', height=2, width=16)
        self.error_value = tk.Label(self, text='ERROR: cam7 shutter', font=labelfont, bg='yellow', fg='black', height=2, width=67, anchor='w')
        self.noerror_value = tk.Label(self, text=' ', font=labelfont, fg='black', bg=bg_color, height=2, width=67, anchor='w')

        # place the frame in the window
        self.grid(row=0, column=0)

        # place the labels in the frame
        # row 0: camera numbers
        self.cameras_label.grid(row=0, column=0, columnspan=4)
        self.cameras_value.grid(row=0, column=4, columnspan=4)

        # row 1: blank

        # row 2: wb, shutter, fstop, iso
        self.wb_label.grid(row=2, column=0)
        self.wb_value.grid(row=2, column=1)
        self.shutter_label.grid(row=2, column=2)
        self.shutter_value.grid(row=2, column=3)
        self.fstop_label.grid(row=2, column=4)
        self.fstop_value.grid(row=2, column=5)
        self.iso_label.grid(row=2, column=6)
        self.iso_value.grid(row=2, column=7)

        # row 3: blank
        
        # row 4: encoding
        self.enc_label.grid(row=4, column=4)
        self.enc_value.grid(row=4, column=5, columnspan=3)

        # row 5: status, frame rate
        self.status_standby_label.grid(row=5, column=0, columnspan=2)
        self.fps_label.grid(row=5, column=4)
        self.fps_value.grid(row=5, column=5, columnspan=3)

        # row 6: error message
        self.error_value.grid(row=6, column=0, columnspan=8)
        #self.noerror_value.grid(row=6, column=0, columnspan=8)

        # minimum sizes for rows and columns
        self.columnconfigure((0,1,4,5,6,7,8), minsize=240)
        self.columnconfigure(2, minsize=260)
        self.columnconfigure(3, minsize=220)
        self.rowconfigure((0,1,2,3,4,5), minsize=50)    # height of empty rows
        self.rowconfigure(6, minsize=148)


def periodic(root):
    while 1:
        r = select.select([sys.stdin],[],[],0.0)[0]
        if not r:
            break
        k = sys.stdin.read(1)
        print('received 0x%x' % (ord(k)))
    root.after(100, periodic, root)


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry("1916x1076+0-32")
    root.resizable(0,0)         # no resizing
    grid = ParamGrid(master=root)

    print('hello world')

    orig_settings = termios.tcgetattr(sys.stdin)
    try:
        tty.setcbreak(sys.stdin)
        periodic(root)
        grid.mainloop()
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, orig_settings)
        print('goodbye')

