# ==============================================================================
"""LEAP FROG : implement the LeapFrog puzzle"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0" # skeleton version (for mouse or keyboard interaction)
__date__    = "2022-11-12"
# ------------------------------------------------------------------------------
from ezTK import *
# ------------------------------------------------------------------------------
def main(frog=3):
  """create the main window and pack the widgets"""
  global win
  win = Win(title='LEAP FROG', op=3, click=on_click) # mouse-based version
  #win = Win(title='LEAP FROG', op=3, key=on_key) # keyboard-based version
  # ----------------------------------------------------------------------------
  # TODO (use 1 Frame, (2*frog+1) Bricks, 1 Button)
  # ----------------------------------------------------------------------------
  images = (Image('frog0.gif'),Image('frog1.gif'),Image('frog2.gif')) # load 3 frogs images
  frame = Frame(win,op=2)
  for i in range(2*frog+1):
    Brick(frame, image=images,border=2)
  Button(win,text='RESET',command=on_reset)
  # ----------------------------------------------------------------------------
  win.frog = frog # sauvegarde du nombre de grenouilles
  win.frame = win[0]  # pour retrouver les grenouilles facilement
  on_reset(); win.loop() # set initial configuration and start event loop
# ------------------------------------------------------------------------------
def on_reset() -> None:
  """callback function for the 'RESET' button"""
  # TODO (set initial position for all frogs on board)

  # win.frame[i] ==> grenouille i
  # modifier les états de briques pour afficher la bonne couleur de grenouille
  # on met les premières grenouilles en bleu, etat 1
  for i in range(win.frog):
    win.frame[i].state = 1

  win.frame[win.frog].state = 0  # grenouille du milieu à mettre en blanc, etet 0

  # on met les dernières grenouilles en vert, etat 2
  for i in range(win.frog+1,2*win.frog+1):
    win.frame[i].state = 2
  
# ------------------------------------------------------------------------------
def on_click(widget:object, code:str, mods:str) -> None:
  """callback function for all mouse click events"""
  # print(widget, widget.master, widget.index, code, mods)
  # TODO (check if mouse click is on a frog, then call 'move' for that frog)
  if widget.master != win.frame or widget.index is None:
    return # nothing to do (mouse click is not on a grid cell)
  move(widget.index)
# ------------------------------------------------------------------------------
#def on_key(widget, code, mods):
  #"""callback function for all keyboard events"""
  #print(widget, code, mods)
  # TODO (check if pressed key is valid, then call 'move' for that key)
# ------------------------------------------------------------------------------
def move(n:int) -> None:
  """move frog located at index 'n', after checking if its move is valid"""
  # TODO (check if move is valid, then update corresponding frog position)
  # 1 grenouille bleue ? <==> etat 1
  #   chercher la case blanche (etat 0) en position n+1 ou n+2
  #   si elle existe, déplacer la grenouille
  # 2 grenouille verte ? <==> etat 2
  #   chercher la case blanche (etat 0) en position n-1 ou n-2
  #   si elle existe, déplacer la grenouille
  # ATTENTION A NE PAS SORTIR DES BORNES DU TABLEAU win.frame
# ==============================================================================
if __name__ == '__main__':
  main() # create window with default game parameters: frog=3
  #main(7) # try with 7 frogs per color
# ==============================================================================
