
; -------------------------------Twetch-------------------------------
TwetchAttemptNumber := 1
PostToTwetch:
CurrentSite := "Twetch"

; if(StrLen(PostTitleAndBody) > 256){
	; 	Message = Post Skipped as it's longer than 256 Characters
	; 	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
	; 	IniWrite, Skipped - Too Long, %StatusFileFilePath%, Status, Twetch
	; 	Return
	; }


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

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

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

	sleep, 1000

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

	try CurrentTabURL := driver.Url
	if(InStr(CurrentTabURL, "/welcome")){
		Message = Login Cookies Expired. Trying to Re-Login
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

		sleep, 2000 ; 2 seconds for password to get loaded in by chrome

		Xpath = //button[@type='submit'][normalize-space()='Sign in']
		Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
		if(Status){
			SaveOrPostProgress(Message:="Login Expired. Please log back in",PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}

		Message = Waiting for Page to finish loading
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
		sleep, 2000
		driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
	}



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

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

	; Message =


	; Click on "Create Post"
	SaveOrPostProgress(Message:="Inputting Post Content",PostType:="Tooltip,ErrorLoggingTextFile")

/*Xpath = //textarea[@id='downshift-post-editor-default-input']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
if(Status){
	SaveOrPostProgress(Message:="Post Failed: Check Login Status",PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}
*/

; Click into text input box at top of page
Xpath = //div[@role='textbox']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)



; Input Post contents
Xpath = //div[@role='textbox']
status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=5000,SendKeysContent:=PostTitleAndBody)
if(Status){
	Message =  Failed to Input Post
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

; DevModeMsgBox("input?")

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

	Xpath = //input[@id='rich-file-input']
	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
	}

	SaveOrPostProgress(Message:="Waiting 5 Seconds for Image to Upload ",PostType:="Tooltip,ErrorLoggingTextFile")
	sleep, 5000
}

/*; Get User confirmation if they have the setting checked
if(ConfirmBeforeSubmit){
	Status := ConfirmBeforeSubmitMsgboxFunc()
	if(Status){
		Message := Status
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}
}

*/
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 on "Post" button
sleep, 2000

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

Xpath = //button[@id='composer-post-btn']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=4,SleepLength:=1000)
if(Status){
	Message = Failed to click on Submit Post Button
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

sleep, 1000 

Xpath = //p[normalize-space()='Insufficient wallet balance']
try, FundsStatus := driver.findelementbyxpath(Xpath).Attribute("innerText") ; Grabb innertext
if(FundsStatus){
	Message = Insufficient wallet balance to post
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	IniWrite, Insufficient wallet balance to post, %StatusFileFilePath%, Status, Twetch
	Return
}


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



PauseBetweenPosts()
DevModeMsgBox("done!")

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


Return
; -------------------------------/Twetch-------------------------------
