
Public Enum PhoneState
    Unknown = 0
    NoLine = 1
    NoDevice = 2
    HookOff = 3
    HookOn = 4
    Ring = 5
End Enum

Public Class PhoneButton
    Inherits Control

    Private fState As PhoneState
    Public Property State As PhoneState
        Get
            Return fState
        End Get
        Set(value As PhoneState)
            If (value <> fState) Then
                fState = value
                fDialed = ""
                fTimerRing.Enabled = (State = PhoneState.Ring)
                ReDraw()
            End If
        End Set
    End Property

    Private fRecording As Boolean
    Public Property Recording As Boolean
        Get
            Return fRecording
        End Get
        Set(value As Boolean)
            If (value <> fRecording) Then
                fRecording = value
                ReDraw()
            End If
        End Set
    End Property

    Private fPlaying As Boolean
    Public Property Playing As Boolean
        Get
            Return fPlaying
        End Get
        Set(value As Boolean)
            If (value <> fPlaying) Then
                fPlaying = value
                ReDraw()
            End If
        End Set
    End Property

    Private fCallerID As String
    Public Property CallerID As String
        Get
            Return fCallerID
        End Get
        Set(value As String)
            If (value <> fCallerID) Then
                fCallerID = value
                fTimerCallerID.Enabled = (value <> "")
                ReDraw()
            End If
        End Set
    End Property

    Private fDialed As String
    Public Sub AddDialed(value As String)
        fDialed = fDialed + value

    End Sub

    Private fTimerRing As Timer
    Private Sub HandlerRing(sender As Object, e As EventArgs)
        If (fState = PhoneState.Ring) Then
            State = PhoneState.HookOn
        End If
    End Sub

    Private fTimerCallerID As Timer
    Private Sub HandlerCallerID(sender As Object, e As EventArgs)
        CallerID = ""
    End Sub


    Public Sub New(ByVal id As Integer, apanel As Panel)
        Parent = apanel
        BackColor = Color.BurlyWood
        ForeColor = Color.Black
        fState = PhoneState.Unknown
        fPlaying = False
        fRecording = False
        fDialed = ""
        fCallerID = ""
        Tag = id
        Name = "PhoneLine" + CStr(id)
        ReDraw()
        Visible = True

        fTimerCallerID = New Timer()
        fTimerCallerID.Enabled = False
        fTimerCallerID.Interval = 10000
        AddHandler fTimerCallerID.Tick, AddressOf HandlerCallerID

        fTimerRing = New Timer()
        fTimerRing.Enabled = False
        fTimerRing.Interval = 2000
        AddHandler fTimerRing.Tick, AddressOf HandlerRing

    End Sub

    Private Sub ReDraw()
        Dim s As String = ""
        Select Case fState
            Case PhoneState.NoDevice
                s = "No Device" + vbCrLf
            Case PhoneState.NoLine
                s = "No Line" + vbCrLf
            Case PhoneState.HookOff
                s = "Hook Off" + vbCrLf
            Case PhoneState.HookOn
                s = "Hook On" + vbCrLf
            Case PhoneState.Ring
                s = "Ring" + vbCrLf
            Case Else
                s = "Unknown" + vbCrLf
        End Select

        If (fRecording) Then
            s = s + "Recording: Yes" + vbCrLf
        Else
            s = s + "Recording: No" + vbCrLf
        End If
        If (fPlaying) Then
            s = s + "Playing: Yes" + vbCrLf
        Else
            s = s + "Playing: No" + vbCrLf
        End If

        s = s + fCallerID + vbCrLf
        s = s + fDialed + vbCrLf

        Text = s
        Refresh()
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        'MyBase.OnPaint(e)
        e.Graphics.FillRectangle(New SolidBrush(BackColor), RectangleF.op_Implicit(ClientRectangle))
        e.Graphics.DrawString(Text, Font, New SolidBrush(ForeColor), RectangleF.op_Implicit(ClientRectangle))
    End Sub

End Class
