Imports System.Runtime.InteropServices
Imports System.Threading

Public Class mainForm

    Dim oHandle As System.IntPtr = Me.Handle.ToInt64

    Enum LogiPanelButton
        LOGI_UNDEFINED = -1
        LOGI_P1 = 1
        LOGI_P2 = 2
        LOGI_P3 = 3
        LOGI_P4 = 4
        LOGI_P5 = 5
        LOGI_P6 = 6
        LOGI_P7 = 7
        LOGI_P8 = 8
    End Enum

    Enum LogiColor
        LOGI_OFF = 0
        LOGI_GREEN = 1
        LOGI_AMBER = 2
        LOGI_RED = 3
    End Enum

    Public Function GetButton(ByVal oButton As LogiPanelButton) As Integer
        Dim iButton As Integer = CType(oButton, Integer)
        Return iButton
    End Function

    Public Function GetColor(ByVal oColor As LogiColor) As Integer
        Dim iColor As Integer = CType(oColor, Integer)
        Return iColor
    End Function

    Public Function GetVBColor(ByVal oColor As LogiColor) As Color
        Select Case oColor
            Case LogiColor.LOGI_OFF
                Return Color.LightGray
            Case LogiColor.LOGI_GREEN
                Return Color.LawnGreen
            Case LogiColor.LOGI_AMBER
                Return Color.Orange
            Case LogiColor.LOGI_RED
                Return Color.Red
            Case Else
                Return Color.Gray
        End Select
    End Function

    <DllImport("JoystickSDKDemo.dll", CallingConvention:=CallingConvention.StdCall, EntryPoint:="ButtonSetColor")> _
    Public Shared Sub ButtonSetColor(ByVal oHandle As System.IntPtr, ByVal iButton As Integer, ByVal iColor As Integer)
    End Sub

    <DllImport("JoystickSDKDemo.dll", CallingConvention:=CallingConvention.StdCall, EntryPoint:="ButtonIsColor")> _
    Public Shared Function ButtonIsColor(ByVal oHandle As System.IntPtr, ByVal iButton As Integer, ByVal iColor As Integer) As Boolean
    End Function

    <DllImport("JoystickSDKDemo.dll", CallingConvention:=CallingConvention.StdCall, EntryPoint:="ButtonsSetAllColor")> _
    Public Shared Sub ButtonsSetAllColor(ByVal oHandle As System.IntPtr, ByVal iColor As Integer)
    End Sub

    Private Sub allgreenButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles allgreenButton.Click
        Try
            Console.WriteLine("All Green Button Clicked: " & DateTime.Now)
            ButtonsSetAllColor(oHandle, GetColor(LogiColor.LOGI_GREEN))
            Console.WriteLine("All Green Finished: " & DateTime.Now)
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message & vbCrLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub allamberButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles allamberButton.Click
        Try
            Console.WriteLine("All Amber Button Clicked: " & DateTime.Now)
            ButtonsSetAllColor(oHandle, GetColor(LogiColor.LOGI_AMBER))

            Console.WriteLine("All Amber Finished: " & DateTime.Now)
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message & vbCrLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub allredButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles allredButton.Click
        Try
            ButtonsSetAllColor(oHandle, GetColor(LogiColor.LOGI_RED))
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message & vbCrLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub alloffButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles alloffButton.Click
        Try
            ButtonsSetAllColor(oHandle, GetColor(LogiColor.LOGI_OFF))
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message & vbCrLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        p1Label.BackColor = GetVBColor(LogiColor.LOGI_OFF)
        p2Label.BackColor = GetVBColor(LogiColor.LOGI_OFF)
        p3Label.BackColor = GetVBColor(LogiColor.LOGI_OFF)
        p4Label.BackColor = GetVBColor(LogiColor.LOGI_OFF)
        p5Label.BackColor = GetVBColor(LogiColor.LOGI_OFF)
        p6Label.BackColor = GetVBColor(LogiColor.LOGI_OFF)
        p7Label.BackColor = GetVBColor(LogiColor.LOGI_OFF)
        p8Label.BackColor = GetVBColor(LogiColor.LOGI_OFF)

    End Sub

    Private Sub p1Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p1Label.Click
        selectedbuttonTextBox.Text = 1
    End Sub

    Private Sub p2Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p2Label.Click
        selectedbuttonTextBox.Text = 2
    End Sub

    Private Sub p3Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p3Label.Click
        selectedbuttonTextBox.Text = 3
    End Sub

    Private Sub p4Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p4Label.Click
        selectedbuttonTextBox.Text = 4
    End Sub

    Private Sub p5Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p5Label.Click
        selectedbuttonTextBox.Text = 5
    End Sub

    Private Sub p6Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p6Label.Click
        selectedbuttonTextBox.Text = 6
    End Sub

    Private Sub p7Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p7Label.Click
        selectedbuttonTextBox.Text = 7
    End Sub

    Private Sub p8Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p8Label.Click
        selectedbuttonTextBox.Text = 8
    End Sub

    Private Sub setcolorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setcolorButton.Click

        Try
            Dim iButton As Integer
            Dim oColor As LogiColor

            If Integer.TryParse(selectedbuttonTextBox.Text, iButton) Then

                If offRadioButton.Checked = True Then
                    oColor = LogiColor.LOGI_OFF
                ElseIf greenRadioButton.Checked = True Then
                    oColor = LogiColor.LOGI_GREEN
                ElseIf amberRadionButton.Checked = True Then
                    oColor = LogiColor.LOGI_AMBER
                ElseIf redRadioButton.Checked = True Then
                    oColor = LogiColor.LOGI_RED
                End If

                ButtonSetColor(Me.Handle, iButton, GetColor(oColor))

            Else
                MessageBox.Show("Select a button to change color.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message & vbCrLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub getledsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getledsButton.Click

    End Sub

    Private Sub setledsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setledsButton.Click
        Try
            Dim sValue As String = "80"





        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message & vbCrLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub iscolorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles iscolorButton.Click
        Try
            Dim iButton As Integer
            Dim oColor As LogiColor

            If Integer.TryParse(selectedbuttonTextBox.Text, iButton) Then

                If offRadioButton.Checked = True Then
                    oColor = LogiColor.LOGI_OFF
                ElseIf greenRadioButton.Checked = True Then
                    oColor = LogiColor.LOGI_GREEN
                ElseIf amberRadionButton.Checked = True Then
                    oColor = LogiColor.LOGI_AMBER
                ElseIf redRadioButton.Checked = True Then
                    oColor = LogiColor.LOGI_RED
                End If

                Dim bIsColor As Boolean = ButtonIsColor(Me.Handle, iButton, GetColor(oColor))

                iscolorCheckBox.Checked = bIsColor

            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message & vbCrLf & ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class
