# =============================================================================
"""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)
  # ----------------------------------------------------------------------------
  # parcours du tableau 2D maze
  
  for row in range(rows):    # boucle sur les lignes
    for col in range(cols):  # boucle sur les colonnes
       # calcul des coordonnées du rectangle et de la couleur
       xa, ya, xb, yb = step*col,step*row,step*col+step,step*row+step
       # maze[row][col] ? si c'est 'A' ou 'B' pour déterminer la couleur
       if maze[row][col] == 'A':
         color = '#000'
       else:
         color = '#FFF'
       # dessin du rectangle en noir (lettre A) ou en blanc (lettre B)
       win.canvas.create_rectangle(xa, ya, xb, yb, fill=color,width=0)

  win.walk = walk # stokage des données dans win pour que la fonction tick
  win.step = step # puisse y acceder
  
  
  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(grid(maze))
  return maze, walk
# ------------------------------------------------------------------------------
def tick() -> None:
  """update walker position on canvas"""
  # TODO (get next walker position and move walker on canvas)

  step = win.step
  col,row = win.walk.pop(0)
  xa, ya, xb, yb = step*col,step*row,step*col+step,step*row+step
  win.canvas.create_oval(xa+1, ya+1, xb-1, yb-1, fill='#0F0',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
# ==============================================================================
