;---------------Facebook----------------------------------
PostToFacebook:
CurrentSite := "Facebook"

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

Status := NavigateFromBaseURLTo("https://www.facebook.com/")
if(Status){
	Message = Failed to Navigate to Feed Page
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
}

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

sleep, 2000
js = document.querySelector("div[class='m9osqain a5q79mjw gy2v8mqq jm1wdb64 k4urcfbm qv66sw1b'] span[class='a8c37x1j ni8dbmo4 stjgntxs l9j0dhe7']").click()

; js = document.querySelector('a[aria-label$="Timeline"] + div > div:nth-child(1)').click()
try driver.executeScript(js)

IniWrite, Incomplete, %StatusFileFilePath%, Status, Facebook
try Clipboard := PostTitleAndBody
SaveDriverURLOFErrorPage()

Return







; try html := driver.pagesource ; save page's entire HTML to a variable
; Clipboard := html
; Msgbox % "html: " html

; Pull out greeting message
/*GreetingMessage := StrSplit(HTML, "What's on your mind,")
GreetingMessage := GreetingMessage[2]
GreetingMessage := StrSplit(GreetingMessage, "</span>")
GreetingMessage := GreetingMessage[1]
GreetingMessage := "What's on your mind," . GreetingMessage
*/
; Msgbox % "GreetingMessage: " GreetingMessage

; //span[normalize-space()="What's on your mind, Yuriy?"]
; 

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

; Msgbox % "Status: " Status
if(ImageAttachmentFilepath = "" AND Status){ ; upload a temporary file and then remove it and input psot
	Message = Inputting post using temporary image attachment workaround
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")


	Xpath = //div[@aria-label='Create a post']//div//input[@type='file']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=TemporaryImageFilepath)
	if(Status){
		Message = Post Failed: Check Login Status
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}
	; driver.FindElementByXPath(Xpath).SendKeys(TemporaryImageFilepath)

	sleep, 2000
	; remove attachment
	Xpath = //div[@aria-label='Remove post attachment']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=3000)
	if(Status){
		Message = Upload Failed: Failed to remove temporary image.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

	; driver.FindElementByXPath(Xpath).click()


	Xpath = //span[normalize-space()="What's on your mind, Stefan?"]
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
	if(Status){
		Message = Failed to click with Relative Xpath
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

		; click on "what's on your mind text box"
		Xpath = //div[@aria-label='Create a post']//div[1]//div[1]//div[1]//span[1]
		Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
		if(Status){
			Message = Failed to click "What's on your mind" box to input post with relative and direct xpath
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
			Return
		}
	}
	
}
if(ImageAttachmentFilepath != "") { ; otherwise send image straight to upload input box. The post box will open up automatically
	Message = Attaching Image
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")


	Xpath = //div[@aria-label='Create a post']//div//input[@type='file']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=ImageAttachmentFilepath)
	if(Status){
		Message = Post Failed: Check Login Status
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}
/*	; Attach image button
	Xpath = //div[@aria-label='Photo/Video']//div//div//div//div//div//i[@data-visualcompletion='css-img']
	Status := Selenium_LoopToClickXpathAndWaitForOpenWindow(Xpath:=Xpath,NumOfLoops:=10,SleepLength:=1000)
	if(Status){
		Message = Failed to click "Attach Image" button
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

	; Input filepath of image/video
	Status := InputFilePathIntoOpenWindow(ImageAttachmentFilepath)
	if(Status)
	{
		Message = Upload Failed: Unable to Find "Open File" window to input Video filepath into
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

	*/

	Message = Waiting 5 Seconds For Thumbnail Preview to Load
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip")
	; Wait for thumbnail to load
	sleep, 5000

}

Message = Posting Exited Early for Testing
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
Return

; Xpath = //div[@data-block='true']//div
Xpath = //div[@data-block='true']//div
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=PostTitleAndBody)
if(Status){
	Message = Failed to input Text Post message with Relative and direct Xpath
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}



IniWrite, Input - NOT Submitted, %StatusFileFilePath%, Status, Facebook
Message = Post Button Click Skipped. Please Double Check and Submit Post Manually
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar")
SaveDriverURLOFErrorPage()
Return



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


; Click Post Button
xpath = //div[@aria-label='Post']//div//div//div//span[contains(text(),'Post')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to click "Post" button
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

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

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


PauseBetweenPosts()
DevModeMsgBox("done!")

Return
;---------------/Facebook----------------------------------
