# ==============================================================================
"""COUNTER : a user-controlled digital counter"""
# ==============================================================================
__author__  = "Philippe Blasi"
__version__ = "1.0" # skeleton version
__date__    = "2025-03-14"
# ==============================================================================
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, bg='#DDD', op=5)
  # ----------------------------------------------------------------------------
  # 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))
    
  # ----------------------------------------------------------------------------
  Label(win, text=0, font=font2, border=2)
  # ----------------------------------------------------------------------------
  win.counter = 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.counter['text']
  # créer un tuple avec toutes les valeurs possibles après clic sur un bouton
  all_val = (-1000,val-10,val-1,0,val+1,val+10,1000)
  # choisir la valeur en fonction de l'index du bouton
  val = all_val[index]
  # correction de la valeur en cas de sortie de l'intervalle [-1000,1000]
  win.counter['text'] = max(min(val,1000),-1000)
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
