
; -------------------------------SubScribeStar-------------------------------
PostToSubscribeStar:
CurrentSite := "SubScribeStar"


IniRead, SubscribeStarProfileURL, %SettingsIniFilepath%, SocialMediaPoster, SubscribeStarProfileURL, %A_Space%
if(SubscribeStarProfileURL = ""){
	Message = SubscribeStarProfileURL is blank. Please update Settings.ini
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

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


Status := NavigateFromBaseURLTo(SubscribeStarProfileURL)
if(Status){
	Message = Failed to Navigate to SubScribeStar Page
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
}

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

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

; check for Log In button text 
js = return document.querySelector(".top_bar-login.is-button").innerText;
try LoginStatus := driver.executeScript(js)

if(InStr(LoginStatus, "Log In")){ ; need to log in
	SaveOrPostProgress(Message:="Navigating to login page",PostType:="Tooltip,ErrorLoggingTextFile")

	try driver.Get("https://www.subscribestar.com/login")	; navigate to login page
	try driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding

	if(AutoLogin){

		; Check if email box contains text inputted by chrome
		js = return document.querySelector("input[placeholder='your email']").value;
		status := driver.executeScript(js)
		if(StrLen(Status) > 0){ ; click enter on the email input field
			Xpath = //input[@placeholder='your email']
			driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.ENTER)

			driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
		}
	}
	else, {
		SaveOrPostProgress(Message:="Login Expired. Please log back in",PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
}


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

; click the "Create Post" button
js = document.querySelector("div[class='new_post'] div[class='flat_button for-new_post']").click();  ; Send content through javascript (Great for getting around emoji chrome limitaitons)
try, driver.executeScript(js)

sleep, 1000


; post button at bottom right corner
Selector = ".flat_button.for-post_form"

; check if post button exists
js = return document.querySelector(%Selector%).innerText
try, Status :=  driver.executeScript(js) 
if(!Status){
	Message = Post Failed. Failed to click on "Create Post" button
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}



/*; Click on "Start your post by clicking here"
Xpath = //div[contains(text(),'Create Post')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
if(Status){
	Message = Failed to Click "Create Post" button with relative xpath
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

	Xpath = //div[@class='new_post']//div[@class='new_post-create_button']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
	if(Status){
		Message = Failed to click "Create Post" button with Contains(Text) xpath and relative Xpath - Trying wtih ID
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
		; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")

		Status := Selenium_LoopToClickID(IDName:="cover_upload",NumOfLoops:=5,SleepLength:=1000)
		if(Status){
			Message = Login Expired. Please log back in
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}
	}
}
*/

; sleep, 5000
; msgbox

; Clear any previous content from the post box
Message = Clearing if any text exists from an unposted post
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

Xpath = //trix-editor[@role='textbox']
Status := Selenium_LoopToClearXpath(Xpath,NumOfLoops:=5,SleepLength:=1000)
if(Status){
}
; try driver.FindElementByXPath(Xpath).clear()   ;Clears content from an element and sets


; Input Text into pop-up edit box
SaveOrPostProgress(Message:="Inputting Post Content",PostType:="Tooltip,ErrorLoggingTextFile")

; click on heading format button
Xpath = //button[@title='Heading']//i
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

sleep, 500


Xpath = //trix-editor[@role='textbox']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=PostTitle)
if(Status){
	Message = Failed to input Post Title
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

; driver.FindElementByXPath(Xpath).SendKeys(PostTitle)
sleep, 500
driver.FindElementByXPath(Xpath).SendKeys("`n`n")
sleep, 500

Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=PostBody)
if(Status){
	Message = Failed to input Post Body
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

; check if value was input
js = return document.querySelector("trix-editor[role='textbox']").innerText
try, Status :=  driver.executeScript(js) 
if(!Status){
	Message = Failed to input Post Content (E#8282)
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}
else, { ; if input successfully, post to error log
	String := SubStr(Status, 1, 30)
	Message = Post Content that got input: %String% ...
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
}



; Attach an image if a filepath was provided
if(ImageAttachmentFilepath != ""){
	SaveOrPostProgress(Message:="Attaching Image",PostType:="Tooltip,ErrorLoggingTextFile")

	; this works for both images and videos
	Xpath = //input[@multiple='multiple']
	driver.FindElementByXPath(Xpath).SendKeys(ImageAttachmentFilepath)


/*		Xpath = //span[@class='trix-button-group trix-upload-buttons']//button[2]
		Status := Selenium_LoopToClickXpathAndWaitForOpenWindow(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
		if(Status){
			Message = Failed to click on Picture Upload button
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		}
		
		InputStatus := InputFilePathIntoOpenWindow(ImageAttachmentFilepath)
		if(InputStatus = "Failed")
		{
			Message = Upload Failed:`nUnable to Find "Open File" window to input filepath into
			SaveOrPostProgress(Message:=Message,PostType:=",ErrorLoggingTextFile,DiscordErrorLogging")
			Return
		}
		*/

		SaveOrPostProgress(Message:="Waiting 5 seconds for Image to finish uploading",PostType:="Tooltip,ErrorLoggingTextFile")
		sleep, 5000
	}


	; Click the post Button
	SaveOrPostProgress(Message:="Submitting Post",PostType:="Tooltip,ErrorLoggingTextFile")


	; post button at bottom right corner
	Selector = ".flat_button.for-post_form"

	; check if post button exists
	js = return document.querySelector(%Selector%).innerText
	try, Status :=  driver.executeScript(js) 
	if(!Status){
		Message = Post Failed. Unable to find Post button using usual method.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
	else, {
		Message = Post Button Exists and has the text: %Status%
		SaveOrPostProgress(Message:=Message,PostType:=",ErrorLoggingTextFile,DiscordErrorLogging")
	}

	; click submit button
	js = document.querySelector(%Selector%).click()
	try, Status :=  driver.executeScript(js) 
	; Msgbox % "Status: " Status

	sleep, 2000

	; check if post button still exists
	js = return document.querySelector(%Selector%).innerText
	try, Status :=  driver.executeScript(js) 
	if(Status){
		Message = Post Failed. Failed to click Post button using usual method.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	; double double check that post got posted by checking if able to grab text that was just input
	js = return document.querySelector("trix-editor[role='textbox']").innerText
	try, Status :=  driver.executeScript(js) 
	if(Status){
		Message = Post Failed. Input box still exists on page but no errors were thrown when clicking post button
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	


/*	Xpath = //button[contains(text(),'Post')]
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
	if(Status){
		Message = Failed to Submit Post
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	*/
	IniWrite, Successful, %StatusFileFilePath%, Status, SubScribeStar
	AddToTotalPostsPostedCount()

	
	
	PauseBetweenPosts()

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

	Return
	; -------------------------------/SubScribeStar-------------------------------
