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

pd.set_option('display.max_columns', 50)
pd.set_option('display.min_rows', 60)
pd.set_option('display.max_rows', 60)
pd.set_option('display.expand_frame_repr', False)
pd.set_option('max_colwidth', 9)

#autoreject hours that aren't same day
#pop up GUI interface for rejects
#name date section reasonWhy


def scheduledPayWin(sectionDatabaseFilename,RegHoursFilename,defaultFont):

	secD = pd.read_csv(sectionDatabaseFilename,dtype=str)

	currDate=input("Current Date: ")
	
	
	#Set all hours for sections on elapsed dates to 0 (future dates are set back to blank)
	for staffNum in range(0,10):
		secD.loc[secD['Date'].apply(HF.dateBeforeOrEqual,args=[currDate])&~secD['Staff'+str(staffNum)].isna(),'Hours'+str(staffNum)]=0
	for staffNum in range(0,10):
		secD.loc[~secD['Date'].apply(HF.dateBeforeOrEqual,args=[currDate])&~secD['Staff'+str(staffNum)].isna(),'Hours'+str(staffNum)]=''
	

	#read in ALL regularly scheduled hours responses
	forms=[]
	with open(RegHoursFilename) as f:
		headerLine=f.readline()
		reader = csv.reader(f)
		for row in reader:
			forms.append(row)
			
	warningHeader=":\n   ID       : section     : hours date : submitted on\n   --------------------------------------------------\n"
	diffDayWarning="Submitted on a different day than the hours"+warningHeader
	futureDayWarning="Submitted for a future date"+warningHeader
	missingWarning="ULA not assigned to the section"+warningHeader
	
	
	
	for form in forms:
		ID=form[2]
		section=form[3]
		date=form[4]
		vals=form[0].split(' ')[0].split('/')
		submissionDate=vals[1]+'/'+vals[2]
		
		errorString=""
		
		#TODO move these warning from terminal to GUI and allow for handling through the interface
		if not date == submissionDate:
			diffDayWarning+=f"   {ID:8} : {section:11} : {date:10} : {submissionDate}\n"
		if not HF.dateBeforeOrEqual(date,currDate):
			futureDayWarning+=f"   {ID:8} : {section:11} : {date:10} : {submissionDate}\n"
			
		
		staffIDs=secD.loc[(secD['Date']==date) & (secD['Section']==section),'Staff0':'Staff9']
		
		staffNum=staffIDs.columns[staffIDs.eq(ID).any()].values
		if len(staffNum)==0:	#corresponding section entry does not exist
			missingWarning+=f"   {ID:8} : {section:11} : {date:10} : {submissionDate}\n"
		else:
			secD.loc[(secD['Date']==date) & (secD['Section']==section),"Hours"+staffNum[0][-1]]=2
			
	sg.popup("The following hours were rejected due to the following reasons:\n",missingWarning,futureDayWarning,diffDayWarning,title="Automatically Rejected Hours",font=defaultFont,keep_on_top=True)
		
		
			
	secD.to_csv(sectionDatabaseFilename,index=False,index_label=False)