'DON'T FORGET TO COPY THESE BITS TOO!
Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions

Public Module Extensions
	Public defaultStandardTheme, defaultNightTheme, defaultTertiaryTheme As Theme
	Public ControlColour As Color = SystemColors.Control 'shorthand due to laziness and bad memory
	Public FileToOpen As String = ""
	Public Structure Notification
		Dim Text As String
		Dim Category As Integer
	End Structure

	Public Structure Theme
		Dim TextBoxFG, TextBoxBG, SidebarFG, SidebarBG, StatusBarFG, StatusBarBG, NotificationCriticalFG, NotificationCriticalBG, NotificationWarningFG, NotificationWarningBG, NotificationInfoFG, NotificationInfoBG As Color
	End Structure

	'*******************
	'***REUSABLE CODE***
	'*******************
	'everything between here and the "end reusable" section can be reused in future projects

	Public Function GetPath(file As String, Optional AppendBackslash As Boolean = True) As String
		Dim FileArray As String() = file.Split("\")
		Dim ReturnMe As String = ""
		For i = 0 To FileArray.Length - 2
			ReturnMe &= FileArray(i) & "\"
		Next
		If Not AppendBackslash Then ReturnMe.RemoveLast(1)
		Return ReturnMe
	End Function

	Public Function GetFile(file As String, Optional StripExtension As Boolean = False) As String
		If StripExtension Then
			Return RemoveFileExtension(file.Split("\")(file.Split("\").Length - 1)) 'strips the file extension, including the period. returns "filename" for "C:\filename.png"
		Else
			Return file.Split("\")(file.Split("\").Length - 1) 'returns the last item divided by backslashes. returns "filename.png" for "C:\filename.png".
		End If
	End Function

	Public Function RemoveFileExtension(Filename As String) As String
		Return Filename.RemoveLastInstanceOf("." & Filename.Split(".")(Filename.Split.Length))
	End Function

	Public Function GetFileExtension(Filename As String) As String
		Return "." & Filename.Split(".")(Filename.Split(".").Length - 1)
	End Function

	Public Function ValidFileName(NameToCheck As String, Optional AllowPeriods As Boolean = False) As Boolean
		If Not IsNothing(NameToCheck) Then
			Dim disallowedCharacters As String() = {"/", "\", "<", ">", ":", "?", "*", ",", "|", "."}
			If AllowPeriods Then NameToCheck = NameToCheck.Replace(".", " ")
			For i = 0 To disallowedCharacters.Length - 1
				If NameToCheck.Contains(disallowedCharacters(i)) Then
					Return False
				End If
			Next
		Else
			Return False
		End If
		Return True
	End Function

	Public Function ProgramIcon() As Icon
		Return Icon.ExtractAssociatedIcon(Application.ExecutablePath)
	End Function

	<Extension()>
	Public Function RemoveLast(ByVal stringToModify As String, amountToRemove As Integer) As String
		Return stringToModify.Remove(stringToModify.Length - amountToRemove)
	End Function

	<Extension()>
	Public Function RemoveFirst(ByVal StringToModify As String, AmountToRemove As Integer) As String
		Return StrReverse(StrReverse(StringToModify).Remove(StringToModify.Length - AmountToRemove))
	End Function

	<Extension()>
	Public Function RemoveLastInstanceOf(ByVal StringToModify As String, StringToRemove As String) As String
		Return StrReverse(Replace(StrReverse(StringToModify), StrReverse(StringToRemove), "",, 1))
	End Function

	<Extension()>
	Function BatchRemove(ByRef StringToClean As String, TextToStrip As String()) As String
		Dim ReturnText As String = StringToClean
		For i = 0 To TextToStrip.Count - 1
			ReturnText = ReturnText.Replace(TextToStrip(i), "")
		Next
		Return ReturnText
	End Function

	Public Function RandomArrayObject(Array) As Object
		If IsNothing(Array) Then Return "===ERROR: Passed array is empty!==="
		Return Array(Rnd() * (Array.Length - 1))
	End Function

	Public Function RAO(Array) As Object
		Return RandomArrayObject(Array)
	End Function

	Public Function ValidURL(URL) As Boolean ' http://stackoverflow.com/a/3809435
		Dim URLCheckPattern As String = "(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)"
		Return Regex.IsMatch(URL, URLCheckPattern, RegexOptions.IgnoreCase) 'WARNING: returns true for invalid/missing TLDs (such as: invalidurl/file.html)
	End Function

	'******************
	'***END REUSABLE***
	'******************

	Public Function ReturnThemeSettingsAsThemeObject(chosenThemeIndex As String)
		Try
			chosenThemeIndex = CInt(chosenThemeIndex)
			If chosenThemeIndex > 2 Then chosenThemeIndex = 0

		Catch ex As Exception
			chosenThemeIndex = chosenThemeIndex.ToLower()
			If chosenThemeIndex.Contains("standard") Then
				chosenThemeIndex = 0
			ElseIf chosenThemeIndex.Contains("night") Or chosenThemeIndex.Contains("nite") Then
				chosenThemeIndex = 1
			Else
				chosenThemeIndex = 2
			End If
		End Try
		Dim themeName() As String = {"Standard", "Nite", "Tertiary"}

		Dim returnedTheme As Theme
		returnedTheme.NotificationCriticalBG = CallByName(My.Settings, themeName(chosenThemeIndex) & "NotifyColourCritical", CallType.Get)
		returnedTheme.NotificationWarningBG = CallByName(My.Settings, themeName(chosenThemeIndex) & "NotifyColourWarning", CallType.Get)
		returnedTheme.NotificationInfoBG = CallByName(My.Settings, themeName(chosenThemeIndex) & "NotifyColourInfo", CallType.Get)
		returnedTheme.NotificationCriticalFG = CallByName(My.Settings, themeName(chosenThemeIndex) & "NotifyTextColourCritical", CallType.Get)
		returnedTheme.NotificationWarningFG = CallByName(My.Settings, themeName(chosenThemeIndex) & "NotifyTextColourWarning", CallType.Get)
		returnedTheme.NotificationInfoFG = CallByName(My.Settings, themeName(chosenThemeIndex) & "NotifyTextColourInfo", CallType.Get)

		returnedTheme.SidebarFG = CallByName(My.Settings, themeName(chosenThemeIndex) & "SidebarFG", CallType.Get)
		returnedTheme.SidebarBG = CallByName(My.Settings, themeName(chosenThemeIndex) & "SidebarBG", CallType.Get)
		returnedTheme.StatusBarFG = CallByName(My.Settings, themeName(chosenThemeIndex) & "StatFG", CallType.Get)
		returnedTheme.StatusBarBG = CallByName(My.Settings, themeName(chosenThemeIndex) & "StatBG", CallType.Get)
		returnedTheme.TextBoxFG = CallByName(My.Settings, themeName(chosenThemeIndex) & "TextFG", CallType.Get)
		returnedTheme.TextBoxBG = CallByName(My.Settings, themeName(chosenThemeIndex) & "TextBG", CallType.Get)

		Return returnedTheme
	End Function

	Public Sub SetDefaultThemes()
		'********************
		'***DEFAULT THEMES***
		'********************

		'Standard:

		defaultStandardTheme.TextBoxBG = Color.White
		defaultStandardTheme.TextBoxFG = Color.Black
		defaultStandardTheme.SidebarBG = Color.White
		defaultStandardTheme.SidebarFG = Color.Black
		defaultStandardTheme.StatusBarBG = ControlColour
		defaultStandardTheme.StatusBarFG = Color.Black
		defaultStandardTheme.NotificationCriticalBG = Color.Red
		defaultStandardTheme.NotificationCriticalFG = Color.White
		defaultStandardTheme.NotificationWarningBG = Color.Orange
		defaultStandardTheme.NotificationWarningFG = Color.White
		defaultStandardTheme.NotificationInfoBG = Color.Green
		defaultStandardTheme.NotificationInfoFG = Color.White

		'Night:

		defaultNightTheme.TextBoxBG = Color.FromArgb(30, 30, 30)
		defaultNightTheme.TextBoxFG = Color.FromArgb(226, 226, 226)
		defaultNightTheme.SidebarBG = Color.FromArgb(30, 30, 30)
		defaultNightTheme.SidebarFG = Color.FromArgb(226, 226, 226)
		defaultNightTheme.StatusBarBG = Color.Black
		defaultNightTheme.StatusBarFG = Color.White
		defaultNightTheme.NotificationCriticalBG = Color.Black
		defaultNightTheme.NotificationCriticalFG = Color.Red
		defaultNightTheme.NotificationWarningBG = Color.Black
		defaultNightTheme.NotificationWarningFG = Color.Orange
		defaultNightTheme.NotificationInfoBG = Color.Black
		defaultNightTheme.NotificationInfoFG = Color.LimeGreen

		'Tertiary:

		defaultTertiaryTheme.TextBoxBG = Color.RoyalBlue
		defaultTertiaryTheme.TextBoxFG = Color.White
		defaultTertiaryTheme.SidebarBG = Color.RoyalBlue
		defaultTertiaryTheme.SidebarFG = Color.White
		defaultTertiaryTheme.StatusBarBG = ControlColour
		defaultTertiaryTheme.StatusBarFG = Color.Black
		defaultTertiaryTheme.NotificationCriticalBG = Color.Red
		defaultTertiaryTheme.NotificationCriticalFG = Color.White
		defaultTertiaryTheme.NotificationWarningBG = Color.Orange
		defaultTertiaryTheme.NotificationWarningFG = Color.White
		defaultTertiaryTheme.NotificationInfoBG = Color.Green
		defaultTertiaryTheme.NotificationInfoFG = Color.White
	End Sub

End Module