# =============================================================================
"""MAZE : animate walker in a maze"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "2.0" # use rounded lines 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':'#000', '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 = col*step, row*step, colors[maze[row][col]]
      win.canvas.create_rectangle(x, y, x+step, y+step, fill=color, width=0)
  # ----------------------------------------------------------------------------
  x, y = walk[0]; win.xy = (x+0.5)*step, (y+0.5)*step # initial walker position
  # ----------------------------------------------------------------------------
  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"""
  step = win.step; xa, ya = win.xy # get current walker position
  width, color = max(3, step//2), '#5F5' # set width and color for walker lines
  col, row = win.walk.pop(0) # get new walker position and remove it from list
  xb, yb = (col+0.5)*step, (row+0.5)*step # convert position into canvas coords
  win.xy = xb, yb # store new walker position and show it on canvas
  win.canvas.create_line(xa, ya, xb, yb, width=width, fill=color, caps='round')
  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
# ==============================================================================
