Imports System.IO
Imports EgtBEAMWALL.Core
Imports EgtUILib

Public Class ProdFileVM
   Inherits Core.ProdFileVM

#Region "CONSTRUCTORS"

   Sub New(ProdFileM As ProdFileM)
      MyBase.New(ProdFileM)
      ' aggiungo nome btl
      Dim sBTLFileName As String = ""
      For Each TempCurrProj In nProjIdList
         Dim TempProj As ProjFileM = DbControllers.m_ProjController.FindByProjIdConv(TempCurrProj)
         If Not IsNothing(TempProj) Then sBTLFileName &= TempProj.sListName
      Next
      SetBTLFileName(sBTLFileName)
      ' verifico valore description
      If Not IsNothing(ProdFileM) AndAlso String.IsNullOrWhiteSpace(ProdFileM.sName) Then
         For Each TempCurrProj In nProjIdList
            Dim TempProj As ProjFileM = DbControllers.m_ProjController.FindByProjIdConv(TempCurrProj)
            If Not IsNothing(TempProj) AndAlso Not String.IsNullOrWhiteSpace(TempProj.sBTLFileName) Then
               DbControllers.m_ProdController.UpdateDescription(ProdFileM.nProdId, TempProj.sBTLFileName)
               ProdFileM.sName = TempProj.sBTLFileName
            End If
         Next
      End If
   End Sub

#End Region    ' CONSTRUCTORS

   Friend Function GetProjPath(nProjId As Integer)
      Dim sPath As String = String.Empty
      If nProjId = 0 Then Return String.Empty
      Return refMainWindowVM.MainWindowM.sProjsDir & "\" & nProjId.ToString("0000") & "\" & nProjId.ToString("0000") & ".nge"
   End Function

   Friend Shared Function VerifyProjectModification(CurrProject As ProdFileVM) As MessageBoxResult
      If IsNothing(CurrProject) Then Return True
      Dim bNewProject As Boolean = False
      Dim sProjectDirPath As String = ""
      sProjectDirPath = CurrProject.sProdDirPath
      Dim VerifyResult As MessageBoxResult = MessageBoxResult.None
      ' verifico se progetto modificato, e chiedo se salvare
      If EgtGetModified() OrElse CurrProject.bIsNew OrElse Map.refCALCPanelVM.IsMachineModified() Then
         'VerifyResult = MessageBox.Show(EgtMsg(61875), "", MessageBoxButton.YesNoCancel, MessageBoxImage.Question)
         If bOnlyProd Then
            VerifyResult = EgtBEAMWALL.Core.OnlyProdEgtMessageBoxV.Show(Application.Current.MainWindow, EgtMsg(61875), "", MessageBoxButton.YesNoCancel, MessageBoxImage.Question)
         Else
            VerifyResult = EgtBEAMWALL.Core.EgtMessageBoxV.Show(Application.Current.MainWindow, EgtMsg(61875), "", MessageBoxButton.YesNoCancel, MessageBoxImage.Question)
         End If
         Select Case VerifyResult
            Case MessageBoxResult.Yes
               ' salvo proj
               If Not IsNothing(Map.refProdManagerVM) Then
                  Map.refProdManagerVM.Save()
               ElseIf Not IsNothing(Map.refOnlyProdManagerVM) Then
                  Map.refOnlyProdManagerVM.Save()
               End If
            Case MessageBoxResult.Cancel
               Return MessageBoxResult.Cancel
            Case Else   ' No
               EgtResetModified()
               Map.refCALCPanelVM.RevertToOriginalMachine()
               If CurrProject.bIsNew Then
                  ' lo elimino
                  Try
                     Directory.Delete(sProjectDirPath, True)
                  Catch ex As Exception
                  End Try
                  If Not IsNothing(Map.refProdManagerVM) Then
                     Map.refProdManagerVM.m_MruFiles.Remove(CurrProject.sProdPath)
                     Map.refProdManagerVM.NotifyPropertyChanged(NameOf(Map.refProdManagerVM.MruFileNames))
                  ElseIf Not IsNothing(Map.refOnlyProdManagerVM) Then
                     Map.refOnlyProdManagerVM.m_MruFiles.Remove(CurrProject.sProdPath)
                     Map.refOnlyProdManagerVM.NotifyPropertyChanged(NameOf(Map.refProdManagerVM.MruFileNames))
                  End If
                  DbControllers.m_ProdController.DeleteProd(CurrProject.nProdId, False)
                  If Not IsNothing(Map.refProjManagerVM) AndAlso CurrProject.nProdId = ProjectManagerVM.CurrProj.nProdId Then
                     ' reset prod in path proj
                     Map.refProjManagerVM.UpdateCurrProj()
                  ElseIf Not IsNothing(Map.refOnlyProdManagerVM) AndAlso CurrProject.nProdId = ProjectManagerVM.CurrProj.nProdId Then
                     ' reset prod in path proj
                     Map.refOnlyProdManagerVM.UpdateCurrProj()
                  End If
                  If Not IsNothing(Map.refProdManagerVM) Then
                     ProjectManagerVM.CurrProd = Nothing
                  End If
                  EgtResetModified()
                  Return MessageBoxResult.No
               End If
         End Select
      Else
         EgtResetModified()
      End If
      DbControllers.m_ProdController.ResetController()
      DbControllers.m_ProdController.LockByProdId(CurrProject.nProdId, False, Map.refMainWindowVM.MainWindowM.GetKeyNumber())
      Return VerifyResult
   End Function

End Class