
; -------------------------------Locals-------------------------------
PostToLocals:
CurrentSite := "Locals"
; @todo: Add auto-login to locals
SaveOrPostProgress(Message:="Navigating to Post Creation Page",PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


IniRead, LocalsURL, %SettingsIniFilepath%, %ScriptSettingsSection%, LocalsURL, %A_Space%

if(LocalsURL = ""){
	Message = Please add your Locals Profile URL to the settings.ini file under`n`n[General]`nLocalsURL=
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

; /share/post is the legacy new post page.
; Starting in 2024 all posts are published through the home page now.
LocalsURL := StrReplace(LocalsURL, "/share/post")

Status := NavigateFromBaseURLTo(LocalsURL)
if(Status = "Failed")
Return

Status := CheckCurrentTabForCurrentSite()
if(Status){
	Return
}
sleep, 1000

Message = Checking Login Status

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




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

Xpath = //input[@id='title']
try driver.FindElementByXPath(Xpath).click()
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=PostTitle)
if(Status){
	Message = Failed to input Title
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}


Xpath = //*[@id="body"]
try driver.FindElementByXPath(Xpath).click()
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=PostBody)
if(Status){
	Message = Failed to input Body
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

; click the "Hide Links Preview" checkbox to hide previews of hyperlinks
if(InStr(PostBody, "https")){
	Xpath = (//div[@class='hide-switcher-container'])[1]
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=1,SleepLength:=100)

}


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


	Xpath = //input[@multiple='multiple']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=ImageAttachmentFilepath)
	if(Status){
		Message = Failed to Upload Image
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
	; driver.FindElementByXPath(Xpath).SendKeys(ImageAttachmentFilepath)
	; driver.FindElementByXPath(Xpath).SendKeys(ImageAttachmentFilepath)


	SaveOrPostProgress(Message:="Waiting 10 seconds for Image to finish uploading",PostType:="Tooltip,ErrorLoggingTextFile")

	sleep, 10000
}
Message = Submitting Post
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
if (ConfirmBeforeSubmit && ConfirmBeforeSubmitMsgboxFunc() != true) {
	Message = User Selected STOP button when asked for confirmation. Cancelling Rest of Site Upload.
    SaveOrPostProgress(Message:=Message, PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
    return
}

; Click Publish post
js = document.getElementsByName('submitPost')[0].click();  ; Send content through javascript (Great for getting around emoji chrome limitaitons)
try driver.executeScript(js)

Message = Double checking that post got submitted
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
sleep, 5000


try CurrentTabURL := driver.Url
if(InStr(CurrentTabURL, "share/post")) { ; post failed to submit if it's still on the create new post page
Message = Error: Post Was Input but Failed to Submit.
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
SaveDriverURLOFErrorPage()
Return

}


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



SaveOrPostProgress(Message:=Message,PostType:="Tooltip")
PauseBetweenPosts()
DevModeMsgBox("done!")

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



Return
; -------------------------------/Locals-------------------------------
