# ==============================================================================
"""COUNTER : a user-controlled digital counter"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0" # skeleton version
__date__    = "2022-11-12"
# ==============================================================================
from ezTK import *
# ------------------------------------------------------------------------------
def main():
  """create the main window and pack the widgets"""
  global win
  font1, font2 = 'Courier 16 bold', 'Arial 96 bold'
  win = Win(title='COUNTER', font=font1, op=5,bg='#DDD')
  # ----------------------------------------------------------------------------
  # TODO (use 1 Frame, 7 Buttons using font1, 1 Label using font2)
  # ----------------------------------------------------------------------------
  frame = Frame(win, font=font1)
  Button(frame, text='|<', command=lambda: on_but(0))
  Button(frame, text='<<', command=lambda: on_but(1))
  Button(frame, text='<',  command=lambda: on_but(2))
  Button(frame, text='0',  command=lambda: on_but(3))
  Button(frame, text='>',  command=lambda: on_but(4))
  Button(frame, text='>>', command=lambda: on_but(5))
  Button(frame, text='>|', command=lambda: on_but(6))
  # ----------------------------------------------------------------------------
  win.label = Label(win, text=0, font=font2, border=2)  
  # ----------------------------------------------------------------------------
  # win.label = win[1] # set friendly names for all widgets used in callbacks
  win.loop()
# ------------------------------------------------------------------------------
def on_but(index:int) -> None:
  """generic callback function for all seven buttons"""
  # TODO (update counter value according to selected button given by 'index')

  # on va modifier le texte du label win.label['text'] en fonction du numéro
  # du bouton

  if index == 0:
    win.label['text']= -1000
  elif index == 1:
    win.label['text']-= 10
  elif index == 2:
    win.label['text']-= 1
  elif index == 3:
    win.label['text']= 0
  elif index == 4:
    win.label['text']+= 1
  elif index == 5:
    win.label['text']+= 10
  elif index == 6:
    win.label['text']= 1000

  # si on est sorti de l'intervalle [-1000,1000], on corrige la valeur
  if win.label['text'] < -1000:
    win.label['text'] = -1000
  elif win.label['text'] > 1000:
    win.label['text'] = 1000

  # version automatique avec le max du min
  # win.label['text'] = max(min(win.label['text'],1000),-1000)
    
  
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
