import PySimpleGUI as sg
import csv
import pandas as pd
import helperFunctions as HF

def secViewWin(defaultFont):

	#Printing options for the Pandas dataframe
	pd.set_option('display.min_rows', 20)
	pd.set_option('display.expand_frame_repr', False)
	pd.set_option('max_colwidth', 30)

	#Pull all section titles for the poopulation of the drop-down box
	sections=HF.getAllSectionTitles()

	#Define the window's layout
	layout=[[sg.Combo(sections,size=(7,1),key="-SECTION-",readonly=True, enable_events=True)],
		[sg.Multiline("",size=(160,40),key="-OUTPUT-")],
		[sg.Button("Close")]]

	# Create the Window
	window = sg.Window('Section Viewer', layout, font=defaultFont,resizable=True)

	#Event Loop to process "events" and get the "values" of the inputs
	#Cease running if they close the window
	event=-1
	while event != sg.WIN_CLOSED and event != "Close":
		event, values = window.read()
		
		#If the 'section' box is interacted with and is not blank (i.e. an option was selected from the dropdown box)
		if event=="-SECTION-" and values['-SECTION-']!="":
			#Clear the box
			window["-OUTPUT-"].Update('')
			
			#get the data
			sec=values['-SECTION-']
			output,output2=HF.getAllDataForSection(sec)
			output=output.values.tolist()
			output2=output2.head(1).values.tolist()[0]
			
			#Print the normal schedule
			window["-OUTPUT-"].print("Normally Scheduled: "+str(output2[2])+" @ "+str(output2[3]),text_color='black')
			normalNames=[]
			for ID in output2[4:]:
				normalNames.append(f'{HF.IDToName(ID)[:15]:15}')
			window["-OUTPUT-"].print('1  '+output2[1]+' '+', '.join(normalNames),text_color='blue')
			
			#Print a linebreak
			window["-OUTPUT-"].print("-"*160,text_color='black')
			
			#print the schedule for each week
			window["-OUTPUT-"].print("Currently Scheduled:",text_color='black')
			for line in output:
				names=[]
				for ID in line[4:]:
					names.append(f'{HF.IDToName(ID)[:15]:15}')
				color='blue'
				if names!=normalNames:
					color='red'
				window["-OUTPUT-"].print(f'{line[0]:2} '+line[1]+' '+', '.join(names),text_color=color)
			
		
		
		
	window.close()
