# =============================================================================
"""MAZE : animate walker in a maze"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0" # use rectangles for walker animation
__date__    = "2022-11-12"
# ==============================================================================
from ezTK import *
from ezCLI import read_csv
# ------------------------------------------------------------------------------
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 for given index
  rows, cols = len(maze), len(maze[0]) # get maze size from data file
  # ----------------------------------------------------------------------------
  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 = min(width/cols, height/rows) # compute step according to screen size
  win.canvas = Canvas(win, width=cols*step, height=rows*step)
  # ----------------------------------------------------------------------------
  colors = {'A':'#555', 'B':'#FFF'} # define one color for each maze symbol
  for row in range(rows):   # loop over maze cells and draw one square per cell,
    for col in range(cols): # selecting color according to maze symbol (A or B)
      x, y, color = step*col, step*row, colors[maze[row][col]]
      win.canvas.create_rectangle(x, y, x+step, y+step, fill=color)
  # ----------------------------------------------------------------------------
  win.walk, win.step = walk, step; win.after(1000, tick); win.loop()
# ------------------------------------------------------------------------------
def read_files(index:int) -> (list,list):
  """return content of data files (maze file + walk file) for provided index"""
  mazefile, walkfile = f"maze{index}.csv", f"walk{index}.csv" # built filenames
  maze = read_csv(mazefile) # read maze file as a list of char lists
  walk = read_csv(walkfile) # read walk file as a list of integer pairs
  return maze, walk
# ------------------------------------------------------------------------------
def tick() -> None:
  """update walker position on canvas"""
  col, row = win.walk.pop(0) # get new walker position and remove it from list
  x, y = win.step*col, win.step*row # convert walker position to canvas coords
  # show new walker position by drawing a green (= #5F5) square on the canvas
  win.canvas.create_rectangle(x, y, x+win.step, y+win.step, fill='#5F5')
  if win.walk != []: win.after(50, tick) # schedule next step, if list not empty
# ==============================================================================
if __name__ == '__main__':
  main(0) # show maze grid and animate walker for the provided maze index
# ==============================================================================
