Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports EgtBEAMWALL.Core
Imports EgtBEAMWALL.Core.ConstBeam
Imports EgtUILib
Imports EgtWPFLib5

Public MustInherit Class NewOpenProjectFileDialogVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Public Enum BTLDateTypes As Integer
      CREATEDATE = 0
      EXPORTDATE = 1
   End Enum

   Private Enum FilterTypes As Integer
      NULL = 0
      ID = 1
      BTLFILENAME = 2
      LISTNAME = 3
      MACHINE = 4
   End Enum

   Public Enum DayTypes As Integer
      LASTMONTH = 0
      LAST3MONTHS = 1
      LAST6MONTHS = 2
      PERIOD = 3
      ALWAYS = 4
   End Enum

   Public Enum RowQuantities As Integer
      FIFTY = 0
      HUNDRED = 1
      HUNDREDANDFIFTY = 2
   End Enum

   Friend Event m_CloseWindow(bDialogResult As Boolean)

   Protected m_ProdColumns As New ObservableCollection(Of EgtDataGridColumn)
   Public Property ProdColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return m_ProdColumns
      End Get
      Set(value As ObservableCollection(Of EgtDataGridColumn))
         m_ProdColumns = value
      End Set
   End Property

   Private m_colProdFile_Name As EgtDataGridColumn
   Public ReadOnly Property colProdFile_Name As EgtDataGridColumn
      Get
         Return m_colProdFile_Name
      End Get
   End Property

   Private m_colArchived_Name As EgtDataGridColumn
   Public ReadOnly Property colArchived_Name As EgtDataGridColumn
      Get
         Return m_colArchived_Name
      End Get
   End Property

   Protected m_ProjColumns As New ObservableCollection(Of EgtDataGridColumn)
   Public Property ProjColumns As ObservableCollection(Of EgtDataGridColumn)
      Get
         Return m_ProjColumns
      End Get
      Set(value As ObservableCollection(Of EgtDataGridColumn))
         m_ProjColumns = value
      End Set
   End Property

   Protected m_ProjectType As ProjectType

   Protected m_sFilter As String
   Public Property Filter As String
      Get
         Return m_sFilter
      End Get
      Set(value As String)
         m_sFilter = value
      End Set
   End Property

   Protected m_sFileNameFilter As Predicate(Of String)
   Public Property FileNameFilter As Predicate(Of String)
      Get
         Return m_sFileNameFilter
      End Get
      Set(value As Predicate(Of String))
         m_sFileNameFilter = value
      End Set
   End Property

   Protected m_sExtensions As New List(Of String)
   Public ReadOnly Property Extensions As List(Of String)
      Get
         Return m_sExtensions
      End Get
   End Property

   Public m_ProjectList_View As CollectionView = Nothing
   Protected m_ProjectList As New ObservableCollection(Of ProdItem)
   Public ReadOnly Property ProjectList As ObservableCollection(Of ProdItem)
      Get
         Return m_ProjectList
      End Get
   End Property

   Private m_nSelProjIndex As Integer
   Public ReadOnly Property nSelProjIndex As Integer
      Get
         Return m_nSelProjIndex
      End Get
   End Property

   Protected m_SelProject As ProdItem
   Public Property SelProject As ProdItem
      Get
         Return m_SelProject
      End Get
      Set(value As ProdItem)
         m_SelProject = value
         ' verifico se abilitare il bottone Delete
         If IsNothing(m_SelProject) Then
            SetDeleteIsEnabled(False)
            SetArchivedIsEnabled(False)
         ElseIf IsNothing(m_SelProject.ProdFileVM) Then
            SetDeleteIsEnabled(True)
            SetArchivedIsEnabled(False)
         Else
            SetDeleteIsEnabled(Not SelProject.ProdFileVM.bIsProduced)
            SetArchivedIsEnabled(True)
         End If
         NotifyPropertyChanged(NameOf(Archive_Msg))
      End Set
   End Property

   Protected m_FileName As String
   Public Property FileName As String
      Get
         Return m_FileName
      End Get
      Set(value As String)
         m_FileName = value
      End Set
   End Property

   Private m_bFixedProjectList As Boolean
   Public ReadOnly Property bFixedProjectList As Boolean
      Get
         Return m_bFixedProjectList
      End Get
   End Property
   Public Sub SetFixedProjectList(bValue As Boolean)
      m_bFixedProjectList = bValue
      NotifyPropertyChanged(NameOf(Filters_Visibility))
   End Sub

   Public ReadOnly Property Filters_Visibility As Visibility
      Get
         Return If(m_bFixedProjectList, Visibility.Collapsed, Visibility.Visible)
      End Get
   End Property

   Protected m_BTLDateTypeList As New List(Of String)
   Public ReadOnly Property BTLDateTypeList As List(Of String)
      Get
         Return m_BTLDateTypeList
      End Get
   End Property

   Protected m_SelBTLDateType As BTLDateTypes = Nothing
   Public Property SelBTLDateType As Integer
      Get
         Return m_SelBTLDateType
      End Get
      Set(value As Integer)
         ' aggiorno lista da Db
         RefreshProjectList()
         m_ProjectList_View.Refresh()
      End Set
   End Property

   Private m_DayTypeList As New List(Of String)({EgtMsg(62532), EgtMsg(62533), EgtMsg(62534), EgtMsg(62531)})
   Public Overridable ReadOnly Property DayTypeList As List(Of String)
      Get
         Return m_DayTypeList
      End Get
   End Property

   Protected m_SelDayType As DayTypes = DayTypes.LASTMONTH
   Public Property SelDayType As Integer
      Get
         Return m_SelDayType
      End Get
      Set(value As Integer)
         Dim PrevSelDayType As DayTypes = m_SelDayType
         m_SelDayType = value
         WriteMainPrivateProfileString(S_OPENFILEDIALOG, K_DAYTYPE, Convert.ToInt32(m_SelDayType))
         m_Date_Visibility = If(m_SelDayType = DayTypes.PERIOD, Visibility.Visible, Visibility.Collapsed)
         If m_SelDayType = DayTypes.PERIOD Then
            Select Case PrevSelDayType
               Case DayTypes.LASTMONTH
                  m_dtEndDate = DateTime.Today + TimeSpan.FromDays(1)
                  m_dtStartDate = m_dtEndDate - TimeSpan.FromDays(30.5)
               Case DayTypes.LAST3MONTHS
                  m_dtEndDate = DateTime.Today + TimeSpan.FromDays(1)
                  m_dtStartDate = m_dtEndDate - TimeSpan.FromDays(30.5 * 3)
               Case DayTypes.LAST6MONTHS
                  m_dtEndDate = DateTime.Today + TimeSpan.FromDays(1)
                  m_dtStartDate = m_dtEndDate - TimeSpan.FromDays(30.5 * 6)
               Case DayTypes.ALWAYS
                  m_dtEndDate = DateTime.Today + TimeSpan.FromDays(1)
                  m_dtStartDate = New DateTime(2020, 1, 1)
            End Select
            NotifyPropertyChanged(NameOf(dtStartDate))
            NotifyPropertyChanged(NameOf(dtEndDate))
         End If
         NotifyPropertyChanged(NameOf(Date_Visibility))
         ' aggiorno lista da Db
         RefreshProjectList()
         m_ProjectList_View.Refresh()
      End Set
   End Property

   Private m_dtStartDate As DateTime = DateTime.Today
   Public Property dtStartDate As DateTime
      Get
         Return m_dtStartDate
      End Get
      Set(value As DateTime)
         m_dtStartDate = value
         Dim lStartDate As Long = m_dtStartDate.ToFileTimeUtc()
         WriteMainPrivateProfileString(S_OPENFILEDIALOG, K_STARTDATE, lStartDate.ToString())
         ' aggiorno lista da Db
         RefreshProjectList()
         m_ProjectList_View.Refresh()
      End Set
   End Property

   Private m_dtEndDate As DateTime = DateTime.Today
   Public Property dtEndDate As DateTime
      Get
         Return m_dtEndDate
      End Get
      Set(value As DateTime)
         m_dtEndDate = value
         Dim lEndDate As Long = m_dtEndDate.ToFileTimeUtc()
         WriteMainPrivateProfileString(S_OPENFILEDIALOG, K_ENDDATE, lEndDate.ToString())
         ' aggiorno lista da Db
         RefreshProjectList()
         m_ProjectList_View.Refresh()
      End Set
   End Property

   Private m_Date_Visibility As Visibility = Visibility.Collapsed
   Public ReadOnly Property Date_Visibility As Visibility
      Get
         Return m_Date_Visibility
      End Get
   End Property

   Private m_RowQuantityList As New List(Of String)({50, 100, 150})
   Public ReadOnly Property RowQuantityList As List(Of String)
      Get
         Return m_RowQuantityList
      End Get
   End Property

   Private m_SelRowQuantity As RowQuantities
   Public Property SelRowQuantity As Integer
      Get
         Return m_SelRowQuantity
      End Get
      Set(value As Integer)
         m_SelRowQuantity = value
         RefreshProjectList()
         m_ProjectList_View.Refresh()
      End Set
   End Property

   ' lista campi per ricerca testuale e con datepicker
   Protected m_FilterTypeList As New ObservableCollection(Of IdNameStruct)
   Public ReadOnly Property FilterTypeList As ObservableCollection(Of IdNameStruct)
      Get
         Return m_FilterTypeList
      End Get
   End Property

   Protected m_SelFilterType As IdNameStruct = Nothing
   Public Property SelFilterType As IdNameStruct
      Get
         Return m_SelFilterType
      End Get
      Set(value As IdNameStruct)
         ' resetto il contenuto della barra di ricerca testuale
         m_SearchText = ""
         NotifyPropertyChanged(NameOf(SearchText))
         ' resetto le selezioni sul calendario
         If Not IsNothing(SearchDate) Then SearchDate.Clear()
         ' setto il Tipo Filtro a NULL e refresho la lista dei progetti (in modo da averli visibili tutti)
         m_SelFilterType = m_FilterTypeList(0)
         m_ProjectList_View.Refresh()
         ' ora setto il valore scelto come Tipo Filtro
         m_SelFilterType = value
         Select Case m_SelFilterType.Id
            Case FilterTypes.ID, FilterTypes.BTLFILENAME, FilterTypes.LISTNAME, FilterTypes.MACHINE
               SearchText_Visibility = Visibility.Visible
               SearchDate_Visibility = Visibility.Collapsed
            Case BTLDateTypes.EXPORTDATE, BTLDateTypes.CREATEDATE
               SearchText_Visibility = Visibility.Collapsed
               SearchDate_Visibility = Visibility.Visible
            Case Else
               SearchText_Visibility = Visibility.Collapsed
               SearchDate_Visibility = Visibility.Collapsed
         End Select
      End Set
   End Property

   Protected m_SearchText As String = ""
   Public Property SearchText As String
      Get
         Return m_SearchText
      End Get
      Set(value As String)
         m_SearchText = value
         m_ProjectList_View.Refresh()
      End Set
   End Property

   Friend m_SearchDate As SelectedDatesCollection = Nothing
   Public ReadOnly Property SearchDate As SelectedDatesCollection
      Get
         Return m_SearchDate
      End Get
   End Property

   Protected m_SearchText_Visibility As Visibility = Visibility.Collapsed
   Public Property SearchText_Visibility As Visibility
      Get
         Return m_SearchText_Visibility
      End Get
      Set(value As Visibility)
         m_SearchText_Visibility = value
         NotifyPropertyChanged(NameOf(SearchText_Visibility))
      End Set
   End Property

   Protected m_SearchDate_Visibility As Visibility = Visibility.Collapsed
   Public Property SearchDate_Visibility As Visibility
      Get
         Return m_SearchDate_Visibility
      End Get
      Set(value As Visibility)
         m_SearchDate_Visibility = value
         NotifyPropertyChanged(NameOf(SearchDate_Visibility))
      End Set
   End Property

   Public Property bIsEditNameActive As Boolean
      Get
         Return Not m_colProdFile_Name.IsReadOnly
      End Get
      Set(value As Boolean)
         If Not IsNothing(m_colProdFile_Name) Then
            m_colProdFile_Name.IsReadOnly = Not value
         End If
      End Set
   End Property
   Public Sub SetIsEditNameActive(value As Boolean)
      If Not IsNothing(m_colProdFile_Name) Then
         m_colProdFile_Name.IsReadOnly = Not value
      End If
      NotifyPropertyChanged(NameOf(bIsEditNameActive))
   End Sub

   Private m_bViewArchived As Boolean
   Public Property bViewArchived As Boolean
      Get
         Return m_bViewArchived
      End Get
      Set(value As Boolean)
         m_bViewArchived = value
         WriteMainPrivateProfileString(S_OPENFILEDIALOG, K_VIEWARCHIVED, If(bViewArchived, 1, 0))
         If Not IsNothing( m_colArchived_Name) Then
             m_colArchived_Name.Visible = value
             RefreshProjectList()
         End If
      End Set
   End Property

   Private m_bArchived_IsEnabled As Boolean
   Public ReadOnly Property bArchived_IsEnabled As Boolean
      Get
         Return m_bArchived_IsEnabled
      End Get
   End Property
   Public Sub SetArchivedIsEnabled(bValue As Boolean)
      m_bArchived_IsEnabled = bValue
      NotifyPropertyChanged(NameOf(bArchived_IsEnabled))
   End Sub

   Private m_bDelete_IsEnabled As Boolean
   Public ReadOnly Property bDelete_IsEnabled As Boolean
      Get
         Return m_bDelete_IsEnabled
      End Get
   End Property
   Public Sub SetDeleteIsEnabled(bValue As Boolean)
      m_bDelete_IsEnabled = bValue
      NotifyPropertyChanged(NameOf(bDelete_IsEnabled))
   End Sub

   Protected m_ChangeOpenedProjectOnCancel As Boolean = False

#Region "Messages"

   Public ReadOnly Property Title As String
      Get
         Return EgtMsg(61986)
      End Get
   End Property

   Public ReadOnly Property Filters_Msg As String
      Get
         Return EgtMsg(61985)
      End Get
   End Property

   Public ReadOnly Property FilterType_Msg As String
      Get
         Return EgtMsg(61836)
      End Get
   End Property
   Public ReadOnly Property Search_Msg As String
      Get
         Return EgtMsg(61837)
      End Get
   End Property

   Public ReadOnly Property Id_Msg As String
      Get
         Return EgtMsg(61815)
      End Get
   End Property
   Public ReadOnly Property BTLFileName_Msg As String
      Get
         Return EgtMsg(61819)
      End Get
   End Property
   Public ReadOnly Property ListName_Msg As String
      Get
         Return EgtMsg(61820)
      End Get
   End Property
   Public ReadOnly Property ExportDate_Msg As String
      Get
         Return EgtMsg(61821)
      End Get
   End Property
   Public ReadOnly Property CreateDate_Msg As String
      Get
         Return EgtMsg(61822)
      End Get
   End Property
   Public ReadOnly Property Machine_Msg As String
      Get
         Return EgtMsg(61886)
      End Get
   End Property
   Public ReadOnly Property Name_Msg As String
      Get
         Return EgtMsg(61808)
      End Get
   End Property

   Public ReadOnly Property Open_Msg As String
      Get
         Return EgtMsg(30006)
      End Get
   End Property

   Public ReadOnly Property Archived_Msg As String
      Get
         Return EgtMsg(61983)
      End Get
   End Property

   Public ReadOnly Property Archive_Msg As String
      Get
         If Not IsNothing(m_SelProject) AndAlso m_SelProject.bIsArchived Then
            Return EgtMsg(61984)
         Else
            Return EgtMsg(61981)
         End If
      End Get
   End Property
   Public ReadOnly Property Delete_Msg As String
      Get
         Return EgtMsg(61823)
      End Get
   End Property
   Public ReadOnly Property Cancel_Msg As String
      Get
         Return EgtMsg(30002)
      End Get
   End Property

   Public ReadOnly Property From_Msg As String
      Get
         Return EgtMsg(62523)
      End Get
   End Property

   Public ReadOnly Property To_Msg As String
      Get
         Return EgtMsg(62524)
      End Get
   End Property

   Public ReadOnly Property ViewArchived_Msg As String
      Get
         Return EgtMsg(61982)
      End Get
   End Property

   Public ReadOnly Property Rows_Msg As String
      Get
         Return EgtMsg(61995)
      End Get
   End Property

#End Region

   ' Definizione comandi
   Private m_cmdProjectDoubleClick As ICommand
   Private m_cmdDelete As ICommand
   Private m_cmdArchive As ICommand
   Private m_cmdCancel As ICommand

#End Region    ' FIELDS & PROPERTIES

   Sub New()
      ' assegno delegato a elementi lista
      ProdItem.m_delIsModifiedSetUp = AddressOf SetIsEditNameActive
      ' carico colonne
      LoadColumns()
      m_colProdFile_Name = m_ProdColumns.FirstOrDefault(Function(x) x.Name = COL_NAME)
      m_colArchived_Name = m_ProdColumns.FirstOrDefault(Function(x) x.Name = COL_ARCHIVED)
      ' leggo valori per filtri
      m_SelDayType = GetMainPrivateProfileInt(S_OPENFILEDIALOG, K_DAYTYPE, 0)
      Select Case m_SelDayType
         Case DayTypes.LASTMONTH
            m_dtEndDate = DateTime.Today + TimeSpan.FromDays(1)
            m_dtStartDate = m_dtEndDate - TimeSpan.FromDays(30.5)
         Case DayTypes.LAST3MONTHS
            m_dtEndDate = DateTime.Today + TimeSpan.FromDays(1)
            m_dtStartDate = m_dtEndDate - TimeSpan.FromDays(30.5 * 3)
         Case DayTypes.LAST6MONTHS
            m_dtEndDate = DateTime.Today + TimeSpan.FromDays(1)
            m_dtStartDate = m_dtEndDate - TimeSpan.FromDays(30.5 * 6)
         Case DayTypes.PERIOD
            Dim sStartDate As String = "0"
            Dim lStartDate As Long = 0
            GetMainPrivateProfileString(S_OPENFILEDIALOG, K_STARTDATE, 0, sStartDate)
            Long.TryParse(sStartDate, lStartDate)
            m_dtStartDate = If(lStartDate > 0, DateTime.FromFileTimeUtc(lStartDate), DateTime.Today)
            Dim sEndDate As String = "0"
            Dim lEndDate As Long = 0
            GetMainPrivateProfileString(S_OPENFILEDIALOG, K_ENDDATE, 0, sEndDate)
            Long.TryParse(sEndDate, lEndDate)
            m_dtEndDate = If(lEndDate > 0, DateTime.FromFileTimeUtc(lEndDate), DateTime.Today)
            m_Date_Visibility = Visibility.Visible
      End Select
      m_bViewArchived = GetMainPrivateProfileInt(S_OPENFILEDIALOG, K_VIEWARCHIVED, 0) <> 0
   End Sub

#Region "METHODS"

   Public MustOverride Function Init(ProjectType As ProjectType, Optional CurrProjectList As List(Of ProjectFileVM) = Nothing, Optional GoToProd As Boolean = False) As Boolean?
   Public Overridable Sub RefreshProjectList(Optional GoToProd As Boolean = False)
   End Sub

   Protected Sub LoadColumns()
      'If ProjectType = ProjectType.PROJ Then
      ' carico le colonne della datagrid
      ' GetPrivateProfileColumns(S_OPENPROJFILEDLG_PROJ, ProjColumns)
      '   ' carico campi su cui eseguire il filtro di ricerca
      '   m_BTLDateTypeList = New List(Of String)({CreateDate_Msg, ExportDate_Msg})
      '   m_SelBTLDateType = BTLDateTypes.CREATEDATE
      '   NotifyPropertyChanged(NameOf(BTLDateTypeList))
      '   NotifyPropertyChanged(NameOf(SelBTLDateType))
      '   m_FilterTypeList = New ObservableCollection(Of IdNameStruct)({New IdNameStruct(FilterTypes.NULL, ""),
      '                                                                 New IdNameStruct(FilterTypes.ID, Id_Msg),
      '                                                                 New IdNameStruct(FilterTypes.BTLFILENAME, BTLFileName_Msg),
      '                                                                 New IdNameStruct(FilterTypes.LISTNAME, ListName_Msg),
      '                                                                 New IdNameStruct(FilterTypes.MACHINE, Machine_Msg)})
      '   m_SelFilterType = m_FilterTypeList.FirstOrDefault(Function(x) x.Id = FilterTypes.NULL)
      '   NotifyPropertyChanged(NameOf(SelFilterType))
      'ElseIf ProjectType = ProjectType.PROD Then
      ' carico le colonne della datagrid
      GetPrivateProfileColumns(S_NEWOPENPROJFILEDLG_PROD, ProdColumns)
      ' carico campi su cui eseguire il filtro di ricerca
      m_BTLDateTypeList = New List(Of String)({CreateDate_Msg})
      m_SelBTLDateType = BTLDateTypes.CREATEDATE
      NotifyPropertyChanged(NameOf(BTLDateTypeList))
      NotifyPropertyChanged(NameOf(SelBTLDateType))
      m_FilterTypeList = New ObservableCollection(Of IdNameStruct)({New IdNameStruct(FilterTypes.NULL, ""),
                                                                    New IdNameStruct(FilterTypes.BTLFILENAME, BTLFileName_Msg),
                                                                    New IdNameStruct(FilterTypes.LISTNAME, ListName_Msg),
                                                                    New IdNameStruct(FilterTypes.MACHINE, Machine_Msg)})
      m_SelFilterType = m_FilterTypeList.FirstOrDefault(Function(x) x.Id = FilterTypes.NULL)
      NotifyPropertyChanged(NameOf(SelFilterType))
      'End If
      ' setto la visibilità delle colonne delle EgtDataGrid
      For Each col In ProjColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
      For Each col In ProdColumns
         col.ColumnVisibility = If(col.Visible, Visibility.Visible, Visibility.Collapsed)
      Next
   End Sub

   Friend Function VerifySelected() As Boolean
      If IsNothing(SelProject) Then Return False
      Dim AllFilesInDir As IEnumerable(Of String) = Nothing
      If m_ProjectType = Core.ConstBeam.ProjectType.PROJ Then
         If Not IsNothing(SelProject.ProdFileVM) Then
            If String.IsNullOrWhiteSpace(SelProject.ProdFileVM.sProdDirPath) OrElse Not Directory.Exists(SelProject.ProdFileVM.sProdDirPath) Then Return False
            AllFilesInDir = Directory.EnumerateFiles(SelProject.ProdFileVM.sProdDirPath)
         ElseIf SelProject.ProjFileList.Count > 0 Then
            If String.IsNullOrWhiteSpace(SelProject.ProjFileList(0).sProjDirPath) OrElse Not Directory.Exists(SelProject.ProjFileList(0).sProjDirPath) Then Return False
            AllFilesInDir = Directory.EnumerateFiles(SelProject.ProjFileList(0).sProjDirPath)
         Else Return False
         End If
      ElseIf m_ProjectType = Core.ConstBeam.ProjectType.PROD Then
         If String.IsNullOrWhiteSpace(SelProject.ProdFileVM.sProdDirPath) OrElse Not Directory.Exists(SelProject.ProdFileVM.sProdDirPath) Then Return False
         AllFilesInDir = Directory.EnumerateFiles(SelProject.ProdFileVM.sProdDirPath)
      Else
         Return False
      End If
      Return AllFilesInDir.Any(Function(x) Path.GetExtension(x).ToLower() = ".nge")
   End Function

   Protected Function ProjectFilter(Proj As Object) As Boolean
      Dim bProjectOk As Boolean = True
      'If TypeOf Proj Is ProjFileVM Then
      Dim CurrProj As ProdItem = DirectCast(Proj, ProdItem)
      If m_SelFilterType.Id <> FilterTypes.NULL AndAlso (
         (m_SelFilterType.Id = BTLDateTypes.EXPORTDATE AndAlso Not CurrProj.ProjFileList.Any(Function(x) SearchDate.Contains(DirectCast(x, ProjFileVM).dtExportDate.Date))) OrElse
         (m_SelFilterType.Id = BTLDateTypes.CREATEDATE AndAlso Not SearchDate.Contains(CurrProj.dtCreateDate.Date))) Then bProjectOk = False
      If m_SelFilterType.Id <> FilterTypes.NULL AndAlso m_SearchText <> String.Empty AndAlso (
         (m_SelFilterType.Id = FilterTypes.BTLFILENAME AndAlso CurrProj.ProjFileList.Any(Function(x) DirectCast(x, ProjFileVM).sBTLFileName.IndexOf(SearchText, StringComparison.InvariantCultureIgnoreCase) < 0)) OrElse
         (m_SelFilterType.Id = FilterTypes.LISTNAME AndAlso CurrProj.ProjFileList.Any(Function(x) DirectCast(x, ProjFileVM).sListName.IndexOf(SearchText, StringComparison.InvariantCultureIgnoreCase) < 0)) OrElse
         (m_SelFilterType.Id = FilterTypes.MACHINE AndAlso CurrProj.sMachine.IndexOf(SearchText, StringComparison.InvariantCultureIgnoreCase) < 0)) Then bProjectOk = False
      'ElseIf TypeOf Proj Is ProdFileVM Then
      '   Dim CurrProj As ProdFileVM = DirectCast(Proj, ProdFileVM)
      '   If m_SelFilterType.Id <> FilterTypes.NULL AndAlso (
      '      (m_SelFilterType.Id = BTLDateTypes.CREATEDATE AndAlso Not SearchDate.Contains(CurrProj.dtCreateDate.Date))) Then bProjectOk = False
      '   If m_SelFilterType.Id <> FilterTypes.NULL AndAlso m_SearchText <> String.Empty AndAlso (
      '      (m_SelFilterType.Id = FilterTypes.ID AndAlso CurrProj.sProdId.ToString.IndexOf(SearchText, StringComparison.InvariantCultureIgnoreCase) < 0) OrElse
      '      (m_SelFilterType.Id = FilterTypes.BTLFILENAME AndAlso CurrProj.sBTLFileName.IndexOf(SearchText, StringComparison.InvariantCultureIgnoreCase) < 0) OrElse
      '      (m_SelFilterType.Id = FilterTypes.MACHINE AndAlso CurrProj.sMachine.IndexOf(SearchText, StringComparison.InvariantCultureIgnoreCase) < 0)) Then bProjectOk = False
      'End If
      Return bProjectOk
   End Function

   Protected Sub CloseWindow(bDialogResult As Boolean)
      RaiseEvent m_CloseWindow(bDialogResult)
   End Sub

#End Region    ' METHODS

#Region "COMMANDS"

#Region "ProjectDoubleClick"

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

   ''' <summary>
   ''' Execute the New.  This method is invoked by the NewCommand.
   ''' </summary>
   Friend Sub ProjDoubleClick()
      ' verifico se non e' attivo edit del nome
      If bIsEditNameActive Then Return
      ' verifico validita' selezionato
      If VerifySelected() Then
         RaiseEvent m_CloseWindow(True)
      End If
   End Sub

#End Region ' ProjectDoubleClick

#Region "Archive"

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

   ''' <summary>
   ''' Execute the New.  This method is invoked by the NewCommand.
   ''' </summary>
   Public Overridable Sub Archive()
   End Sub

#End Region ' Archive

#Region "Delete"

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

   ''' <summary>
   ''' Execute the New.  This method is invoked by the NewCommand.
   ''' </summary>
   Public Overridable Sub Delete()
   End Sub

#End Region ' Delete

#Region "Cancel"

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

   ''' <summary>
   ''' Execute the New.  This method is invoked by the NewCommand.
   ''' </summary>
   Public Overridable Sub Cancel()
   End Sub

#End Region ' Cancel

#End Region    ' Commands

End Class

Public Class ProdItem
   Inherits VMBase

   Public Shared m_delIsModifiedSetUp As Action(Of Boolean)

   Protected m_ProdFileVM As ProdFileVM
   Public ReadOnly Property ProdFileVM As ProdFileVM
      Get
         Return m_ProdFileVM
      End Get
   End Property

   Private m_ProjFileList As New List(Of ProjectFileVM)
   Public ReadOnly Property ProjFileList As List(Of ProjectFileVM)
      Get
         Return m_ProjFileList
      End Get
   End Property

   Public ReadOnly Property sProdId As String
      Get
         Return If(Not IsNothing(m_ProdFileVM), m_ProdFileVM.sProdId, "")
      End Get
   End Property

   Public Overridable Property sName As String
      Get
         Return If(Not IsNothing(m_ProdFileVM), m_ProdFileVM.sName, "")
      End Get
      Set(value As String)
         If Not IsNothing(m_ProdFileVM) Then
            m_ProdFileVM.sName = value
         End If
      End Set
   End Property

   Public ReadOnly Property dtCreateDate As Date
      Get
         Return If(Not IsNothing(m_ProdFileVM), {m_ProdFileVM.dtCreateDate, m_ProjFileList.Min(Function(x) x.dtCreateDate)}.Min(), m_ProjFileList.Min(Function(x) x.dtCreateDate))
      End Get
   End Property
   Public ReadOnly Property sCreateDate As String
      Get
         Return dtCreateDate.ToString()
      End Get
   End Property

   Public ReadOnly Property sMachine As String
      Get
         If Not IsNothing(m_ProdFileVM) Then
            Return m_ProdFileVM.sMachine
         ElseIf Not IsNothing(m_ProjFileList(0)) Then
            Return m_ProjFileList(0).sMachine
         Else Return ""
         End If
      End Get
   End Property

   Public ReadOnly Property bIsArchived As Boolean
      Get
         If Not IsNothing(m_ProdFileVM) Then
            Return m_ProdFileVM.bIsArchived
         ElseIf Not IsNothing(m_ProjFileList(0)) Then
            Return m_ProjFileList(0).bIsArchived
         Else
            Return True
         End If
      End Get
   End Property

   Public ReadOnly Property Archived_Visibility As Visibility
      Get
         Return If(bIsArchived, Visibility.Visible, Visibility.Collapsed)
      End Get
   End Property

   Sub New(ProdFileVM As ProdFileVM, ProjFileVM As ProjFileVM)
      m_ProdFileVM = ProdFileVM
      m_ProjFileList.Add(ProjFileVM)
   End Sub

   Sub New(ProjFileVM As ProjFileVM)
      m_ProjFileList = New List(Of ProjectFileVM)({ProjFileVM})
   End Sub

   Sub New(ProdFileVM As ProjectFileVM, ProjFileVMList As List(Of ProjectFileVM))
      m_ProdFileVM = ProdFileVM
      m_ProjFileList = ProjFileVMList
   End Sub

End Class
