# Find the largest size possible and set AXIS to it
maxgeo=root_window.tk.call("wm","maxsize",".")
try:
   fullsize=maxgeo.split(' ')[0] + 'x' + maxgeo.split(' ')[1]
except:
   fullsize=str(maxgeo[0]) + 'x' + str(maxgeo[1])
root_window.tk.call("wm","geometry",".",fullsize)
# Uncomment for fullscreen
root_window.attributes('-fullscreen', True)

# change the font

font = 'mono 10'
fname,fsize = font.split()
root_window.tk.call('font','configure','TkDefaultFont','-family',fname,'-size',fsize)

# gcode font is independent

root_window.tk.call('.pane','paneconfigure','.pane.top',"-stretch","never","-height","640", "-minsize","580")

root_window.tk.call('.pane.top.tabs.fmdi.history','configure','-foreground','lime',"-font","mono 11 bold",'-background','black')
root_window.tk.call(pane_bottom + ".t.text","configure","-foreground","Lime","-font","mono 11 bold","-background","black")
#root_window.tk.call('.pane.top.gcodes','configure','-foreground','limeg',"-font","mono 11 bold",'-background','black')

# change dro screen
root_window.tk.call('.pane.top.right.fnumbers.text','configure','-foreground','lime','-background','black')

root_window.tk.call('.pane.bottom.t.sb','configure','-relief','flat','-troughcolor','grey30','-background','grey90','-width','40','-elementborderwidth','1')
root_window.tk.call('.pane.top.right','configure','-width','50','-arcradius','3','-font','mono 14 bold')
root_window.tk.call('.pane.top.tabs','configure','-width','50','-arcradius','3','-font','mono 14 bold')

root_window.tk.call('.pane.top.tabs.fmanual.axes.axisx','configure','-width','2','-height','2','-relief','solid','-font','mono 13 bold')
root_window.tk.call('.pane.top.tabs.fmanual.axes.axisy','configure','-width','2','-height','2','-relief','solid','-font','mono 13 bold')
root_window.tk.call('.pane.top.tabs.fmanual.axes.axisz','configure','-width','2','-height','2','-relief','solid','-font','mono 13 bold')

root_window.tk.call('.pane.top.tabs.fmanual.jogf.jog.jogminus','configure','-width','2','-height','1','-font','mono 13 bold')
root_window.tk.call('.pane.top.tabs.fmanual.jogf.jog.jogplus','configure','-width','2','-height','1','-font','mono 13 bold')
root_window.tk.call('.pane.top.tabs.fmanual.jogf.jog.jogincr','configure','-width','10','-height','1','-font','mono 12 bold')

##########################################


def _remaining_error_count(widgets):
    """ Returns the count of remaining error messages """
    count = 0
    for i, item in enumerate(widgets):
        frame, icon, text, button, iname = item
        if iname == "icon_std_error":
            count += 1
    return count


def my_add(self, iconname, message):
    """ Signal to PSNG that an error has occurred. """
    self.original_add(iconname, message)

    if iconname == "error":
        probe_user_comp["error"] = True


def my_remove(self, widgets):
    """ Signal to PSNG when all errors have been cleared. """
    self.original_remove(widgets)

    if _remaining_error_count(self.widgets) == 0:
        probe_user_comp["error"] = False


# Rename the original add/remove method so we can retain the ability to call them
Notification.original_add = Notification.add
Notification.original_remove = Notification.remove

# Replace the add/remove methods with a wrapped version that manages an error pin
Notification.add = my_add
Notification.remove = my_remove

if hal_present == 1:
    # Add an probe.user.error pin
    probe_user_comp = hal.component("probe.user")
    probe_user_comp.newpin("error", hal.HAL_BIT, hal.HAL_OUT)
    probe_user_comp.ready()

