
Imports DirectShowLib
Imports System.Math

Public Class frmLandmarkConfiguration
    Sub frmVideoConfigure_Load() Handles Me.Load
        Try
            Me.Visible = True
            subPopulateCameraList()
            mnuVidSelectComboBox.SelectedIndex = intVideoSource
            frmVideo.subInitializeCapture()
            subConfigureMenu()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in frmVideoConfigure_Load :  " & ex.ToString)
        End Try
    End Sub



    Private Sub me_exit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
        Me.Visible = False
    End Sub


    Private Sub OK_Button_Click(sender As Object, e As EventArgs) Handles OK_Button.Click
        Try
            Me.Visible = False
            strStatusDriving = ""
            Me.Close()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in OK_Button_Click :  " & ex.ToString)
        End Try

    End Sub

    Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click
        Try
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Dim tbpTempPose As typeBotPose = tbpBotPose ' make sure we aren't throwing away important stuff!
            frmGotABotTop.mnuLoadSettings_Click(sender, e)
            tbpBotPose = tbpTempPose
            strStatusDriving = ""
            Me.Close()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in Cancel_Button_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub mnuFOV_Click(sender As Object, e As EventArgs) Handles mnuFOV.Click
        Try
            Dim strCameraFOV As String
            Dim sngDiagonalFOV As Single = Sqrt(sngCameraFOV ^ 2 + (sngCameraFOV * ticCameraResolution.Y / ticCameraResolution.X) ^ 2)
            strCameraFOV = InputBox("Enter Camera Diagonal Field Of View ", "Camera FOV", Str(sngDiagonalFOV))
            If strCameraFOV <> "" And IsNumeric(strCameraFOV) Then
                sngCameraFOV = Convert.ToSingle(strCameraFOV) * ticCameraResolution.X / Sqrt(ticCameraResolution.X ^ 2 + ticCameraResolution.Y ^ 2)
            End If
            boolSettingsChanged = True
            subConfigureMenu()
            mnuCamera.ShowDropDown()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuFOV_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub mnuVisualLocalizationTime_Click(sender As Object, e As EventArgs) Handles mnuVisualLocalizationTime.Click
        Try
            Dim strRecognitionTime As String
            strRecognitionTime = InputBox("Enter the time (in seconds) the robot will look for Land Marks (Longer gives better accuracy) ", "Localization Time", Str(sngLocalizationTime))
            If strRecognitionTime <> "" And IsNumeric(strRecognitionTime) Then
                sngLocalizationTime = Convert.ToSingle(strRecognitionTime)
            End If
            boolSettingsChanged = True
            subConfigureMenu()
            mnuMisc.ShowDropDown()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuVisualLocalizationTime_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub mnuEnableRCC_Click(sender As Object, e As EventArgs) Handles mnuEnableRCC.Click
        Try
            mnuEnableRCC.Checked = Not mnuEnableRCC.Checked
            boolEnableRcc = mnuEnableRCC.Checked
            boolSettingsChanged = True
            subConfigureMenu()
            mnuMisc.ShowDropDown()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuEnableRCC_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub subConfigureMenu() Handles mnuCamera.Click, mnuMisc.Click, mnuLandmarks.Click
        Try
            mnuEnableRCC.Checked = boolEnableRcc
            mnuVidSelectComboBox.Text = mnuVidSelectComboBox.Items.Item(intVideoSource).ToString
            mnuResolutionSelectComboBox.Text = Str(ticCameraResolution.X) & "x" & Str(ticCameraResolution.Y)
            mnuFOV.Text = "Camera FOV                 (" & FormatNumber(sngCameraFOV * Sqrt(ticCameraResolution.X ^ 2 + ticCameraResolution.Y ^ 2) / ticCameraResolution.X, 1) & ")"
            updnOffsetX.Value = sngCameraOffset
            mnuVisualLocalizationTime.Text = "Visual Localization Time  (" & sngLocalizationTime & ")"
            updnOffsetX.Value = sngCameraOffset
            mnuCameraOffsetDegrees.Text = "Camera Offset Degrees   (" & sngCameraOffsetDegrees & ")"
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subConfigureMenu :  " & ex.ToString)
        End Try
    End Sub

    Private Sub mnuCamera_Click(sender As Object, e As EventArgs) Handles mnuCamera.Click
        Try
            mnuCamera.ShowDropDown()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuCamera_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub mnuMisc_Click(sender As Object, e As EventArgs) Handles mnuMisc.Click
        Try
            mnuMisc.ShowDropDown()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuMisc_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub mnuLandmarks_Click(sender As Object, e As EventArgs) Handles mnuLandmarks.Click
        Try
            frmLandMarkEdit.Visible = Not frmLandMarkEdit.Visible
            boolViewLM = frmLandMarkEdit.Visible
            frmLandMarkEdit.Focus()
            mnuLandmarks.ShowDropDown()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuLandmarks_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub updnOffsetX_ValueChanged(sender As Object, e As EventArgs) Handles updnOffsetX.ValueChanged
        Try
            If updnOffsetX.Focused Then
                sngCameraOffset = updnOffsetX.Value
                boolSettingsChanged = True
                subConfigureMenu()
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in updnOffsetX_ValueChanged :  " & ex.ToString)
        End Try
    End Sub

    Function ProcessesRunning(ByVal ProcessName As String) As Integer
        '  Generalized Function to Check if an Instance of Application is running in the machine
        Try
            Return Process.GetProcessesByName(ProcessName).GetUpperBound(0) + 1
        Catch ex As Exception
            Return 0
            frmDebug.subWriteLogs(" EXCEPTION in ProcessesRunning :  " & ex.ToString)
        End Try
    End Function


    Private Sub mnuCameraOffsetDegrees_Click(sender As Object, e As EventArgs) Handles mnuCameraOffsetDegrees.Click
        Try
            Dim strCameraOffset As String = InputBox("Enter Camera Offset in degrees ", "Camera Offset", Str(sngCameraOffsetDegrees))
            If strCameraOffset <> "" And IsNumeric(strCameraOffset) Then
                sngCameraOffsetDegrees = Convert.ToSingle(strCameraOffset)
            End If
            boolSettingsChanged = True
            subConfigureMenu()
            mnuCamera.ShowDropDown()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuCameraOffsetDegrees_Click :  " & ex.ToString)
        End Try
    End Sub
    Private Sub mnuVidSelectComboBox_Click(sender As Object, e As EventArgs) Handles mnuVidSelectComboBox.Click
        Try
            mnuVidSelectComboBox.Items.Clear()
            subPopulateCameraList()
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuVidSelectComboBox_Click :  " & ex.ToString)
        End Try
    End Sub

    Private Sub VidSelectComboBox1_Change(sender As System.Object, e As System.EventArgs) Handles mnuVidSelectComboBox.TextChanged
        Try
            If mnuVidSelectComboBox.Focused And (boolEnableFaceRecognition Or boolEnableVideo) Then
                intVideoSource = Val(mnuVidSelectComboBox.SelectedIndex)
                mnuVidSelectComboBox.Text = "Video Device : " & mnuVidSelectComboBox.Items.Item(intVideoSource).ToString
                boolSettingsChanged = True
                frmVideo.subInitializeCapture()
                Me.mnuCamera.HideDropDown()
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in VidSelectComboBox1_Change :  " & ex.ToString)
        End Try
    End Sub


    Private Sub mnuResolutionSelectComboBox_Click(sender As Object, e As EventArgs) Handles mnuResolutionSelectComboBox.TextChanged
        Try
            If mnuResolutionSelectComboBox.Focused Then
                Dim intResolutionSelected As Integer = Val(mnuResolutionSelectComboBox.SelectedIndex)
                If mnuResolutionSelectComboBox.Items.Item(intResolutionSelected) IsNot Nothing Then
                    Dim strResolution As String = mnuResolutionSelectComboBox.Items.Item(intResolutionSelected)
                    Dim strParsedText As String() = strResolution.Split("x"c)
                    ticCameraResolution.X = Convert.ToInt16(strParsedText(0))
                    ticCameraResolution.Y = Convert.ToInt16(strParsedText(1))
                    frmVideo.subInitializeCapture()
                    boolSettingsChanged = True
                    subConfigureMenu()
                    mnuCamera.ShowDropDown()
                End If
            End If
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in mnuResolutionSelectComboBox_Click :  " & ex.ToString)
        End Try
    End Sub

    Sub subPopulateCameraList()
        Try
            Dim _SystemCameras As DsDevice() = DsDevice.GetDevicesOfCat(DirectShowLib.FilterCategory.VideoInputDevice)
            Dim _DeviceIndex As Integer = 0
            For Each _Camera As DirectShowLib.DsDevice In _SystemCameras
                mnuVidSelectComboBox.Items.Add(New KeyValuePair(Of Integer, String)(_DeviceIndex, _Camera.Name))
                _DeviceIndex += 1
            Next
            Dim listAvailableResolutions As List(Of String) = frmVideo.ftnGetAllAvailableResolution(_SystemCameras(intVideoSource))
            mnuResolutionSelectComboBox.Items.Clear()
            For Each strResolution As String In listAvailableResolutions
                mnuResolutionSelectComboBox.Items.Add(strResolution)
            Next
        Catch ex As Exception
            frmDebug.subWriteLogs(" EXCEPTION in subPopulateCameraList :  " & ex.ToString)
        End Try
    End Sub
End Class