OdyseeSchedule:
;------------------------------------------------
CurrentSite := "Odysee"
SaveOrPostProgress(Message:="Scheduling Post",PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Status := NavigateFromBaseURLTo("https://odysee.com/$/livestream")
if(Status)
Return


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

CheckForAlerts()

; Check Login Status
Try, PageURL := driver.Url
if(InStr(PageURL, "/signup")){
	Message = Website is logged out. Please log back in.
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

; If clear button exists, click it and clear any data from a previous attempt
Xpath = //span[contains(text(),'Clear')]
try, Status := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text
if(InStr(Status, "Clear")){
	Message = Clearing out previous unfinished upload
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

	; we need to wipe out the previously unfinished upload
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
	if(Status){
		Message = Failed to clear out previous upload
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}
}


; create a slug string and format it
FormatTime, OdyseeLivestreamDate , %LivestreamDate%, MM-dd-yyyy ; _hhmmss 
OdyseeLivestreamSlug := "livestream-" . OdyseeLivestreamDate
OdyseeURLSLUG := LBRYCMDTextReplacement(OdyseeLivestreamSlug)
IniWrite, %OdyseeURLSLUG%, %PostStatusesFilepath%, Livestream,OdyseeURLSLUG

/*Xpath = (//div[@class='form-field__prefix'])[1]
try OdyseeChannelURL := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text

*/

Message = Inputting Livestream Information
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = //input[@placeholder='Descriptive titles work best']
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 = //input[@name='content_name']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=OdyseeURLSLUG)
if(Status){
	Message = Failed to Input Livestream URL
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


Xpath = //textarea[@id='content_description']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamDescription)
if(Status){
	Message = Failed to Input Livestream Description
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}





; Click "Scheduled Time" checkbox
Message = Selecting -Scheduled Time- checkbox
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = //label[normalize-space()='Scheduled Time']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=5000)
if(Status){
	Message = Failed to click the "Scheduled Time" checkbox
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


; Create variables with time and date of timestream
FormatTime, LivestreamYear , %LivestreamDate%, yyyy ; _hhmmss 
FormatTime, LivestreamMonth , %LivestreamDate%, MM ; _hhmmss 
FormatTime, LivestreamDay , %LivestreamDate%, dd ; _hhmmss 

FormatTime, LivestreamHour , %LivestreamTime%, h ; _hhmmss 
FormatTime, LivestreamMinute , %LivestreamTime%, m ; _hhmmss 
FormatTime, LivestreamAMPM , %LivestreamTime%, tt ; _hhmmss 


; Input Livestream Time and Date
Xpath = //input[@placeholder='----']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamYear)

Xpath = //input[@name='month']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamMonth, ClearElement:=1)

Xpath = //input[@name='day']
Status := Selenium_LoopToSendValueToXpath(Xpath,NumOfLoops:=1,SleepLength:=1000,StringTextContent:=LivestreamDay, ClearElement:=1)

Xpath = //input[@name='hour12']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamHour, ClearElement:=1)

Xpath = //input[@name='minute']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamMinute, ClearElement:=1)

Xpath = //select[@name='amPm']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamAMPM)


if(LivestreamThumbnail != ""){
	; Upload Thumbnail
	Xpath = //input[@accept='.png, .jpg, .jpeg, .gif, .webp']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamThumbnail)
	if(Status){
		Message = Failed to Upload Thumbnail
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	}

	; check for the Upload Thumbnail Confirmation popup
	Xpath = //h1[normalize-space()='Upload thumbnail']
	loop, 10 {
		if(A_index = 10){
			Message = Upload Thumbnail Confirmation did not appear after 10 seconds.
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		}

		try, Status := driver.findelementbyxpath(Xpath).Attribute("innerText")
		if(Status = "Upload Thumbnail"){
			break
		}
		else, {
			sleep, 1000
			Continue
		}
	}

	try, Status := driver.findelementbyxpath(Xpath).Attribute("innerText")
	if(Status = "Upload Thumbnail"){
		; click confirm upload button
		Xpath = //div[@class='card__actions']//button[@aria-label='Upload']//span[@class='button__content']
		Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
	}
	else, {
		Message = Upload Failed: Upload thumbnail confirmation popup did not show up.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

/*	; check if the "Are you sure you want to upload this thumbnail to odysee.com?" popup exists
	; Xpath = /html/body/div[4]/div/div/label
	; try Status := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text
	if(Status){ ; if element exists, then click the confirm button

		Xpath = //div[@class='card__actions']//button[@aria-label='Upload']//span[@class='button__content']
		Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
		; MsgBox,,Element innerText: `n, % driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text
	}
	else, {
		Message = Upload thumbnail confirmation popup did not show up.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	}
	*/


	Xpath = //div[@class='column__item thumbnail-picker__preview']

	; Get element with Thumbnail and check if image upload was successful
	Try, Status := driver.findelementbyxpath(Xpath).Attribute("outerHTML") ;XPATH-ID & Tag
	if(!InStr(Status, "thumbs.odycdn.com")){
		Message = Thumbnail Upload Failed
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	}

	} else, {
		Message = Video Upload Failed. Odysee Requires a thumbnail for video uploads.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}


	LivestreamTagsArray := StrSplit(LivestreamTags, ",")

	Loop % LivestreamTagsArray.Length(){
		; ArrayItem := ARRAY[A_Index]
		Tag := LivestreamTagsArray[A_Index]
		Xpath = //input[@placeholder='gaming, crypto']

		Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=Tag)
		sleep, 500
		driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.ENTER)
		sleep, 500

		; Odysee accepts max of 5 tags
		if(A_Index = 5)
		break
	}


	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
			}
		}
/*
*/


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

; Click Create Button at bottom of the screen
Xpath = //span[contains(text(),'Create')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
if(Status){
	Message = Failed to click "Create" button at the bottom of page
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

sleep, 1000

; Click confirm button in the popup
Xpath = //span[contains(text(),'Confirm')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
if(Status){
	Message = Failed to click "Confirm" button in confirmation popup
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


IniRead, OdyseeChannelURL, %SettingsIniFilepath%, %ScriptSettingsSection%, OdyseeChannelURL, %A_Space%
if(!OdyseeChannelURL){
	OdyseeChannelURL := "https://odysee.com/@freedomain:b/"
	IniWrite, %OdyseeChannelURL%, %SettingsIniFilepath%, %ScriptSettingsSection%, OdyseeChannelURL
}


OdyseeLivestreamURL := OdyseeChannelURL .  OdyseeURLSLUG
IniWrite, %OdyseeLivestreamURL%, %PostStatusesFilepath%, Livestream,OdyseeLivestreamURL


; Grab RTMP Settings
Message = Waiting for Confirmation Screen to grab RTMP Settings
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = (//span[contains(text(),'View Livestream Settings')])[1]
loop, 60 {
	try, InnerText := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text

	if(InnerText = "View Livestream Settings"){
		break
	}
	sleep, 1000
}

; click on the "View livestream Settings" button
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
if(Status){
	Message = Failed to click on -View Livestream Settings- Button
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


; Grab the Stream URL and Key
Xpath = (//input[@name='stream-server'])[1]
loop, 5 {
	Try OdyseeRTMPURL := driver.findelementbyxpath(Xpath).Attribute("value") ;Xpath Value

	if(OdyseeRTMPURL)
	break
	else, 
	sleep, 1000
}

Xpath = (//input[@name='livestream-key'])[1]
Try OdyseeRTMPKey := driver.findelementbyxpath(Xpath).Attribute("value") ;Xpath Value


if(OdyseeRTMPKey = ""){
	Message = Failed to grab Odysee RTMP Key. Page did not load?
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

IniWrite, %OdyseeRTMPURL%, %PostStatusesFilepath%, Livestream,OdyseeRTMPURL
IniWrite, %OdyseeRTMPKey%, %PostStatusesFilepath%, Livestream,OdyseeRTMPKey



Return