instPath = SystemPath( "ScriptPath" )

If (FileExists("\Windows\StartUp\Mortscript.lnk"))
	Delete("\Windows\StartUp\MortScript.lnk")
EndIf
If (FileExists(instPath&"\ATPluginInstaller.exe"))
	Move(instPath&"\ATPluginInstaller.exe",instPath&"\plgFiles")
	Delete(instPath&"\default.plg")
EndIf

Call MainMenu
Exit

###### Main menu 
Sub MainMenu

SetChoiceEntryFormat(28,14)

title[1] = "1) Run Weather Update and exit"
title[2] = "2) Run Full Update and exit"
title[3] = "3) Forecast Options/Setup"
title[4] = "4) Radar Setup"
title[5] = "5) Panel Display Options"
title[6] = "6) Uninstall"

While (1)
	Choice("TitaniumWeather v4.1","Select an option, then push OK.^CR^Press Cancel to exit.",title)

	Case(0)
		Vibrate(25)
		Exit
	Case(1)
		RegWriteDWord("HKLM","Software\TitaniumWeather","forceupdate",1)
		CallScript("TitaniumWeather.mscr")
		Vibrate(25)
		Exit
	Case(2)
		RegWriteDWord("HKLM","Software\TitaniumWeather","forceupdate",1)
		RegWriteDWord("HKLM","Software\TitaniumWeather","fupdate",1)
		CallScript("TitaniumWeather.mscr")
		Vibrate(25)
		Exit
	Case(3)
		Vibrate(25)
		Call AdvancedOptions
	Case(4)
		Vibrate(25)
		Call RadarSetup
	Case(5)
		Vibrate(25)
		Call DisplayOptions
	Case(6)
		Vibrate(25)
		ans = Question("Are you sure you want to uninstall TitaniumWeather?","Uninstall TitaniumWeather","YesNo")
		If (ans = TRUE)
			Vibrate(25)
			Call Uninstall
		EndIf
		Vibrate(25)
	EndChoice
EndWhile

EndSub


#####################
###### Forecast Options/Setup
Sub AdvancedOptions

While(1)
	code = RegRead("HKLM", "Software\TitaniumWeather","Code")
	units = RegRead("HKLM", "Software\TitaniumWeather","Units")
	usertime = RegRead("HKLM", "Software\TitaniumWeather","usertime")
	userauto = RegRead("HKLM", "Software\TitaniumWeather","userauto")
	startconn = RegRead("HKLM", "Software\TitaniumWeather","Connect")
	ontime = RegRead("HKLM", "Software\TitaniumWeather","ONtime")
	offtime = RegRead("HKLM", "Software\TitaniumWeather","OFFtime")
	nextupdate = RegRead("HKLM", "Software\TitaniumWeather","nextupdate")

	units=toupper(units)

	If ( (ontime >= 0) AND (offtime >= 0) )
		ontime = ontime & " to " & offtime
	Else
		ontime = "Always"
	EndIf
	
	If (startconn)
		startconn = "Y"
	Else
		startconn = "N"
	EndIf

	Choice("Setup","Select an option, then push OK. Press Cancel to exit to main menu.","1) Zip or Location Code ("&code&")","2) Set Auto On/Off ("&ontime&")","3) Weather Update Interval ("&usertime&" min)","4) Forecast Update Interval ("&userauto&")","5) Connect to Internet ("&startconn&")","6) Temperature units ("&units&")")
	
	# exit options
	Case(0)
		Vibrate(25)
		Call MainMenu
		
	# Location or zip
	Case(1)
		Vibrate(25)
		zip = Input("Specify a zip or location code:", "Zip or Location Code ("&code&")")

		If (zip ne "")
			zip=toupper(zip)
			RegWriteString("HKLM", "Software\TitaniumWeather","Code",zip)
			RegWriteDWord("HKLM","Software\TitaniumWeather","fupdate",1)
		EndIf
		Vibrate(25)
		
	# Set ON/OFF update periods
	Case(2)
		Vibrate(25)
		If(Question("Would you like set an ON/OFF update hours? ^CR^Press NO for always on.","ON/OFF Update Hours","YesNo"))
			offtime = Input("At what time should auto updates disable?^CR^Enter hour only in 24 hr format^CR^Example: for 10 PM, enter 22","OFF Time",TRUE)
			ontime  = Input("At what time should auto updates resume?^CR^Enter hour only in 24 hr format^CR^Example: for 6 AM, enter 6","ON Time",TRUE)
		Else
			offtime = -1
			ontime  = -1
		EndIf
		Vibrate(25)
		
		RegWriteDWord("HKLM", "Software\TitaniumWeather","OFFtime",offtime)
		RegWriteDword("HKLM", "Software\TitaniumWeather","ONtime",ontime)
		
		# schedule an update in case previously schedule to run at next ON time
		RemoveNotifications(instPath&"\TitaniumWeather.exe")
		RunAt(TimeStamp()+60, instPath&"\TitaniumWeather.exe")

	# Update interval
	Case(3)
		Vibrate(25)
		If(Question("Would you like to setup for auto updates?","Auto Updates","YesNo"))
			usertime = Input("Specify an auto update interval in minutes:", "Update Interval ("&usertime&" min)",TRUE)
			
			# check if next update has been scheduled further ahead before
			If ((TimeStamp()+usertime*60) < nextupdate)
				RemoveNotifications(instPath&"\TitaniumWeather.exe")
				RunAt(TimeStamp()+usertime*60, instPath&"\TitaniumWeather.exe")
			EndIf
		Else
			usertime = 0
		EndIf
		Vibrate(25)
		RegWriteDWord("HKLM","Software\TitaniumWeather", "usertime", usertime)
		
	# Full update interval
	Case(4)
		Vibrate(25)
		userauto = Input("Specify number of auto updates before full update runs:", "Update Interval ("&userauto&" current before full update)",TRUE)
		RegWriteDWord("HKLM","Software\TitaniumWeather", "userauto", userauto)
		Vibrate(25)

	# Attempt to connect to internet
	Case(5)
		startconn = Question("Attempt to create internet connection before updating ?", "Internet Setting", "YesNo")
		Vibrate(25)
		RegWriteDWord("HKLM","Software\TitaniumWeather", "Connect", startconn)

	# Units - English or metric
	Case(6)
		Vibrate(25)
		If(Question("Switch to Celsius degrees display? Select 'No' to display degrees in Fahrenheit", "Temperature Units", "YesNo"))
			RegWriteString("HKLM","Software\TitaniumWeather", "Units", "C")
			copy (instPath&"\digits\c.png",instPath&"\panelicons\degree.png", TRUE)
		Else
			RegWriteString("HKLM","Software\TitaniumWeather", "Units", "F")
			copy (instPath&"\digits\f.png",instPath&"\panelicons\degree.png", TRUE)
		EndIf
		
		Vibrate(25)
		RegWriteDWord("HKLM","Software\TitaniumWeather","fupdate",1)
	EndChoice
EndWhile
EndSub
# End Sub AdvancedOptions

#################
Sub DisplayOptions

While(1)
	summary = RegRead("HKLM", "Software\TitaniumWeather","Summary")
	current = RegRead("HKLM", "Software\TitaniumWeather","Current")
	forecast = RegRead("HKLM", "Software\TitaniumWeather","Forecast")
	links = RegRead("HKLM", "Software\TitaniumWeather","Links")
	radars = RegRead("HKLM", "Software\TitaniumWeather","Radars")
	Icons = RegRead("HKLM", "Software\TitaniumWeather","Icons")
	Choice("Display","Select the pages to enable/disable or choose 6 to set icon pack, then push OK.^CR^Press Cancel to exit to main menu.","1) Summary page ("&Summary&")","2) Current Detailed ("&Current&")","3) Forecast Detailed ("&Forecast&")","4) Weather Links ("&links&")","5) Radar ("&radars&")","6) Type of Icons used("&Icons&")")
	
	# exit options
	Case(0)
		Vibrate(25)
		Call MainMenu
	Case(1)
		Vibrate(25)
		summary = Question("Display the Summary page?","Summary","YesNo")
		If (summary)
			summary = "Enabled"
		Else
			summary = "Disabled"
		EndIf
		Vibrate(25)
		RegWriteString("HKLM","Software\TitaniumWeather", "Summary",summary)
		Call SetPages		
	Case(2)
		Vibrate(25)
		current = Question("Display the Current Conditions page?","Current Conditions","YesNo")
		If (current)
			current = "Enabled"
		Else
			current = "Disabled"
		EndIf
		Vibrate(25)
		RegWriteString("HKLM","Software\TitaniumWeather", "Current",current)		
		Call SetPages		
	Case(3)
		Vibrate(25)
		forecast = Question("Display the Detailed Forecast page?","Detailed Forecast","YesNo")
		If (forecast)
			forecast = "Enabled"
		Else
			forecast = "Disabled"
		EndIf
		Vibrate(25)
		RegWriteString("HKLM","Software\TitaniumWeather", "Forecast",forecast)		
		Call SetPages		
	Case(4)
		Vibrate(25)
		links = Question("Display the Weather Links page?","Weather Links","YesNo")
		If (links)
			links = "Enabled"
		Else
			links = "Disabled"
		EndIf
		Vibrate(25)
		RegWriteString("HKLM","Software\TitaniumWeather", "Links",links)		
		Call SetPages	
	Case(5)
		Vibrate(25)
		radars = Question("Display the Radar page?","Radar","YesNo")
		If (radars)
			radars = "Enabled"
		Else
			radars = "Disabled"
		EndIf
		Vibrate(25)
		RegWriteString("HKLM","Software\TitaniumWeather", "Radars",radars)		
		Call SetPages	
	Case(6)
		Vibrate(25)
		icons= DirContents(instPath&"\plgFiles\*.plg",DC_Files)
		x=  Choice ("Icon Choices", "Choose an icon type to make change", 0, 0, icons)
		If (x>0)
			Vibrate(25)
			Run(instPath&"\plgFiles\ATPluginInstaller.exe","-i "&icons[x])
			Message("Please wait 20 seconds while layouts are updated.^CR^Press OK to continue process.","Updating....")
			xicon=Split(icons[x],".")
			RegWriteString("HKLM","Software\TitaniumWeather", "Icons",xicon[1])		
			RegWriteString("HKLM","Software\Microsoft\CHome\TitaniumWeather\Page8","CurrentIcon","\Program Files\TitaniumWeather\"&xicon[1]&"\02.png")
			RegWriteDWord("HKLM","Software\TitaniumWeather","forceupdate",1)
			RegWriteDWord("HKLM","Software\TitaniumWeather","fupdate",1)
			CallScript("TitaniumWeather.mscr")
			Message("Icon layout update is complete.","Finished")
			Run(instPath&"\CHomeRefresh.exe")
		EndIf
	EndChoice
EndWhile
EndSub
# End Sub DisplayOptions

#####################
Sub RadarSetup

		maxURLs = 6

		radarurls = ""
		For n = 1 to maxURLs step 1
			radarurls = radarurls & RegRead("HKLM","Software\TitaniumWeather","radar"&n) & "^NL^"
		Next

		radarurls = Input("Enter complete URL(s) for your custom radar image. Enter each URL (up to "&maxURLs&") separately, one on each line.", "Custom Radar URL",FALSE,TRUE,radarurls)

		If (radarurls ne "")
			n = 0

			# store the the URLs
			ForEach url in Split(radarurls, "^NL^", TRUE)
				If (Find(url,"http") && (n < maxURLs))
					n = n + 1
					RegWriteString("HKLM","Software\TitaniumWeather", "radar"&n, url)
				EndIf
			EndForEach
			RegWriteDWord("HKLM","Software\TitaniumWeather","totalURL",n)
			# clear out the old ones
			While (n < maxURLs)
				n = n + 1
				RegWriteString("HKLM","Software\TitaniumWeather", "radar"&n, "")
			EndWhile

		EndIf
		Vibrate(25)
EndSub
#####################
Sub SetPages

	pages = ""
	summary = RegRead("HKLM", "Software\TitaniumWeather","Summary")
	current = RegRead("HKLM", "Software\TitaniumWeather","Current")
	forecast = RegRead("HKLM", "Software\TitaniumWeather","Forecast")
	links = RegRead("HKLM", "Software\TitaniumWeather","Links")
	radars = RegRead("HKLM", "Software\TitaniumWeather","Radars")

	If (summary eq "Enabled")
		pages = pages&"Page1;"
	EndIf
	If (current eq "Enabled")
		pages = pages&"Page2;"
	EndIf
	If (forecast eq "Enabled")
		pages = pages&"Page3;Page4;Page5;Page6;Page7;"
	EndIf
	If (links eq "Enabled")
		pages = pages&"Page8;"
	EndIf
	If (radars eq "Enabled")
		pages = pages&"Page9"
	EndIf
	RegWriteString("HKLM","Software\Microsoft\CHome\TitaniumWeather","Pages",pages)
	RegWriteDWord ("HKLM", "Software\Microsoft\CHome\TitaniumWeather","Updated","0")
	RegWriteDWord ("HKLM", "Software\Microsoft\CHome\TitaniumWeather","Updated","1")

EndSub
#################
Sub Uninstall
	RemoveNotifications("\Program Files\TitaniumWeather\TitaniumWeather.exe")
	Message("Notification Queue has been cleared for future updates.  To finish, uninstall:  Please go to your phone settings menu and disable 'Windows Default' today item.  Finally, go to Remove Programs and remove Showaco TitaniumWeather from there and reboot your phone.","Finish Uninstall")
EndSub
