# ==============================================================================
"""CHRONO : a user-controlled digital stopwatch"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "0.0" # skeleton version
__date__    = "2022-11-12"
# ==============================================================================
from ezTK import *
# ------------------------------------------------------------------------------
def main():
  """create the main window and pack the widgets"""
  global win
  font1, font2 = 'Arial 16', 'Times 120 bold'
  win = Win(title='CHRONO', font=font1, op=5)
  # ----------------------------------------------------------------------------
  # TODO (use 1 Frame, 2 Buttons, 1 Label)
  # ----------------------------------------------------------------------------
  frame = Frame(win, font=font1)
  win.start_stop = Button(frame, text=('START','STOP'), command=lambda: on_start())
  Button(frame, text='RESET', command=lambda: on_reset())
  # ----------------------------------------------------------------------------
  # ajouter les 6 couleurs au label
  colors = ('#F00','#0F0','#00F','#0FF','#F0F','#FF0')
  win.chono = Label(win, text=0, width= 5,font=font2, fg=colors,border=2)
  win.loop()
# ------------------------------------------------------------------------------
def on_reset() -> None:
  """callback function for the 'RESET' button"""
  # TODO (reset counter value to 0)
  # mettre le texte du label à 0  : 
  win.chono['text']= 0
# ------------------------------------------------------------------------------
def on_start() -> None:
  """callback function for the 'START/STOP' button"""
  # TODO (switch label for START/STOP button and call 'tick' to start counter)
  # changer l'etat du bouton : passer de 0 à 1 et inversement
  win.start_stop.state = 1 - win.start_stop.state  # bascule entre 0 et 1
  # appeler la fonction tick
  tick()
# ------------------------------------------------------------------------------
def tick() -> None:
  """increment counter value and schedule next 'tick'"""
  # TODO (increment counter value and use 'win.after' for recursive call)
  # la fonction tick ne doit faire quelque chose qui si l'état du bouton est à 1
  if win.start_stop.state == 1:
    # ajouter 1 au label  ==>   win.chono['text']
    win.chono['text'] += 1

    # attention, toute les 50 valeurs de win.chono['text'], il faut changer de couleur
    # pour le label, c'est à dire changer l'état du label
    if win.chono['text'] % 50 == 0:
      win.chono.state += 1
    
    # rappeler la fonction tick après x ms avec la fonction win.after(x,tick)
    win.after(10,tick)
  
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
