Imports System.Collections.Generic
Imports System.Windows.Threading
Imports Telerik.Windows.Controls

Public Class ExampleViewModel
    Inherits ViewModelBase
    Private Const queueCapacity As Integer = 30
    Private r As New Random()
    Private cpuData As New Queue(Of SystemLoadInfo)(queueCapacity)
    Private nowTime As New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second)
    Private timer1 As DispatcherTimer
    Private _data As Queue(Of SystemLoadInfo)
    Private _axisXMinValue As Double
    Private _axisXMaxValue As Double
    Private _axisXStep As Double

    Public Sub New()
        Me.SetUpTimer()
    End Sub

    Public Property Data() As Queue(Of SystemLoadInfo)
        Get
            Return Me._data
        End Get
        Set(ByVal value As Queue(Of SystemLoadInfo))
            If Me._data IsNot value Then
                Me._data = value
                Me.OnPropertyChanged("Data")
            End If
        End Set
    End Property

    Public Property AxisXMinValue() As Double
        Get
            Return Me._axisXMinValue
        End Get
        Set(ByVal value As Double)
            If Me._axisXMinValue <> value Then
                Me._axisXMinValue = value
                Me.OnPropertyChanged("AxisXMinValue")
            End If
        End Set
    End Property

    Public Property AxisXMaxValue() As Double
        Get
            Return Me._axisXMaxValue
        End Get
        Set(ByVal value As Double)
            If Me._axisXMaxValue <> value Then
                Me._axisXMaxValue = value
                Me.OnPropertyChanged("AxisXMaxValue")
            End If
        End Set
    End Property

    Public Property AxisXStep() As Double
        Get
            Return Me._axisXStep
        End Get
        Set(ByVal value As Double)
            If Me._axisXStep <> value Then
                Me._axisXStep = value
                Me.OnPropertyChanged("AxisXStep")
            End If
        End Set
    End Property

    Public Sub StartTimer()
        If timer1 IsNot Nothing Then
            timer1.Start()
        End If
    End Sub

    Public Sub StopTimer()
        If timer1 IsNot Nothing Then
            timer1.[Stop]()
        End If
    End Sub

    Private Sub SetUpTimer()
        timer1 = New DispatcherTimer()
        timer1.Interval = TimeSpan.FromMilliseconds(500)
        AddHandler timer1.Tick, AddressOf OnTimerTick
    End Sub

    Private Sub OnTimerTick(ByVal sender As Object, ByVal e As EventArgs)
        Me.nowTime = Me.nowTime.AddMilliseconds(500)

        Me.UpdateData(Me.nowTime)
        Me.SetUpAxisXRange(Me.nowTime)

        Me.Data = Nothing
        Me.Data = Me.cpuData
    End Sub

    Private Sub SetUpAxisXRange(ByVal now As DateTime)
        Me.AxisXMinValue = now.AddSeconds(-14.5).ToOADate()
        Me.AxisXMaxValue = now.ToOADate()
        Me.AxisXStep = 1 / 24 / 3600 / 2
    End Sub

    Private Sub UpdateData(ByVal now As DateTime)
        If Me.cpuData.Count >= queueCapacity Then
            Me.cpuData.Dequeue()
        End If

        Dim systemInfo As New SystemLoadInfo()

        systemInfo.CPULoad = r.NextDouble()
        systemInfo.MemUsage = r.[Next](100, 200)
        systemInfo.Time = now

        Me.cpuData.Enqueue(systemInfo)
    End Sub
End Class

