
; -------------------------------Minds-------------------------------
PostToMinds:
CurrentSite := "Minds"


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


URLAttempt := NavigateFromBaseURLTo("https://www.minds.com/newsfeed/subscriptions")
if(URLAttempt = "Failed")
Return

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

sleep, 1000

try CurrentTabURL := driver.Url
; Msgbox % "CurrentTabURL: " CurrentTabURL
if(InStr(CurrentTabURL, "login")){
	Message = Login Cookies Expired. Trying to Re-Login
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
	sleep, 2000
	Xpath = //div[contains(text(),'Login')]
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
	driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
}

Message = Clicking into Speak your mind.. Box
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")


loop, 3 { 
	if(A_index = 3){
		SaveOrPostProgress(Message:="Failed to click into -speak your mind..- input box.",PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}


	; Click the Big Compose Box at the top of the page
	Xpath = //div[@class='m-composer__triggerOverlay']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=10,SleepLength:=1000)
	; Status := Selenium_LoopToClickXpath(Xpath,5,1000)
	if(Status){
		SaveOrPostProgress(Message:="Login Expired. Please log back in",PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	; check that popup appears by checking for "Create Blog" button in popup
	js = return document.querySelector("div[class='m-composer__base'] div[class='m-composerTitleBar__createBlogTrigger ng-star-inserted']").innerText
	try status := driver.executeScript(js)
	if(InStr(Status, "Create Blog")){
		break
	}
	else, {
		continue
	}

	; Msgbox % "status: " status



}

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


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

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

	sleep, 2000


	SaveOrPostProgress(Message:="Inputting Post Content`nMight take 5-15 seconds to get input",PostType:="Tooltip,ErrorLoggingTextFile")

	; Click the plus button to make the title box show up
	Xpath = //div[@class='m-composer__base m-composer__base--withPreview']//i[@class='material-icons ng-star-inserted'][normalize-space()='add_circle_outline']
	try driver.FindElementByXPath(Xpath).click()
	catch e { ; if failed to plus button, then input the post body and text into the same box
		; msgbox, failed to click Plus
		; goto, InputMindsPostCombined
	} 

	; if plus button was clicked successfully, input title and body individually
	; Input title
	; Msgbox % "PostTitle: " PostTitle
	Xpath = //textarea[@placeholder='Title']
	; driver.FindElementByXPath(Xpath).SendKeys("heleeoeoe")

	; CLick into title box
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000) 

	; Input text itno title box
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=PostTitle)
	if(Status){ ; if failed to input title then skip to inputting both combined
		; msgbox, failed to input title
		Message = Failed to input Title
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

		MindsTitleStatus := "Failed"
		; Return

		; goto, InputMindsPostCombined
	}

	; msgbox, title input?
	; Input body
	if(MindsTitleStatus)
	MindsPost := PostTitleAndBody
	else, MindsPost := PostBody

	; Msgbox % "PostBody: " PostBody
	Xpath = //textarea[@id='minds-m-composer__textarea--1003']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=MindsPost)
	if(Status){
		; msgbox, failed to input body
		Message = Failed to post with Xpath, trying with ID
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
		Status := Selenium_LoopToSendValueToID("minds-m-composer__textarea--1003",5,1000,MindsPost)
		if(Status){
			Message = Failed to input Post Content into Page
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}
	}
} 
else, { ; otherwise, input just text
	SaveOrPostProgress(Message:="Inputting Text Post",PostType:="Tooltip,ErrorLoggingTextFile")
	; DevModeMsgBox("Inputting Text Post")
	Xpath = //textarea[@id='minds-m-composer__textarea--1003']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=PostTitleAndBody)


	if(Status){
		Message = Failed to post with Xpath, trying with ID
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
		Status := Selenium_LoopToSendValueToID("minds-m-composer__textarea--1003",5,1000,PostTitleAndBody)
		if(Status){
			Message = Failed to input Post Content into Page
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}
	}


	if(InStr(PostBody,"http"))
	{
		Message = Sleeping 5 seconds for preview to load
		SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile")
		sleep, 5000
	}

}

; sleep, 2000
SaveOrPostProgress(Message:="Submitting Post",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
}


loop, 3 {
	if(A_index = 3){
		SaveOrPostProgress(Message:="Failed to submit post",PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}


	; msgbox, submit with xpath
	Xpath = (//span[@class='ng-star-inserted'][normalize-space()='Post'])[2]
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
	if(Status){
		Message = Failed to Submit Post after 2 different attempts
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return

	}


	Loop, 5 {
		if(A_index = 5){
			Message = Failed to submit after 5 attempts.
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}

		js = document.querySelector("button[class='m-button m-button--blue m-button--small m-button--dropdown'] span[class='ng-star-inserted']").click();
		try status := driver.executeScript(js)

		sleep, 500

		; check if Post button still exists
		js = return document.querySelector("m-composer__base[ismodal='true'] m-button[data-ref='post-button'] div").innerText
		try, Status := driver.executeScript(js)    ;Executes a Javascript on the webpage, mostly used for buttons.

		if(!Status){
			; msgbox, successfully submitted after %A_index% attempts
			break
		}
		

		sleep, 1000
	}


	SaveOrPostProgress(Message:="Waiting 5 seconds to confirm post got submitted",PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	sleep, 5000

	; check that popup appears by checking for "Create Blog" button in popup
	js = return document.querySelector("div[class='m-composer__base'] div[class='m-composerTitleBar__createBlogTrigger ng-star-inserted']").innerText
	try status := driver.executeScript(js)
	if(InStr(Status, "Create Blog")){
		continue
	}
	else, {
		break
	}


}


/*		if(!TestingMode){
			SaveOrPostProgress(Message:="Submitting Post",PostType:="Tooltip,ErrorLoggingTextFile")

			Xpath = Xpath = (//span[@class='ng-star-inserted'][normalize-space()='Post'])[2]
			Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=5000)
			sleep, 1000
			Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=5000)
			if(Status){
				Message = Failed to Submit Post
				SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
				SaveDriverURLOFErrorPage()
				Return
			}
			}

			*/

			; Click the post button


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



			PauseBetweenPosts()
			DevModeMsgBox("done!")

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


			Return
			; -------------------------------/Minds-------------------------------
