

MediaManagerUpdate:
; ------------------------------------------------


CurrentSite := "Media Manager"
SaveOrPostProgress(Message:="Navigating to Website",PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


IniRead, FDRRadioURL, %SettingsIniFilepath%, General, FDRRadioURL, %A_Space%
if(FDRRadioURL = ""){
	Message = FDRRadioURL is blank in Settings.ini. Please add it to the following location: [General]`nFDRRadioURL=
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


FDRRadioBaseURL := "https://" . ExtractBaseURL(FDRRadioURL)
FDRRadioURL :=  FDRRadioBaseURL . "/manage/"

Status := NavigateFromBaseURLTo(FDRRadioURL)
if(Status)
Return

CheckForAlerts()

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

Try, PageURL := driver.Url
if(InStr(PageURL, "/NotLoggedIn")){
	Message = Logged out. Trying to log back in.
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

	Xpath = (//span[contains(text(),'Login')])[1]
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

	sleep, 1000

	Try, PageURL := driver.Url
	if(InStr(PageURL, "/NotLoggedIn")){
		Message = Failed to log back in automatically. Please login and try again.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

}

Message = Updating Redirect URLs
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

; URL for each redirect
LocalsLiveRedirectURL := FDRRadioBaseURL . "/manage/#/Redirects/Modify?redirectID=738"
RumbleLiveRedirectURL := FDRRadioBaseURL . "/manage/#/Redirects/Modify?redirectID=739"
OdyseeLiveRedirectURL := FDRRadioBaseURL . "/manage/#/Redirects/Modify?redirectID=742"

; read the url's from the status file
IniRead, LocalsLivestreamURL, %PostStatusesFilepath%, Livestream,LocalsLivestreamURL, %A_Space%
IniRead, RumbleLivestreamURL, %PostStatusesFilepath%, Livestream,RumbleLivestreamURL, %A_Space%
IniRead, OdyseeLivestreamURL, %PostStatusesFilepath%, Livestream,OdyseeLivestreamURL, %A_Space%



if(LocalsLivestreamURL)
UpdateMediaManagerRedirect(LocalsLiveRedirectURL, LocalsLivestreamURL)
else, {
	Message = LocalsLivestreamURL is blank. Skipping
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
}

if(RumbleLivestreamURL)
UpdateMediaManagerRedirect(RumbleLiveRedirectURL, RumbleLivestreamURL)
else, {
	Message = RumbleLivestreamURL is blank. Skipping
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
}


if(OdyseeLivestreamURL)
UpdateMediaManagerRedirect(OdyseeLiveRedirectURL, OdyseeLivestreamURL)
else, {
	Message = OdyseeLivestreamURL is blank. Skipping
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
}





UpdateMediaManagerRedirect(MediaManagerRedirectPageURL, RedirectURL){

	try, driver.Get(MediaManagerRedirectPageURL)	;Open selected URL
	try, driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding

	Xpath = (//input[@id='destinationURL'])[1]
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=RedirectURL, ClearElement := 1)


	; Snippet to for check:
	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
	}

	; Save button
	Xpath = (//span[contains(text(),'Save')])[1]
	Status := Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
	if(Status){
		Message = Failed to click Save button
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

	Sleep, 1000
	Return "Success"

}
