
; -------------------------------Minds-------------------------------
PostToSubstack:
CurrentSite := "Substack"
Message = Navigating to Post Creationg Page
SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")



IniRead, SubstackProfileURL, %SettingsIniFilepath%, SocialMediaPoster, SubstackProfileURL, %A_Space%
if(SubstackProfileURL = ""){
	Message = SubstackProfileURL is blank. Please update Settings.ini under [SocialMediaPoster]`nSubstackProfileURL=https://PROFILENAME.substack.com/publish/home
	IniWrite,%A_Space%,%SettingsIniFilepath%, SocialMediaPoster, SubstackProfileURL
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}



URLAttempt := NavigateFromBaseURLTo(SubstackProfileURL)
if(URLAttempt = "Failed")
Return

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

; Check Login Status
; ------------------------------------------------
Message = Checking Login Status
SaveOrPostProgress(Message,PostType:="Tooltip")


; Check for New Post button at the top right of page
Xpath = (//button[@id='trigger7'])[1]
try ElementInnerText := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text
if(!InStr(ElementInnerText, "New post")){
	Message = New post button not found. Please check login status. If already logged in, the xpath of the element might have changed.
	SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

; Start Text Post
; ------------------------------------------------
Message = Creating New Post
SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

; click new post button
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(status){
	Message = Failed to click New post button
	SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


; click new note button in popup menu
Xpath = //div[contains(text(),'New note')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(status){
	Message = Failed to click New note button
	SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


; Input Title and Text
; ------------------------------------------------

; Input Text Box, this is awful and will probably break in the future, but works for now.
; @todo: Figure out relative xpath, SelectorHub is not providing anything useful.
Xpath = /html[1]/body[1]/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]


try, CurrentText := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text
if(strlen(CurrentText) > 1){
	Message = Exiting. Functionality to clear out previous failed posts is a WIP.
	SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}


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



; Attach Image
; ------------------------------------------------
if(ImageAttachmentFilepath){
	Message = Attaching Image
	SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

	Xpath = (//input[@accept='image/*,.heic'])[1]

	; Attach image
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=ImageAttachmentFilepath, ClearElement:=0)
	if(status){
		Message = Failed to attach image
		SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}
}

; driver.FindElementByXPath(Xpath).SendKeys(PostTitleAndBody)




sleep, 1000


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
}



Xpath = //button[normalize-space()='Post']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(status){
	Message = Failed to Click Post Now Button
	SaveOrPostProgress(Message,PostType:=",ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}







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

PauseBetweenPosts()
DevModeMsgBox("done!")

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


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