
; -------------------------------LinkedIn-------------------------------
PostToLinkedIN:
CurrentSite := "LinkedIn"

if(StrLen(PostTitleAndBody) > 1300){
	Message = Post longer than LinkedIn's 1300 character limit. Posting Skipped.
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

	IniWrite, Skipped - Too Long, %StatusFileFilePath%, Status, LinkedIn
	Return
}

SaveOrPostProgress(Message:="Navigating to Post Creation Page",PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")



URLAttempt := NavigateFromBaseURLTo("https://www.linkedin.com/login")
if(URLAttempt = "Failed")
Return

Status := CheckCurrentTabForCurrentSite()
if(Status){
	Return
}

Message = Checking Login Status
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

sleep, 1000
try CurrentTabURL := driver.Url
if(InStr(CurrentTabURL, "login")){
	Message = Login Expired. Please log back in
	; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}


Message = Clicking "Start a Post" button
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")


loop, 3 {
	Xpath = (//span[normalize-space()='Start a post'])[1] ; start a post button
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
	if(!Status)
	Break
	; Msgbox % "Status: " Status
	if(Status AND A_index = 1){
		try CurrentURL := driver.url
		Message = Failed to click on "Start a post" button`nChecking Login Status`nCurrent URL: %CurrentURL%
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
		try html := driver.pagesource ; save page's entire HTML to a variable
		if(InStr(HTML, "Sign In") AND InStr(HTML, "Forgot password")){
			Xpath = //button[normalize-space()='Sign in']
			Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
			driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
			sleep, 2000
			driver.Get("https://www.linkedin.com/feed/")	;Open selected URL
			; driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
			continue
		}
		else, ; otherwise check and toggle the chat popup on the side
		{
			Message = Checking if Chat Sidebar is in the way
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip")
			try html := driver.pagesource ; save page's entire HTML to a variable
			if(InStr(HTML,"You are on the messaging overlay. Press enter to minimize it.")){ ; if sidebar is open
				Xpath = //body//div//aside//button[2] ; click to minimize it
				try driver.FindElementByXPath(Xpath).click()
				Message = Chat sidebar was minimized
				SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
				Continue ; continue to next loop
			}
		}
	}
	; DevModeMsgBox("test")
	; Check if linked in post popup appeared
	Xpath = //div[@aria-label='Text editor for creating content']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
	if(Status){
		Message = Failed to click into Create Post popup to input post
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	}
}

if(Status){
	Message = Login Expired. Please log back in
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

if(ImageAttachmentFilepath != ""){
	; Click the upload image button
	SaveOrPostProgress(Message:="Attaching Image",PostType:="Tooltip,ErrorLoggingTextFile")

	; click attach image button
	Xpath = //button[@aria-label='Add media']
	Status := Selenium_LoopToClickXpathAndWaitForOpenWindow(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
	if(Status){
		Message = Failed to click "Attach Image" button with Selenium
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

		Xpath = //input[@id='image-sharing-detour-container__file-input']
		Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=ImageAttachmentFilepath)
		if(Status){
			Message = Failed to Attach photo with both attempts.
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}

		if(WinExist("Open")) ; if open image popup still exists, close it
		WinClose, Open
		; Return
	}
	else, {
		if(WinExist("Open")){
			Message = Closing "Open" Popup Window so Image can be send directly to input element
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

			WinClose, Open
		} 

		Xpath = //input[@id='media-editor-file-selector__file-input']
		Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=ImageAttachmentFilepath)
		if(Status){
			Message = Post Failed: Failed to Upload Image by sending directly to element.
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}

		Xpath = //span[normalize-space()='Next']
		Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
		if(Status){
			Message = Post Failed. Image was attached successfully but failed to navigate back to New Post Page
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}


		; driver.FindElementByXPath(Xpath).SendKeys(ImageAttachmentFilepath)

/*		Status := InputFilePathIntoOpenWindow(ImageAttachmentFilepath)
		if(Status)
		{
			Message = Failed to input filepath into "Open File" window.
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}
		*/
	}
	; sleep, 10000

/*	Xpath = //span[normalize-space()='Done'] ; click done button
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=10,SleepLength:=1000)
	if(Status){
		Message = Failed to click "Done" button on image upload
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	SaveOrPostProgress(Message:="Waiting 5 seconds for Image to finish uploading",PostType:="Tooltip,ErrorLoggingTextFile")
	sleep, 5000
*/

}

Message = Inputting Post
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

; Input Post text contents
loop, 3 {
	Xpath = //div[@aria-label='Text editor for creating content']  ; Input text of post 
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000,StringTextContent:=PostTitleAndBody)
	if(Status){
		Message = Failed to input post content
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	}

	; Double check that post got input
	Jscheck = return document.querySelector("div[aria-label='Text editor for creating content']").innerText
	try Status := driver.executeScript(Jscheck)
	; DevModeMsgBox(Status)
	if(StrLen(Status) > 5)
	Break

	if(A_index = 3){
		Message = Post Failed: Failed to input Post for some reason
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
	; Continue
}

/*	if(!Status){ ; if BLANK 
		Message = Post Failed: Failed to input Post for some reason
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
	*/
	; sleep, 2000

	if(InStr(PostTitleAndBody, "http") OR InStr(PostTitleAndBody, "www")){
		Message = Waiting 10 seconds for Preview to Get Fetched
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
		sleep, 10000 
	}
	else, {
		sleep, 2000
	}


	SaveOrPostProgress(Message:="Submitting Post",PostType:="Tooltip,ErrorLoggingTextFile")


	Xpath = //span[normalize-space()='Post'] ; Click the final "post" button
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
	if(Status){
		Jscheck = return document.querySelector("div[aria-label='Text editor for creating content']").textContent ; Grabb innertext
		try TextContent := driver.executeScript(Jscheck)

		Message = Failed to Submit Post
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")

		Message = Failed to Submit Post: Text Content that got input: %TextContent%
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

/*	; Keep trying to click the home button until able to and then move on to next site because sometimes it takes a while for a post to upload
	Xpath = //span[normalize-space()='Home']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=30,SleepLength:=1000)

	*/

	IniWrite, Successful, %StatusFileFilePath%, Status, LinkedIn
	AddToTotalPostsPostedCount()

	; DevModeMsgBox("done!")
	
	PauseBetweenPosts()
	DevModeMsgBox("done!")

	Message = Post Publish Successful
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


	Return
	; -------------------------------/LinkedIn-------------------------------
