Imports System.IO
Imports EgtBEAMWALL.Core
Imports EgtUILib
Imports EgtWPFLib5

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 CurrProj In nProjIdList
         Dim TempProj As ProjFileM = DbControllers.m_ProjController.FindByProjIdConv(CurrProj)
         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 CurrProj In nProjIdList
            Dim TempProj As ProjFileM = DbControllers.m_ProjController.FindByProjIdConv(CurrProj)
            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

   '' funzione che restituisce le parti di nome file
   'Friend Shared Function VerifyProjectFile(nProjectType As ProjectType, ProjectFileName As String, ByRef nProjId As Integer, ByRef nProdId As Integer, ByRef sBTLFileName As String) As Boolean
   '   If nProjectType = ProjectType.PROJ Then
   '      Dim DataFromFileName As String() = ProjectFileName.Split(FILENAMESEPARATOR)
   '      If DataFromFileName.Count = 3 Then
   '         If Not String.IsNullOrEmpty(DataFromFileName(0)) Then
   '            Integer.TryParse(DataFromFileName(0), nProjId)
   '         Else Return False
   '         End If
   '         If Not String.IsNullOrEmpty(DataFromFileName(1)) Then
   '            Integer.TryParse(DataFromFileName(1), nProdId)
   '         Else
   '            nProdId = 0
   '         End If
   '         If Not String.IsNullOrEmpty(DataFromFileName(2)) Then
   '            sBTLFileName = DataFromFileName(2)
   '         End If
   '         Return True
   '      Else Return False
   '      End If
   '   ElseIf nProjectType = ProjectType.PROD Then
   '      Return Integer.TryParse(ProjectFileName, nProdId)
   '   Else Return False
   '   End If
   'End Function

   '' funzione che dato un nome file e il tipo restituisce il corretto numero di progetto
   'Friend Shared Function GetProjectNumber(nProjectType As ProjectType, ProjectFileName As String, ByRef nProjId As Integer, ByRef nProdId As Integer, ByRef sBTLFileName As String) As Boolean
   '   If String.IsNullOrWhiteSpace(ProjectFileName) Then
   '      nProjId = 0
   '      nProdId = 0
   '      sBTLFileName = ""
   '      Return False
   '   ElseIf ProjectFileName.Contains(FILENAMESEPARATOR) Then
   '      Dim DataFromFileName As String() = ProjectFileName.Split(FILENAMESEPARATOR)
   '      If DataFromFileName.Count = 3 Then
   '         If Not String.IsNullOrEmpty(DataFromFileName(0)) Then
   '            Integer.TryParse(DataFromFileName(0), nProjId)
   '         Else Return False
   '         End If
   '         If Not String.IsNullOrEmpty(DataFromFileName(1)) Then
   '            Integer.TryParse(DataFromFileName(1), nProdId)
   '         Else
   '            nProdId = 0
   '         End If
   '         If Not String.IsNullOrEmpty(DataFromFileName(2)) Then
   '            sBTLFileName = DataFromFileName(2)
   '         End If
   '         Return True
   '      Else Return False
   '      End If
   '   ElseIf nProjectType = ProjectType.PROJ Then
   '      Dim ProjectFileNameProd As String = Integer.TryParse(ProjectFileName, nProdId)
   '      Dim nTempProjId As Integer = 0
   '      Dim nTempProdId As Integer = 0
   '      Dim sTempBTLFileName As String = ""
   '      ' ricavo lista cartelle proj
   '      Dim AllDirsInDir As IEnumerable(Of String) = IO.Directory.EnumerateDirectories(Map.refMainWindowVM.MainWindowM.sProjsDir)
   '      For Each Directory In AllDirsInDir
   '         Dim AllFilesInDir As IEnumerable(Of String) = IO.Directory.EnumerateFiles(Directory)
   '         For Each File In AllFilesInDir
   '            If Path.GetExtension(File).ToLower() = ".nge" Then
   '               If File.Contains(FILENAMESEPARATOR) Then
   '                  Dim DataFromFileName As String() = ProjectFileName.Split(FILENAMESEPARATOR)
   '                  If DataFromFileName.Count = 3 Then
   '                     If Not String.IsNullOrEmpty(DataFromFileName(0)) Then
   '                        Integer.TryParse(DataFromFileName(0), nTempProjId)
   '                     Else Return False
   '                     End If
   '                     If Not String.IsNullOrEmpty(DataFromFileName(1)) Then
   '                        Integer.TryParse(DataFromFileName(1), nTempProdId)
   '                     Else
   '                        nProdId = 0
   '                     End If
   '                     If Not String.IsNullOrEmpty(DataFromFileName(2)) Then
   '                        sTempBTLFileName = DataFromFileName(2)
   '                     End If
   '                     If ProjectFileNameProd = nTempProdId Then
   '                        nProjId = nTempProjId
   '                        nProdId = nTempProdId
   '                        sTempBTLFileName = sBTLFileName
   '                        Return True
   '                     End If
   '                  Else Return False
   '                  End If
   '               End If
   '            End If
   '         Next
   '      Next
   '   End If
   '   Return False
   '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)
         Select Case VerifyResult
            Case MessageBoxResult.Yes
               ' salvo proj
               Map.refProdManagerVM.Save()
            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
                  Map.refProdManagerVM.m_MruFiles.Remove(CurrProject.sProdPath)
                  Map.refProdManagerVM.NotifyPropertyChanged(NameOf(Map.refProdManagerVM.MruFileNames))
                  DbControllers.m_ProdController.DeleteProd(CurrProject.nProdId, False)
                  If CurrProject.nProdId = Map.refProjManagerVM.CurrProj.nProdId Then
                     ' reset prod in path proj
                     Map.refProjManagerVM.UpdateCurrProj()
                  End If
                  Map.refProdManagerVM.CurrProd = Nothing
                  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