LocalsSchedule:
;------------------------------------------------


CurrentSite := "Locals"
SaveOrPostProgress(Message:="Scheduling Post",PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

IniRead, LocalsPostPageURL, %SettingsIniFilepath%, General, LocalsPostPageURL, %A_Space%
if(LocalsPostPageURL = ""){
	Message = LocalsPostPageURL is blank in Settings.ini. Please add the Locals server URL Under [General]`nLocalsPostPageURL=
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
} 
else {
	; Reformat URL in case it has any extras we don't need.
	LocalsPostPageURLArray := StrSplit(LocalsPostPageURL,".locals.com")
	LocalsPostPageURL := LocalsPostPageURLArray[1] . ".locals.com/share?livestream"
}



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

try driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding

CheckForAlerts()

; Check Login Status on Locals
#Include, %A_ScriptDir%\Modules\Locals-Check-Login-Status.ahk 


; Input Title and Description into Create a Livestream - First Screen
; ------------------------------------------------
Xpath = //input[@id='title']

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


Xpath = //textarea[@id='body']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamDescription)

; click next button
Xpath = //span[contains(text(),'Next')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)



; Create a Livestream - Second Screen
; ------------------------------------------------
; check if "Schedule Livestream" checkbox is checked, check it if not
try, LivestreamScheduledCheckbox := driver.findElementByID("is_scheduled_livestream").isSelected()   ;Checks if a checkbox is ticked or not and saves it to a variable, usually: 0 = Unchecked,  -1 = Checked
if(LivestreamScheduledCheckbox = 0){
	Xpath = //label[normalize-space()='Schedule this live stream?']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

	LivestreamScheduledCheckbox := driver.findElementByID("is_scheduled_livestream").isSelected()
	if(LivestreamScheduledCheckbox = 0){
		Message = Failed to select the "Schedule Livestream" checkbox
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	}
}

; Make sure the "Notify Users" checkbox is checked
try, NotifyUsersCheckbox := driver.findElementByID("is_do_promo").isSelected()   ;Checks if a checkbox is ticked or not and saves it to a variable, usually: 0 = Unchecked,  -1 = Checked
if(NotifyUsersCheckbox = 0){
	Xpath = //label[@for='is_do_promo']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

	try, NotifyUsersCheckbox := driver.findElementByID("is_do_promo").isSelected()
	if(NotifyUsersCheckbox = 0){
		Message = Failed to select the "Notify Users" checkbox
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	}

}

FormatTime, LocalsLivestreamDate , %LivestreamDate%, MM/dd/yyyy ; _hhmmss 
FormatTime, LocalsLivestreamTime , %LivestreamTime%, hh:mm tt ; _hhmmss 

; clear data from date box and input date
Xpath = //input[@id='stream_schedule_date']
try, driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace)
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LocalsLivestreamDate)

; clear data from time box and input time
Xpath = //input[@id='stream_schedule_time']
try, driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace).SendKeys(driver.Keys.BackSpace)
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LocalsLivestreamTime)




; Upload Custom Thumbnail
; ------------------------------------------------

if(Livestreamthumbnail){
	Message = Uploading Thumbnail
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

	; this `label` element is a child of the thumbnail input element.
	xpath = (//label[@id='photo-upload-btn'])[2]

	; the element ID for input elements seems to change/be generated each time the page is generated
	XpathIDForUploads := GetHTMLValueFromXpathOuterHTML(XPATH, "label for")

	if(XpathIDForUploads = "Failed"){
		Message = Failed to grab element needed for uploading thumbnail. Please attach thumbnail manually.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

	ThubmnailInputXpath = (//input[@id='%XpathIDForUploads%'])[1]
	Status := Selenium_LoopToSendValueToXpath(Xpath:=ThubmnailInputXpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=Livestreamthumbnail)
	if(Status){
		Message = Failed to upload livestream thumbnail
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

	Sleep, 2000
}


if(ConfirmBeforeSubmit){
	OnMessage(0x44, "OnMsgBoxUserConfirmation")
	MsgBox 0x21, User Confirmation, Please check that all data was input correctly and fix any mistakes and then click PROCEED to finalize the Upload.`n`nClick STOP to cancel the rest of this Upload and move on to the next website.
	OnMessage(0x44, "")

	IfMsgBox OK, {

		} Else IfMsgBox Cancel, {
			Message = User Selected STOP button when asked for confirmation. Cancelling Rest of Site Upload.
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			Return
		}
	}
/*
*/

; click schedule button
Xpath = //span[normalize-space()='Schedule']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to click schedule post button
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
}

LocalsLivestreamURL := GrabLocalsPostURLUsingTitle(LivestreamTitle)

if(!InStr(LocalsLivestreamURL, "https")){
	; If url was not grabbed. wait 1 minute, reload page and then try grabbing URL again
	; @todo
}





; Navigate to the Post and Grab the URL
; ------------------------------------------------
Message = Navigating to Livestream Page to grab RTMP Settings
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

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


; Switch to RSTP mode
Xpath = //a[normalize-space()='switch to RTMP']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to switch Livestream to RTMP mode
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

Message = Waiting 5 seconds for Page to catch up
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
sleep, 5000

; Open Stream Settings Popup Window
Xpath = //button[normalize-space()='Stream Settings']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
if(Status){
	Message = Failed grab RTMP Settings. Unable to click the Stream Settings button.
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


Message = Waiting 5 seconds for Page to catch up
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
sleep, 5000


; Get RTMP Values from the popup
Xpath = //input[@id='rtmp_url']
try, LocalsRTMPURL := driver.findelementbyxpath(Xpath).Attribute("value")

/*Xpath = //div[@class='modal-wrapper modal-wrapper_stream']//div[5]
try, LocalsRTMPKey := driver.findelementbyxpath(Xpath).Attribute("value")
*/


Xpath = //div[@class='modal-wrapper modal-wrapper_stream']//div[5]

; Pull Out the RTMPKey from the OuterHTML using RegEx
LocalsRTMPKey := GetHTMLValueFromXpathOuterHTML(XPATH, "value")




IniWrite, %LocalsRTMPURL%, %PostStatusesFilepath%, Livestream,LocalsRTMPURL
IniWrite, %LocalsRTMPKey%, %PostStatusesFilepath%, Livestream,LocalsRTMPKey
IniWrite, %LocalsLivestreamURL%, %PostStatusesFilepath%, Livestream,LocalsLivestreamURL

ToolTip
Return
