Imports EgtWPFLib5
Imports System.Windows

Public Class ProdItem
    Inherits VMBase

   Public Shared m_delUpdateNameInDb As Action(Of Integer, String)
   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_delUpdateNameInDb(m_ProdFileVM.nProdId, value)
            m_ProdFileVM.sName = value
            m_delIsModifiedSetUp(False)
         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


   Public Shared Function CreateProdItem(nProdId As Integer, nProjIdList As List(Of Integer), dtCreateProjDate As DateTime, sName As String, nType As BWType,
                                           sMachine As String, sLockedBy As String, dtLock As DateTime, bIsActive As Boolean, bIsProduced As Boolean, bIsArchived As Boolean, ProjMList As List(Of ProjFileM)) As ProdItem


      Dim NewProdFileM = ProdFileM.CreateProdFileM(nProdId, nProjIdList, dtCreateProjDate, sName, nType, sMachine, sLockedBy, dtLock, bIsActive, bIsProduced, bIsArchived)
      Dim NewProdFileVM = New ProdFileVM(NewProdFileM)
      Dim NewProjFileVM As IEnumerable(Of ProjectFileVM)
      NewProjFileVM = ProjMList.Select(Of ProjFileVM)(Function(j) New ProjFileVM(j)).ToList
      Dim NewProdItem = New ProdItem(NewProdFileVM, NewProjFileVM.ToList)

      Return NewProdItem
   End Function

   Public Shared Function CreateProdItem(ProjFileM As ProjFileM) As ProdItem

      Dim NewProdItem = New ProdItem(New ProjFileVM(ProjFileM))

      Return NewProdItem
   End Function

End Class