Imports System.IO
Imports EgtUILib
Imports EgtWPFLib

Class SaveNameWD

   ' Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Private m_nType As Integer
   Friend Enum SAVE_TYPE As UInteger
      PRJ_NAMED = 0
      PRJ_COPY = 1
      TEMPLATE = 2
   End Enum

   Public Sub New(Owner As Window, Type As Integer)
      Me.Owner = Owner
      m_nType = Type
      InitializeComponent()
   End Sub

   Private Sub SaveNameWD_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      Me.Top = Owner.Top + Owner.Height / 2 - Me.Height / 2
      Me.Left = Owner.Left + Owner.Width / 2 - Me.Width / 2
      If m_nType = SAVE_TYPE.PRJ_NAMED Then
         SaveNameTxbl.Text = EgtMsg(MSG_CADCUTPAGEUC + 7)     'Nuovo nome del progetto
         GetPrivateProfileString(S_GENERAL, K_LASTNAMEPROJ, "", SaveNameTxBx.Text, m_MainWindow.GetIniFile())
      ElseIf m_nType = SAVE_TYPE.PRJ_COPY Then
         SaveNameTxbl.Text = EgtMsg(MSG_CADCUTPAGEUC + 10)     'Esporta
      ElseIf m_nType = SAVE_TYPE.TEMPLATE Then
         SaveNameTxbl.Text = EgtMsg(MSG_DIRECTCUTPAGEUC + 45)     'Nuovo nome della dima
      End If
   End Sub

   Private Sub OkBtn_Click(sender As Object, e As RoutedEventArgs) Handles OkBtn.Click
      Dim sPath As String = String.Empty
      If m_nType = SAVE_TYPE.PRJ_NAMED Then
         ' Creo path completa con nome inserito
         sPath = m_MainWindow.GetNamedSaveDir() & "\" & SaveNameTxBx.Text & ".nge"
      ElseIf m_nType = SAVE_TYPE.PRJ_COPY Then
         sPath = m_MainWindow.GetCopyProjDir() & "\" & SaveNameTxBx.Text & ".nge"
      ElseIf m_nType = SAVE_TYPE.TEMPLATE Then
         sPath = m_MainWindow.GetCopyTemplateDir() & "\" & SaveNameTxBx.Text & ".dxf"
      End If
      ' Verifico se il nome inserito è già utilizzato
      If File.Exists(sPath) Then
         Dim ConfirmWD As EgtMsgBox
         If m_MainWindow.IsSiemensPc() Then
            ConfirmWD = New EgtMsgBox(m_MainWindow, m_MainWindow.ActualWidth / 15 * 4.5, EgtMsgBox.WidthType.PIXEL, "", EgtMsg(MSG_EGTMSGBOX + 6), EgtMsgBox.Buttons.OK_CANCEL, EgtMsgBox.Icons.NULL)
         Else
            ConfirmWD = New EgtMsgBox(m_MainWindow, "", EgtMsg(MSG_EGTMSGBOX + 6), EgtMsgBox.Buttons.OK_CANCEL, EgtMsgBox.Icons.NULL)
         End If
         Select Case ConfirmWD.m_nPressedBtn
            Case 0   'Cancel
               ' Esco
               Return
         End Select
      End If
      If m_nType = SAVE_TYPE.PRJ_NAMED Then
         ' Aggiorno file Ini
         WritePrivateProfileString(S_GENERAL, K_LASTNAMEPROJ, SaveNameTxBx.Text, m_MainWindow.GetIniFile())
         ' Eseguo salvataggio
         m_MainWindow.m_CurrentProjectPageUC.SaveNamedProject()
         m_MainWindow.m_CurrentProjectPageUC.SaveProject()
         ' Se cornici, ripristino visualizzazione lavorazioni
         If m_MainWindow.m_ActivePage = MainWindow.Pages.FrameCut Then
            ShowAllCurrPhaseMachinings()
         End If
      ElseIf m_nType = SAVE_TYPE.PRJ_COPY Then
         ' Eseguo copia
         m_MainWindow.m_CurrentProjectPageUC.ExportProject( sPath)
         m_MainWindow.m_CurrentProjectPageUC.SaveProject()
         ' Se cornici, ripristino visualizzazione lavorazioni
         If m_MainWindow.m_ActivePage = MainWindow.Pages.FrameCut Then
            ShowAllCurrPhaseMachinings()
         End If
      ElseIf m_nType = SAVE_TYPE.TEMPLATE Then
         ' Recupero nome gruppo da esportare
         Dim nId = EgtGetFirstNameInGroup(GDB_ID.ROOT, NAME_COPYTEMPLATE)
         ' Se UI in pollici, scalo opportunamente per avere DXF in pollici
         Dim bScale As Boolean = Not EgtUiUnitsAreMM()
         If bScale Then EgtScale(nId, Frame3d.GLOB, 1 / ONEINCH, 1 / ONEINCH, 1 / ONEINCH, GDB_RT.GLOB)
         ' Eseguo esportazione
         EgtExportDxf(nId, sPath)
         ' Se necessario, ripristino scala
         If bScale Then EgtScale(nId, Frame3d.GLOB, ONEINCH, ONEINCH, ONEINCH, GDB_RT.GLOB)
      End If

      EgtDraw()
      Close()
   End Sub

   Private Sub ExitBtn_Click(sender As Object, e As RoutedEventArgs) Handles ExitBtn.Click
      EgtDraw()
      Close()
   End Sub

   Private Sub Me_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
      If e.Key = Key.System And e.SystemKey = Key.F4 Then
         e.Handled = True
      End If
   End Sub

End Class
