
; -------------------------------Bastyon-------------------------------
PostToPocketNet:
CurrentSite := "Bastyon"


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

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

Status := CheckCurrentTabForCurrentSite()
if(Status){
	Return
}
; Loop a bunch of times for page to load
SaveOrPostProgress(Message:="Waiting for Page to Load Fully",PostType:="Tooltip,ErrorLoggingTextFile")
loop, 12 {

	; click into input box
	js = document.querySelector(".emojionearea-editor.pastable").click()
	try driver.executeScript(js)

	Xpath = //div[@class='emojionearea-editor pastable']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=1,SleepLength:=5000)
	if(!Status)
	break

	Xpath = //b[normalize-space()='Sign in']
	try Status := driver.findelementbyxpath(Xpath).Attribute("textContent") ;XPath: ID=site-title & span tag
	if(InStr(Status, "Sign in")){
		break
	}

	; Msgbox % "Status: " Status
}
if(Status){ ; if failed to click into entry box
	Message = Login Expired. Please log back in
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}




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

js = document.querySelector("div[title='Clear post'] i[class='fas fa-times-circle']").click()
try driver.executeScript(js)

sleep, 1000

js = document.querySelector(".btn1.medium").click()
try driver.executeScript(js)

sleep, 1000

; msgbox

; input post text content
Message = Inputting Post Content
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

JsToExecute = document.getElementsByClassName('emojionearea-editor pastable')[0].innerText = '%JSPostTitleAndBody%'
Status := JS_TryToExecute(JsToExecute)
if(Status){
	Message = Post Failed: Check Login Status
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

JsToExecute = return document.getElementsByClassName('emojionearea-editor pastable')[0].textContent;
try Status := driver.executeScript(JsToExecute)
if(Status = ""){
	Message = Post Failed: Check Login Status
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}
; Message = Post that got input: %Status%
; SaveOrPostProgress(Message:=Message,PostType:=",ErrorLoggingTextFile,DiscordErrorLogging")
; DevModeMsgBox(Message)
; msgbox % "textContent: " . textContent



; Doesn't work for some reason
; JsToExecute = return document.querySelector("div.txt div.emojionearea.message div.emojionearea-editor.pastable").innerText
; Status := JS_TryToExecute(JsToExecute)
; if(Status){
	; 	Message = Check Login Status: Failed to Input Post
	; 	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	; 	Return
	; }


	sleep, 2000

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

		; Click the "Image" button
		SaveOrPostProgress(Message:="Attaching Image",PostType:="Tooltip,ErrorLoggingTextFile")
/*			Xpath = //div[@title='Add Images to Post']
			Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
			if(Status){
				Message = Failed to Click "Attach Image" Button
				SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
				SaveDriverURLOFErrorPage()
				Return
			}
			*/



			; sleep, 5000

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


			SaveOrPostProgress(Message:="Waiting 10 Seconds For Image to Upload",PostType:="Tooltip,ErrorLoggingTextFile")

			sleep, 10000


		}

		; Input Tags
		Xpath = //div[@class='search fastSearchShow']//input[@id='text']
		TagsArray := StrSplit(PostTags, ",")
		TagsArrayLength := TagsArray.Length()   ; Save total number of items in the array
		loop, %TagsArrayLength% {
			Tag := TagsArray[A_Index]
			Tag := StrReplace(Tag, " ", "")
			DevModeMsgBox(Tag)
			; Tag = %Tag%
			Xpath = //input[@placeholder='Categories and tags']
			driver.FindElementByXPath(Xpath).SendKeys(Tag)
			driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.ENTER)

			; driver.findElementsByClass("sminput").item[2].click()

			; JsToExecute = document.querySelector("#text[placeholder='Add Categories/Tags']").value = "#%Tag%"
			; Status := JS_TryToExecute(JsToExecute)
			; 
			; driver.SendKeys(driver.Keys.SPACE)
			; 
			; JsToExecute = return document.querySelector("#text[placeholder='Add Categories/Tags']").value
			; Status := JS_TryToExecute(JsToExecute)

			if(A_index = 5)
			Break


			/*Tag := TagsArray[A_Index]
			Tag := StrReplace(Tag, " ", "")
			; Tag = %Tag%
			Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000,SendKeysContent:=Tag)
			try driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.Space)

			*/
			; if(A_index = 8) ; Steemit only allows 8 tags
			; Break
		}
		; msgbox

/*	Xpath = //div[@class='emojionearea-editor pastable']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=3,SleepLength:=1000,StringTextContent:=PostTitleAndBody)
	if(Status){
		SaveOrPostProgress(Message:="Failed to Input Post",PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return

		; Send, 
	}
	*/


	; Msgbox,4096,Pause!!,asdf

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

	; CLick the post button
	Xpath = //button[normalize-space()='Post']
	loop, 3 {
		try driver.FindElementByXPath(Xpath).click()
		sleep, 1500
	}
	; Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=2000)
	; sleep, 2000
	; Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=2000)
/*	if(Status){
		Message = Failed to Submit Post
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}
	*/
	Message = Waiting 5 seconds to double check that post got submitted
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip")
	sleep, 5000
	; try PossiblyError := driver.findElementsByClass("paddingmobilewrapper").item[1].Attribute("innerText") ; Grabb innertext

	; CHeck if input box still contains text
	JsToExecute = return document.getElementsByClassName('emojionearea-editor pastable')[0].textContent;
	try Status := driver.executeScript(JsToExecute)
	if(Status){
		; sleep, 2000
		try Jscheck = return document.querySelector(".paddingmobilewrapper").textContent ; Grabb innertext
		PossiblyError := driver.executeScript(Jscheck)
		PossiblyError := StrReplace(PossiblyError, "`n", "") ; remove extra stuff from text
		PossiblyError := StrReplace(PossiblyError, "  ", "") ; remove extra stuff from text
		PossiblyError := StrReplace(PossiblyError, A_Tab, "") ; remove extra stuff from text

		; Msgbox % "PossiblyError: " PossiblyError
		Message = Failed To Submit Post: Error was: %PossiblyError%
		; DevModeMsgBox(PossiblyError)
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}



	; sleep, 2000 
	; Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=2000)
/*	if(Status){
		Message = Failed to Submit Post
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

	*/


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

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

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


	Return
	; -------------------------------/Bastyon-------------------------------
