import HRInitialaizationModule
import xlwt
from xlutils.copy import copy
from openpyxl import load_workbook
from datetime import datetime

def processVacationRequest(employeeID, daysRequested): 
    sheetDescriptor = HRInitialaizationModule.sheetDescriptor
    wb = copy(HRInitialaizationModule.rb)
    w_sheet = wb.get_sheet(0)
    employeeRowLocation = -1
    for i in range(sheetDescriptor.nrows):
        if (sheetDescriptor.cell_value(i, HRInitialaizationModule.employeeIDColLocation)) == employeeID:
            employeeRowLocation = i
            print("FOUND EMPLOYEE, value is " + str(employeeRowLocation))
    if (employeeRowLocation == -1):    
        print ("Could not find employee")
        return employeeRowLocation
    convertedID = str(sheetDescriptor.cell_value(
        employeeRowLocation, HRInitialaizationModule.employeeIDColLocation))
    convertedName = str(sheetDescriptor.cell_value(
        employeeRowLocation, HRInitialaizationModule.nameColLocation))
    splitNameString = convertedName.split()
    convertedPosition = str(sheetDescriptor.cell_value(
        employeeRowLocation, HRInitialaizationModule.positionColLocation))
    convertedMonthsInService = str(int(sheetDescriptor.cell_value(
        employeeRowLocation, HRInitialaizationModule.monthsinServiceColLocation)))
    convertedVacationsAvailable = int(sheetDescriptor.cell_value(
        employeeRowLocation, HRInitialaizationModule.vacationsAvailableColLocation))

    confirmEmployee = input("please confirm that employee is " + convertedName + "? \n (please type Yes or No)")
    if (confirmEmployee == "yes" or confirmEmployee == "Yes" or confirmEmployee == "YES" or confirmEmployee == "Y" or confirmEmployee == "y"):
         
        modifiedVacationAvailable = convertedVacationsAvailable - daysRequested
        if (modifiedVacationAvailable < 0):
            print("Employee does not have enough vacation balance")
            return -1
        print ("MODIFIED IS " + str(modifiedVacationAvailable))
        w_sheet.write(employeeRowLocation, HRInitialaizationModule.vacationsAvailableColLocation, modifiedVacationAvailable)
        wb.save("../resources/" + HRInitialaizationModule.fileName + ".xlsx")
        
        salaryLetter = HRInitialaizationModule.giveMeSpaceToMidH + " WAHAJ LTD. (RIYADH) " + HRInitialaizationModule.giveMeSpaceToMidV + \
             "Dear HR Manager, " + HRInitialaizationModule.newLine + HRInitialaizationModule.newLine \
                + "This letter is to confirm that Mr. " + convertedName + " has requested and granted a vacation for " \
                + str(daysRequested) + " days." + HRInitialaizationModule.newLine + "Mr. " + str(splitNameString[1]) + \
                " had enough vacation's balance to take the vacation. His annual vacation balance before approving the vacation was " \
                + str(convertedVacationsAvailable) + " days, and after approval was granted, the balance is now reduced to " \
                + str(modifiedVacationAvailable) + " days. The request was submitted to us on " + str(datetime.date(datetime.now())) + \
                "." + HRInitialaizationModule.newLine + HRInitialaizationModule.giveMeSpaceToMidV + \
                "Please keep this for your reference." + HRInitialaizationModule.newLine + HRInitialaizationModule.newLine + \
                "Thank you " + HRInitialaizationModule.newLine + "Human Resources team" + HRInitialaizationModule.newLine 

        f = open("../AutoGeneratedLetters/Vacation Letters/" + convertedName + "_Approved_Vacation.txt", "w")
        f.write(salaryLetter)
        f.close()
        print ("Letter genarated Successfully")
        
        def WriteToSheet():
            vacationsSheet= "../resources/VacationsSheet.xlsx"
            wb = load_workbook(filename = vacationsSheet)
            ws = wb['Sheet1']
            newRowLocation = ws.max_row +1

            #write to the cell you want, specifying row and column, and value :-)
            ws.cell(column = 1,row = newRowLocation, value = employeeID)
            ws.cell(column = 2,row = newRowLocation, value = convertedName)
            ws.cell(column = 3,row = newRowLocation, value = daysRequested)
            ws.cell(column = 4,row = newRowLocation, value = datetime.date(datetime.now()))          
            wb.save(filename=vacationsSheet)
            wb.close()
            print ("SUCEESS INSERTING")
            return 1

        writingStatus = WriteToSheet()
        return

    return -1