# import csv

# WIDTH = 150
# BORDER = "#"

def short_word(word, length):
    if len(word) > length:
        return "".join(["." if x == length - 2 else word[-1] if x == length-1 else word[x] for x in range(length)])
    else:
        return word

def the_header(width, border, left = "", middle = "", right = ""):
    """
    Prints header with desired text and length in correct format.

    Args:
        width (int):  Width of header
        border (str): What border is made of
        left (str):   Left text in header
        middle (str): Center title in header
        right (str):  Right text in header

    Returns:
        None
    """
    
    # Header made out of desired str and with desired length, with height of 3
    header = [border * width for x in range(3)]
    # Length of word that fits into header 
    length = (width - 4) // 3

    left = short_word(left, length)
    middle = short_word(middle, length)
    # If header is small, only command is displayed
    if len(right) >= length:
        right = right[:3]
    
    # Starting point of words
    start_first_print = 2
    start_middle_print = width // 2 - len(middle) // 2
    start_right_print = width - 2 - len(right)

    new_header = ""
    # Loops through each char in header
    for idx, char in enumerate(header[1]):
        # Skips first and last char (Keeping the border)
        if idx > 0 and idx < width - 1:
            # Prints out left word on correct place
            if idx >= start_first_print and idx <= len(left) + start_first_print - 1:
                new_header += left[idx - start_first_print]
                continue
            # Prints out middle word in the center
            if idx >= start_middle_print and idx <= len(middle) + start_middle_print - 1:
                new_header += middle[idx - start_middle_print]
                continue
            # Prints out right word on correct place
            if idx >= start_right_print and idx <= len(right) + start_right_print - 1:
                new_header += right[idx - start_right_print]
                continue
            new_header += " "
        else:
            new_header += char

    # Puts in new header
    header[1] = new_header
    for line in header:
        print(line)

def list_window(width, border, header_row, data_list):
    rows = len(data_list)    
    # Add idx number to start of each data row
    new_data_list = []
    for idx in range(rows):
        temp = ["|{}|".format(idx+1)]
        if idx < len(data_list):
            for i in range(len(header_row)):
                temp.append(data_list[idx][i])
        else:
            for i in range(len(header_row)):
                temp.append("")
        new_data_list.append(temp)
    data_list = new_data_list

        
    # Add select to start of header row
    select = "Select"
    header_row = [select] + header_row

    # Finds max length of data
    data_max_list = [len(x) for x in data_list[0]]
    for line in data_list[1:]:
        for idx, item in enumerate(line):
            if len(item) > data_max_list[idx]:
                data_max_list[idx] = len(item)


    # Calculations for correct window width
    header_max_list = [len(x) for x in header_row]
    length = sum(data_max_list) + 3 * (len(data_max_list) - 1) + 4
    header_length = sum(header_max_list) + 3 * (len(header_max_list) - 1) + 4
    
    while length < width and length < header_length:
        idx_to_add = 0
        max_change = 0
        for idx, item in enumerate(data_max_list):
            if header_max_list[idx] - data_max_list[idx] > max_change:
                idx_to_add = idx
                max_change = header_max_list[idx] - data_max_list[idx]
                
        data_max_list[idx_to_add] += 2
        length = sum(data_max_list) + 3 * (len(data_max_list) - 1) + 4
    
    x = 1
    while length < width:
        idx_to_add = x % len(data_max_list) - 1
        data_max_list[idx_to_add] += 2
        x += 1
        length = sum(data_max_list) + 3 * (len(data_max_list) - 1) + 4
    
    # Shorten header row
    header_row = [short_word(x, data_max_list[idx]) for idx, x in enumerate(header_row)]        

    the_list = [header_row if idx == 0 else data_list[idx-1] for idx, x in enumerate(range(len(data_list) + 1))]
    new_list = []
    
    for line in the_list:
        temp_list = []
        for idx, x in enumerate(line):
            temp_list.append("{:{align}{width}}".format(x, align="^", width=data_max_list[idx]))
        new_list.append(border + " " + " | ".join(temp_list) + " " + border)
        new_list.append(border + "-" * (length-2) + border)
    for line in new_list:
        print(line)



# a_list = []
# with open("UPDATEDSTUDENTDATA\\AircraftType.csv", newline="") as csvfile:
#     reader = csv.reader(csvfile, delimiter=" ", quotechar="|")
#     for row in reader:
#         row = [x.split(",") for x in row]
#         for item in row:
#             a_list.append(item)

# #the_header(WIDTH, BORDER, "NaN","Main Menu", "|Q| Quit")
# header = a_list[0]
# a_list = a_list[1:]
# list_window(WIDTH, BORDER, header, a_list)
