# =============================================================================
"""CSVGRID_E :  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 main():
  """create the main window and pack the widgets"""
  global win
  win = Win(title='CSV FILE', grow=False)
  Button(win, text='OPEN FILE', width=12, command=on_open)
  # ----------------------------------------------------------------------------
  Frame(win) # create an empty frame to store the size and name of the file
  Frame(win) # create an empty frame to store the grid
  # ----------------------------------------------------------------------------
  win.loop()
# ------------------------------------------------------------------------------
def on_open() -> None:
  """callback for the "OPEN FILE" button"""
  name = Dialog('open',title='OPEN FILE')
  grid_tcl(name)
# ------------------------------------------------------------------------------
def grid_tcl(name:str) -> None:
  """create the grid within the main window and pack the widgets"""
  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)+2 # get the maximum width for the cells of matrix
  # ----------------------------------------------------------------------------
  del win[2] # delete the frame containing the current grid
  del win[1] # delete the frame storing the size and name of the file
  # ----------------------------------------------------------------------------
  frame = Frame(win,op=3) # create a new 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 new frame to store the new 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)
  # ----------------------------------------------------------------------------
# ==============================================================================
if __name__ == '__main__':
  main()
# ==============================================================================
