# =============================================================================
"""CSVGRID_C :  display a csv file content in a tclTk window"""
# ==============================================================================
__author__  = "Philippe Blasi"
__version__ = "1.0"
__date__    = "2023-05-02"
# ==============================================================================
from ezTK import *
from ezCLI import read_csv
# ------------------------------------------------------------------------------
def grid_tcl(name:str) -> None:
  """create the main window and pack the widgets"""
  global win
  matrix = read_csv(name) # read content of data file name
  rows, cols = len(matrix), len(matrix[0]) # get matrix size from data file
  widths = [len(str(matrix[row][col])) for col in range(cols) for row in range(rows)]
  width = max(widths) # get the maximum width for the cells of matrix
  # ----------------------------------------------------------------------------
  win = Win(title="CSV FILE", grow=False) # grid window
  # ----------------------------------------------------------------------------
  frame = Frame(win,op=3) # create a frame to store the size and name
  short_name=name.split('/')[-1] # create the local name without the full path
  Label(frame, text=f'File name : {short_name}', grow=False)
  Label(frame, text=f'Number of rows : {rows}', grow=False)
  Label(frame, text=f'Number of cols : {cols}', grow=False)
  # ----------------------------------------------------------------------------
  grid = Frame(win, fold=cols) # create a frame to store the grid
  # ----------------------------------------------------------------------------
  for row in range(rows):   # loop over matrix cells and create a Label for each 
    for col in range(cols): # value of the matrix cell in row and col
      Label(grid,text=matrix[row][col],border=1,width=width)
      #Button(grid,text=matrix[row][col],border=1, width=width )
  # ----------------------------------------------------------------------------
  win.loop()
# ==============================================================================
if __name__ == '__main__':
  grid_tcl('maze0.csv') # display the csv file content in a tclTk window
  #grid_tcl('test.csv')
# ==============================================================================
