Public Class FormSample
    Dim Callback_pin As POS_CIDR.Callback

    Dim myPhones As List(Of PhoneButton) = New List(Of PhoneButton)

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown

        Dim di As String = ""
        Dim l As Integer = 0

        If (POS_CIDR.CIDR_GetInfo(di, l) = 0) Then
            MessageBox.Show("Failed to load DLL!")
            Close()
        End If

        AddLog("DLL Info: " + di)

        AddLog("Registering Callback")
        Callback_pin = AddressOf CIDR_Event
        GC.KeepAlive(Callback_pin)
        POS_CIDR.CIDR_RegisterEvent(Callback_pin, 5)
    End Sub

    Private Sub ButtonInfo_Click(sender As Object, e As EventArgs) Handles ButtonInfo.Click
        MessageBox.Show(POS_CIDR.CIDR_InfoStr())
    End Sub

    Public Sub AddLog(msg As String)
        Dim dt As String = DateTime.Now.ToString("HH:mm:ss.fff")
        ListBoxLog.Items.Add(dt + ": " + msg)
        ListBoxLog.SelectedIndex = ListBoxLog.Items.Count - 1
    End Sub

    Public Sub AddDescription(line As Integer, desc As String)
        Dim dt As String = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
        Dim item As ListViewItem = ListViewDescription.Items.Add(dt)
        If (line > 0) Then
            item.SubItems.Add(CStr(line))
        Else
            item.SubItems.Add("SYS")
        End If
        item.SubItems.Add(desc)
        item.EnsureVisible()
    End Sub

    Public Sub ReArrangeButtons()
        If (myPhones.Count = 0) Then
            Return
        End If

        Dim h As Integer = PanelPhones.ClientRectangle.Height - 10
        Dim y As Integer = (PanelPhones.ClientRectangle.Height - h) / 2
        Dim stp As Integer = PanelPhones.ClientRectangle.Width / myPhones.Count
        Dim w As Integer = stp - 10
        Dim x As Integer = (w - stp) / 2 + 10

        Dim i As Integer
        For i = 0 To myPhones.Count - 1
            myPhones(i).SetBounds(x, y, w, h)
            x += stp
            myPhones(i).Refresh()
        Next
    End Sub

    Private Sub PanelPhones_SizeChanged(sender As Object, e As EventArgs) Handles PanelPhones.SizeChanged
        ReArrangeButtons()
    End Sub

    Public Sub CIDR_Event(ByVal line As Integer, msg As String)
        AddLog("[" + CStr(line) + "] " + msg)

        If (line = 0) Then ' System message

            Dim di As String = ""
            Dim lines As Integer = 0
            POS_CIDR.CIDR_GetInfo(di, lines)
            While (myPhones.Count < lines)
                Dim ph As PhoneButton = New PhoneButton(myPhones.Count + 1, PanelPhones)
                AddHandler ph.MouseUp, AddressOf PohoneButton_MouseUp
                myPhones.Add(ph)
            End While
            ReArrangeButtons()

        ElseIf (msg.Length > 0)    'line Event
            Dim code As String = msg.Substring(0, 1)
            Dim phone As PhoneButton = myPhones(line - 1)
            Dim stopSave As Boolean = False
            Dim temp As String
            Dim temp2 As String
            Dim description As String = ""

            Select Case code
                Case "C"    ' شماره تماس گیرنده
                    temp = msg.Substring(1, msg.IndexOf(",") - 1)
                    'temp  شمازه
                    phone.CallerID = temp
                    temp2 = msg.Substring(msg.IndexOf(",") + 1, 8)
                    'temp2  تاریخ و ساعت
                    temp2 = temp2.Insert(6, ":").Insert(4, " ").Insert(2, "/")
                    description = "شماره تماس گیرنده" + temp + " تاریخ و ساعت مخابرات " + temp2

                Case "D"    'فشرده شدن دکمه
                    temp = msg.Substring(1, 1)
                    description = " دکمه فشرده شده " + temp
                    phone.AddDialed(temp)

                Case "X"    'مسیر تلفن قطع است
                    phone.State = PhoneState.NoLine
                    stopSave = True
                    description = "خط تلفن قطع است"

                Case "Q"    'ارتباط دستگاه قطع است
                    phone.State = PhoneState.NoDevice
                    stopSave = True
                    description = "دستگاه مربوطه متصل نیست"

                Case "F"    'گوشی برداشته شده است
                    phone.State = PhoneState.HookOff
                    If (CheckAutoRecord.Checked) And (Not phone.Recording) Then
                        SaveLine(line)
                    End If
                    description = "گوشی برداشته شده است"

                Case "N"    'گوشی گذاشته شده است
                    If (phone.State = PhoneState.HookOff) Then
                        temp = msg.Substring(1, msg.IndexOf("D") - 1)
                        'temp زمان برداشته شدن گوشی بر حسب ثانیه
                        temp2 = msg.Substring(msg.IndexOf("D") + 1)
                        'temp2 دکمه های فشرده شده
                        description = " گوشی پس از" + temp + " ثانیه "
                        If (temp2.Length > 0) Then
                            description = description + " و شماره گیری " + temp2
                        End If
                        description = description + " گذاشته شده است"

                    Else
                        description = " گوشی گذاشته شده است"
                    End If
                    phone.State = PhoneState.HookOn
                    stopSave = True

                Case "R"    'زنگ خوردن
                    phone.State = PhoneState.Ring
                    temp = msg.Substring(1)
                    'temp تعداد زنگها
                    description = "در حال زنگ خوردن " + temp + "بار "

                Case "I"    'اطلاعات در مورد ذخیره سازی
                    description = "hich!"
                    temp = msg.Substring(1, 5).ToUpper()
                    If (temp = "SAVES") Then    'stop
                        phone.Recording = False
                        description = "ذخیره سازی متوقف شده است"
                    ElseIf (temp = "SAVE ")
                        phone.Recording = True
                        temp = msg.Substring(6)
                        description = "ذخیره سازی شروع شده است " + temp
                    End If
            End Select

            If (description.Length > 0) Then
                AddDescription(line, description)
            End If

            If (CheckAutoRecord.Checked) And (stopSave) Then
                POS_CIDR.CIDR_SaveLine(line, "", "")    'توقف ضبط صدا
            End If

        End If

    End Sub

    Private Sub SaveLine(line As Integer)
        Dim fname As String
        fname = AppDomain.CurrentDomain.BaseDirectory + "تماسها" + "\" _
                + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") _
                + " [" + CStr(line) + "]" + ".mp3"
        'Save Line
        POS_CIDR.CIDR_SaveLine(line, fname, "")
    End Sub

    Private Sub PohoneButton_MouseUp(sender As Object, e As MouseEventArgs)
        If (TypeOf sender Is PhoneButton) Then
            Dim phone As PhoneButton = sender
            If (e.Button = MouseButtons.Left) Then
                Dim k As Integer
                If (phone.Playing) Then
                    k = 0
                Else
                    k = phone.Tag
                End If
                Dim i As Integer
                For i = 1 To myPhones.Count
                    myPhones(i - 1).Playing = False
                Next

                If (POS_CIDR.CIDR_PlayLine(k) > 0) And (k > 0) Then
                    phone.Playing = True
                End If
            ElseIf (e.Button = MouseButtons.Right) Then
                Dim k As Integer
                k = phone.Tag
                If (phone.Recording) Then
                    POS_CIDR.CIDR_SaveLine(k, "", "")
                    phone.Recording = False
                Else
                    SaveLine(k)
                End If
            End If
        End If
    End Sub
End Class
