# =============================================================================
"""MAZE : animate walker in a maze"""
# ==============================================================================
__author__  = "Philippe Blasi"
__version__ = "1.0" # skeleton version
__date__    = "2026-04-24"
# ==============================================================================
from ezTK import *
from ezCLI import read_csv # helper function to parse CSV files
# ------------------------------------------------------------------------------
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()-64, win.winfo_screenheight()-64

  step = 32 # set step size (should be computed from maze size and screen size)
 
  win.canvas = Canvas(win, width=step*cols, height=step*rows)
  # ----------------------------------------------------------------------------
  # TODO (draw maze on Canvas using 'create_rectangle' for each cell)
  # ----------------------------------------------------------------------------
  # boucle sur les lignes
  #    boucle sur les colonnes
  #       calcule à partir de row et col et step les coordonnées du coin superieur
  #       gauche du carré
  #       choisit la couleur en fonction de la lettre de la case maze[row][col], noir pour A, blanc pour B
  #       dessiner le carré avec win.canvas.create_rectangle
  for row in range(rows):
    for col in range(cols):
      xa = col*step
      ya = row*step
      xb = xa+step
      yb = ya+step
      if maze[row][col] == 'A':
        color = "#000"
      else:
        color = "#FFF"
      win.canvas.create_rectangle(xa,ya,xb,yb, fill=color, outline='black', width=0)
  
  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)
  # read_csv permet de lire le fichier
  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)
  return maze, walk
# ------------------------------------------------------------------------------
def tick() -> None:
  """update walker position on canvas"""
  # TODO (get next walker position and move walker on canvas)
# ==============================================================================
if __name__ == '__main__':
  main(1) # show maze and animate walker for the given maze index
# ==============================================================================
