# ==============================================================================
"""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(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))
  # ----------------------------------------------------------------------------
  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')
  # on modifie le texte de win.counter ( win.counter['text'] )
  # en fonction du bouton sur lequel on a cliqué

  if index == 0: # bouton |<
    win.counter['text'] = -1000
  elif index == 1: # bouton <<
    win.counter['text'] -= 10
  elif index == 2:
    win.counter['text'] -= 1
  elif index == 3:
    win.counter['text'] = 0
  elif index == 4:
    win.counter['text'] += 1
  elif index == 5:
    win.counter['text'] += 10
  else:
    win.counter['text'] = 1000
    
  # à la fin, on vérifie que la valeur est dans l'intervalle [-1000,1000] et
  # on corrige la valeur au besoin
  if win.counter['text'] < -1000:
    win.counter['text'] = -1000
  elif win.counter['text'] > 1000:
    win.counter['text'] = 1000

  # autre possibilité
  # win.counter['text'] = max(min(win.counter['text'],1000),-1000)
  
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
