Imports System.Collections.ObjectModel
Imports EgtWPFLib5
Imports EgtUILib
Imports EgtBEAMWALL.Core
Imports System.Windows.Threading

Public Class MDIPageVM
   Inherits VMBase

   Private m_MDIResultTimer As New DispatcherTimer

   Private m_MDICommandColumns As New ObservableCollection(Of EgtDataGridColumn)
   Public Property MDICommandColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return m_MDICommandColumns
      End Get
      Set(value As ObservableCollection(Of EgtDataGridColumn))
         m_MDICommandColumns = value
      End Set
   End Property

   Private m_MDICommandList As List(Of MDICommand)
   Public Property MDICommandList As List(Of MDICommand)
      Get
         Return m_MDICommandList
      End Get
      Set(value As List(Of MDICommand))
         m_MDICommandList = value
      End Set
   End Property

   Private m_SelMDICommand As MDICommand
   Public Property SelMDICommand As MDICommand
      Get
         Return m_SelMDICommand
      End Get
      Set(value As MDICommand)
         m_SelMDICommand = value
         If Not IsNothing(m_SelMDICommand) Then
            m_sMDICommand_Text = m_SelMDICommand.sCommand
            NotifyPropertyChanged(NameOf(sMDICommand_Text))
         End If
      End Set
   End Property

   Private m_sMDICommand_Text As String
   Public Property sMDICommand_Text As String
      Get
         Return m_sMDICommand_Text
      End Get
      Set(value As String)
         If value <> m_sMDICommand_Text Then
            Dim nItem As MDICommand = m_MDICommandList.Find(Function(x) x.sCommand = value)
            If Not IsNothing(nItem) Then
               m_SelMDICommand = nItem
            Else
               m_SelMDICommand = Nothing
            End If
            NotifyPropertyChanged(NameOf(SelMDICommand))
            m_sMDICommand_Text = value
            ' se ancora indicato stato precedente lo annullo 
            If m_MDIResultTimer.IsEnabled Then MDIResultTimer_Tick()
         End If
      End Set
   End Property

   Private m_MDIText_Background As SolidColorBrush
   Public ReadOnly Property MDIText_Background As SolidColorBrush
      Get
         Return m_MDIText_Background
      End Get
   End Property

   ' Definizione comandi
   Private m_cmdSendMDI As ICommand

#Region "Messages"

   Public ReadOnly Property Command_Msg As String
      Get
         Return EgtMsg(62510)
      End Get
   End Property

   Public ReadOnly Property Description_Msg As String
      Get
         Return EgtMsg(62511)
      End Get
   End Property

#End Region    ' Messages

#Region "CONSTRUCTORS"

   Sub New()
      ' imposto riferimento in Map
      Map.SetRefMDIPageVM(Me)
      ' carico le colonne della datagrid
      GetPrivateProfileColumns(S_MDICOMMANDS, MDICommandColumns)
      ' leggo stringhe MDI
      GetPrivateProfileMDICommands(S_MDI, MDICommandList)
      m_MDIResultTimer.Interval = TimeSpan.FromMilliseconds(2000)
      AddHandler m_MDIResultTimer.Tick, AddressOf MDIResultTimer_Tick
   End Sub

   Private Sub GetPrivateProfileMDICommands(IpAppName As String, ByRef MDICommandList As List(Of MDICommand))
      MDICommandList = New List(Of MDICommand)
      Dim MDIIndex As Integer = 1
      Dim sMDICommand As String = ""
      While GetPrivateProfileString(IpAppName, MDIIndex, "", sMDICommand, CurrentMachine.sMachIniFile) > 0
         Dim MDICommand() As String = sMDICommand.Split(","c)
         If MDICommand.Length() >= 2 Then
            Dim nDescription As Integer
            If Integer.TryParse(MDICommand(1), nDescription) Then
               MDICommand(1) = MachMsg(MsgParagraphs.MDI, nDescription)
            End If
            MDICommandList.Add(New MDICommand(MDICommand(0), MDICommand(1)))
         End If
         MDIIndex += 1
      End While
   End Sub

#End Region    ' CONSTRUCTORS

#Region "METHODS"

   Friend Sub SendMDIResult(nResult As Integer)
      If nResult <> 0 Then
         m_MDIText_Background = Brushes.Red
      Else
         m_MDIText_Background = Brushes.Green
      End If
      NotifyPropertyChanged(NameOf(MDIText_Background))
      m_MDIResultTimer.Start()
   End Sub

   Private Sub MDIResultTimer_Tick()
      m_MDIResultTimer.Stop()
      m_MDIText_Background = Brushes.White
      NotifyPropertyChanged(NameOf(MDIText_Background))
   End Sub

#End Region    ' METHODS

#Region "COMMANDS"

#Region "SendMDI"

   ''' <summary>
   ''' Returns a command that do Open.
   ''' </summary>
   Public ReadOnly Property SendMDI_Command As ICommand
      Get
         If m_cmdSendMDI Is Nothing Then
            m_cmdSendMDI = New Command(AddressOf SendMDI)
         End If
         Return m_cmdSendMDI
      End Get
   End Property

   ''' <summary>
   ''' Execute the Open.  This method is invoked by the OpenCommand.
   ''' </summary>
   Friend Sub SendMDI()
      If Not String.IsNullOrWhiteSpace(m_sMDICommand_Text) Then
         MachManaging.AddToCommandList(ThreadCommand.CreateCommand(CommandTypes.MDI, m_sMDICommand_Text))
      End If
   End Sub

#End Region ' SendMDI

#End Region    ' COMMANDS

End Class

Public Class MDICommand

   Private m_sCommand As String
   Public Property sCommand As String
      Get
         Return m_sCommand
      End Get
      Set(value As String)
         m_sCommand = value
      End Set
   End Property

   Private m_sDescription As String
   Public Property sDescription As String
      Get
         Return m_sDescription
      End Get
      Set(value As String)
         m_sDescription = value
      End Set
   End Property

   Sub New(sCommand As String, sDescription As String)
      m_sCommand = sCommand
      m_sDescription = sDescription
   End Sub

End Class