
;---Gettr---
;------------------------------------------------
PostToGettr:
CurrentSite := "Gettr"

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

URLAttempt := NavigateFromBaseURLTo("https://gettr.com/")
if(URLAttempt = "Failed")
Return

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

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

sleep, 1000

; if string in tab url, then we are logged out
try CurrentTabURL := driver.Url
if(InStr(CurrentTabURL, "/onboarding"))
{
	Message = Login Expired. Please log back in
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

; click input area
; Xpath =//button[contains(text(),'Post')]
; Xpath = ///div[@id='textarea-post']
Xpath = //*[contains(text(),'What's new?')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
; double click, first click fails sometimes
sleep, 500
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

; Input image
if(ImageAttachmentFilepath != ""){

	; Attach image
	Xpath = //input[@type='file']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=ImageAttachmentFilepath)
	if(Status){
		Message = Failed to Attach Image
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	Message = Waiting for image to upload
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
	sleep, 3000


}


; click input area
Xpath = //*[@id="textarea-post"]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

; disable spellcheck on input field using javascript so text doesn't get garbled
js = document.getElementById('textarea-post').setAttribute("spellcheck", "false");
try driver.executeScript(js)    ;Executes a Javascript on the webpage, mostly used for buttons.

; DevModeMsgBox(JSPostTitleAndBody)
; JsToExecute = document.querySelector("#textarea-post").text = "%JSPostTitleAndBody%";
; JsToExecute = document.getElementById('textarea-post')[0].text = "%JSPostTitleAndBody%";



StrLengthOfPost := StrLen(PostTitleAndBody)

SaveOrPostProgress(Message:="Inputting post very slow way to avoid issues with gettr's buggy text post-processing",PostType:="Tooltip,ErrorLoggingTextFile")

Loop % StrLengthOfPost{
	; IndexPlusOne := A_index + 1

	Current_Char := SubStr(PostTitleAndBody, A_index, 1)
	; SubStr(String, StartingPos [, Length])
	; MsgBox % Current_Char
	; Gettr_Index_Of_Line_Post := GettrPostArray[A_index]
	; DevModeMsgBox(Gettr_Index_Of_Line_Post)

	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=1,SleepLength:=100,StringTextContent:=Current_Char)
	if(Status){
		Message = Failed to Input Text
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
	; sleep, 10
	; try driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.Right)
}
sleep, 500
; SaveOrPostProgress(Message:="Waiting 2 seconds before submitting",PostType:="Tooltip,ErrorLoggingTextFile")
; sleep, 2000


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

; Click post button
Xpath = //span[normalize-space()='Post']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=10,SleepLength:=1000)
if(Status){
	Message = Failed to Submit Post
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorSummaryVar")
	SaveDriverURLOFErrorPage()
	Return
}


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

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



PauseBetweenPosts()

DevModeMsgBox("done!")

Return


;---/Gettr---