# ==============================================================================
"""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_text = ('|<','<<','<','0','>','>>','>|')

  for i,text in enumerate(button_text):
    Button(frame, text=text, command=lambda x=i: on_but(x)) 
  
  #for i in range(len(button_text)):
  #  Button(frame, text=button_text[i], command=lambda x=i: on_but(x))
  
  # ----------------------------------------------------------------------------
  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')
  val = win.label['text']
  # calcul de toutes valeurs possibles après un clic sur un bouton
  all_val=(-1000,val-10,val-1,0,val+1,val+10,1000)
  # selection de la valeur en fonction du numéro du bouton
  val = all_val[index]

  # on affecte au label la valeur corrigée
  win.label['text'] = max(min(val,1000),-1000)
  
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
