# =============================================================================
"""MAZE : animate walker in a maze"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "0.0" # skeleton version
__date__    = "2022-11-12"
# ==============================================================================
from ezTK import *
from ezCLI import read_csv # helper function to parse CSV files
from ezCLI import grid 
# ------------------------------------------------------------------------------
def main(index:int=0):
  """create the main window and pack the widgets"""
  global win
  maze, walk = read_files(index) # read content of data files
  rows, cols = len(maze), len(maze[0]) # set maze size (should be computed from variable 'maze')
  # ----------------------------------------------------------------------------
  win = Win(title=f"MAZE : {cols}x{rows}", op=4, grow=False) # put size on title

  width, height = win.winfo_screenwidth()-100, win.winfo_screenheight()-100
  step = min(width/cols, height/rows) # compute step according to screen size
  
  win.canvas = Canvas(win, width=step*cols, height=step*rows)
  # ----------------------------------------------------------------------------
  # TODO (draw maze on Canvas using 'create_rectangle' for each cell)
  # ----------------------------------------------------------------------------
  for row in range(rows):       # boucle sur les lignes
      for col in range(cols):   # boucle sur les colonnes
          # calculer les coordonnées xa,ya,xb,yb
          xa, ya, xb, yb = col*step,row*step,col*step+step,row*step+step
          # choisir la couleur en fonction de la lettre contenue dans la case
          # de la matrice maze
          if maze[row][col] == 'A':
            color = '#000'
          else:
            color = '#FFF'
          # dessiner le rectangle
          win.canvas.create_rectangle(xa, ya, xb, yb, fill=color, width=0)


  # de la même facon, afficher en vers le trajet contenu dans walk
  # chaque case de walk contient l'absisse et l'ordonnée dans la matrice
  # print(walk)
    
  win.walk = walk  # permet à la fonction tick d'accèder à walk
  win.step = step
          
  win.after(500, tick); win.loop()
# ------------------------------------------------------------------------------
def read_files(index:int) -> (list,list):
  """return content of data files (maze file + walk file) for given index"""
  # TODO (read maze from "mazeN.csv" and walk from 'walkN.csv', where N=index)
  mazefile, walkfile = f'maze{index}.csv', f'walk{index}.csv'
  maze = read_csv(mazefile) # 'maze' must be a list of lists of chars (one char per maze cell)
  walk = read_csv(walkfile) # 'walk' must be a list of integer pairs (two coords per walk step)

  #print(maze)
  #print(grid(maze))
  
  return maze, walk
# ------------------------------------------------------------------------------
def tick() -> None:
  """update walker position on canvas"""
  # TODO (get next walker position and move walker on canvas)

  col,row = win.walk.pop(0) # renvoie la case 0 et l'enlève de la liste
  xa, ya, xb, yb = col*win.step,row*win.step,col*win.step+win.step,row*win.step+win.step
  win.canvas.create_oval(xa, ya, xb, yb, fill='#5F5', width=0)

  if len(win.walk) != 0:
    win.after(50, tick)

# ==============================================================================
if __name__ == '__main__':
  main(1) # show maze and animate walker for the given maze index
# ==============================================================================
