Imports EgtUILib
Imports EgtWPFLib
Imports System.Reflection
Imports System.Globalization

Public Class CadCutPageUC

   ' Riferimenti a pagine
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Private m_CurrProjPage As CurrentProjectPageUC

   ' Dichiarazione delle Page UserControl
   Friend WithEvents m_NestPage As NestPageUC
   Friend m_SplitPage As SplitPageUC
   Friend m_MoveRawPartPage As MoveRawPartPage
   Friend m_CSVPage As CSVPage
   Friend m_ProjectMgr As ProjectMgrUC
   Friend m_FastGridSlabManager As FastGridSlabManager

   ' Variabile che indica la modalità
   Friend m_CadCutMode As CadCutModes

   ' Elenco pagine interne a cadcutpage
   Friend Enum CadCutModes As Integer
      Nest
      Split
      MoveRawPart
      FastGrid
   End Enum

   Private Sub CadCutPage_Initialized(sender As Object, e As EventArgs)

      ' Creazione delle Page UserControl
      m_NestPage = New NestPageUC
      m_SplitPage = New SplitPageUC
      m_MoveRawPartPage = New MoveRawPartPage
      m_CSVPage = New CSVPage
      m_ProjectMgr = New ProjectMgrUC
      m_FastGridSlabManager = New FastGridSlabManager

      ' Posizionamento nella griglia delle Page UserControl
      m_NestPage.SetValue(Grid.RowSpanProperty, 3)
      m_NestPage.SetValue(Grid.ColumnSpanProperty, 2)
      m_SplitPage.SetValue(Grid.RowSpanProperty, 3)
      m_SplitPage.SetValue(Grid.ColumnSpanProperty, 3)
      m_MoveRawPartPage.SetValue(Grid.RowSpanProperty, 3)
      m_MoveRawPartPage.SetValue(Grid.ColumnSpanProperty, 3)
      m_CSVPage.SetValue(Grid.RowSpanProperty, 3)
      m_CSVPage.SetValue(Grid.ColumnSpanProperty, 3)
      m_ProjectMgr.SetValue(Grid.RowProperty, 2)
      m_ProjectMgr.SetValue(Grid.ColumnProperty, 1)
      m_FastGridSlabManager.SetValue(Grid.RowSpanProperty, 3)
      m_FastGridSlabManager.SetValue(Grid.ColumnSpanProperty, 2)

      ' Assegno UC a questa pagina
      CadCutPageGrid.Children.Add(m_ProjectMgr)
   End Sub

   Private Sub CadCutPage_Loaded(sender As Object, e As RoutedEventArgs)
      m_CurrProjPage = m_MainWindow.m_CurrentProjectPageUC
      ' Se abilitato, visualizzo bottone CSV
      If m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.CSV_AUTO) Or
         m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.CSV_SIMPLE) Then
         m_ProjectMgr.CSVBtn.Visibility = Windows.Visibility.Visible
      Else
         m_ProjectMgr.CSVBtn.Visibility = Windows.Visibility.Hidden
      End If
      ' Se abilitato, visualizzo bottone VeinMatching
      If m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.MAN_PHOTO) And
         GetPrivateProfileInt(S_VEINMATCHING, K_VEINMA_ENABLE, 0, m_MainWindow.GetIniFile()) <> 0 Then
         m_ProjectMgr.VeinMatchingBtn.Visibility = Windows.Visibility.Visible
      Else
         m_ProjectMgr.VeinMatchingBtn.Visibility = Windows.Visibility.Hidden
      End If
      ' Se abilitato, visualizzo bottone Registration
      If m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.REGISTRATION) And
         GetPrivateProfileInt(S_GENERAL, K_REGISTRATION, 0, m_MainWindow.GetIniFile()) <> 0 Then
         m_ProjectMgr.RegisterBtn.Visibility = Windows.Visibility.Visible
      Else
         m_ProjectMgr.RegisterBtn.Visibility = Windows.Visibility.Hidden
      End If
      ' Leggo se la modalità FastGrid è attiva
      If GetPrivateProfileInt(S_FASTGRID, K_FG_ENABLE, 0, m_MainWindow.GetIniFile()) <> 0 And
         GetPrivateProfileInt(S_FASTGRID, K_FG_ACTIVE, 0, m_MainWindow.GetIniFile()) <> 0 Then
         ' Seleziono UserControl di apertura
         CadCutPageGrid.Children.Add(m_FastGridSlabManager)
         m_CadCutMode = CadCutModes.FastGrid
      Else
         ' Seleziono UserControl di apertura
         CadCutPageGrid.Children.Add(m_NestPage)
         m_CadCutMode = CadCutModes.Nest
         DrawBtn.IsEnabled = True
         ImportBtn.IsEnabled = True
      End If
      If (m_MainWindow.m_OptionsPageUC.ThemesCmBx.SelectedIndex = 0) Then
         SplitImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/Split.png", UriKind.Relative))
      Else
         SplitImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/NewIcons/Split.png", UriKind.Relative))
      End If
      ' Se macchina fotografica abilitata
      If m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.MAN_PHOTO) Then
         PhotoBtn.IsEnabled = True
      Else
         PhotoBtn.IsEnabled = False
      End If
   End Sub

   Private Sub PhotoBtn_Click(sender As Object, e As RoutedEventArgs) Handles PhotoBtn.Click
      m_MainWindow.TestOff()
      m_MainWindow.DragRettangleOff()
      m_CurrProjPage.m_SceneButtons.MeasureBtn.IsChecked = False
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      ' Se macchina fotografica collegata, faccio una foto
      If m_MainWindow.m_Camera.GetCameraLink() Then
         If Not m_MainWindow.m_Camera.CameraClick() Then
            m_CurrProjPage.SetErrorMessage(EgtMsg(90313))    'Fotografia non riuscita
         End If
         ' Altrimenti lancio browser di immagini
      Else
         m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_CurrentProjectPageUC)
         m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_PhotoPage)
         m_MainWindow.m_ActivePage = MainWindow.Pages.Photo
         m_MainWindow.m_PrevActivePage = MainWindow.Pages.CadCut
      End If
   End Sub

   Friend Sub PostPhoto(sPath As String, sContour As String)
      ' Carico la foto
      m_MainWindow.m_CurrentProjectPageUC.LoadPhoto(sPath)
      ' Se richiesto il riconoscimento del contorno
      If Not String.IsNullOrEmpty(sContour) Then
         If Not m_MainWindow.m_CurrentProjectPageUC.LoadContour(sContour) Then
            m_MainWindow.m_CurrentProjectPageUC.SetWarningMessage(EgtMsg(90324))    'Riconoscimento contorno non riuscito
         End If
      End If
      ' Aggiorno visualizzazione
      EgtZoom(ZM.ALL)
      ' Salvo il progetto (per dare a foto nome univoco necessario al VeinMatching)
      m_MainWindow.m_CurrentProjectPageUC.SaveProject()
   End Sub

   Private Sub RawPartBtn_Click(sender As Object, e As RoutedEventArgs) Handles RawPartBtn.Click
      m_MainWindow.TestOff()
      m_MainWindow.DragRettangleOff()
      m_CurrProjPage.CurrProjGrid.Visibility = Windows.Visibility.Hidden
      m_CurrProjPage.CurrentProjectPageGrid.Children.Remove(Me)
      m_CurrProjPage.CurrentProjectPageGrid.Children.Add(m_MainWindow.m_RawPartPage)
      m_MainWindow.m_ActivePage = MainWindow.Pages.RawPart
      m_MainWindow.m_PrevActivePage = MainWindow.Pages.CadCut
      ' Se modalità fast grid, elimino tutti i pezzi
      If m_CadCutMode = CadCutModes.FastGrid Then
         Dim nRawId As Integer = CamAuto.GetCurrentRaw()
         Dim nPartId As Integer = EgtGetFirstPartInRawPart( nRawId)
         While nPartId <> GDB_ID.NULL
            Dim nNextPartId = EgtGetNextPartInRawPart( nPartId)
            ' Rimuovo dal grezzo
            EgtRemovePartFromRawPart(nPartId)
            ' Rimuovo le lavorazioni
            EraseMachinings(nPartId)
            'Cancello
            EgtErase(nPartId)
            ' Passo al successivo
            nPartId = nNextPartId
         End While
      End If
   End Sub

   Private Sub DrawBtn_Click(sender As Object, e As RoutedEventArgs) Handles DrawBtn.Click
      m_MainWindow.TestOff()
      m_MainWindow.DragRettangleOff()
      m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_CurrentProjectPageUC)
      m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_DrawPageUC)
      m_MainWindow.m_ActivePage = MainWindow.Pages.Draw
   End Sub

   Private Sub ImportBtn_Click(sender As Object, e As RoutedEventArgs) Handles ImportBtn.Click
      m_MainWindow.TestOff()
      m_MainWindow.DragRettangleOff()
      m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_CurrentProjectPageUC)
      m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_ImportPageUC)
      m_MainWindow.m_ActivePage = MainWindow.Pages.Import
      m_MainWindow.m_PrevActivePage = MainWindow.Pages.CadCut
   End Sub

   Private Sub CadCutPage_Unloaded(sender As Object, e As RoutedEventArgs)
      ' Chiudo tutti gli UserControl attivi
      Select Case m_CadCutMode
         Case CadCutModes.Nest
            CadCutPageGrid.Children.Remove(m_NestPage)
         Case CadCutModes.FastGrid
            CadCutPageGrid.Children.Remove(m_FastGridSlabManager)
         Case CadCutModes.Split
            CadCutPageGrid.Children.Remove(m_SplitPage)
         Case CadCutModes.MoveRawPart
            CadCutPageGrid.Children.Remove(m_MoveRawPartPage)
      End Select
   End Sub

   Private Sub SplitBtn_Click(sender As Object, e As RoutedEventArgs) Handles SplitBtn.Click
      m_MainWindow.DragRettangleOff()
      m_CurrProjPage.m_SceneButtons.MeasureBtn.IsChecked = False
      EgtDeselectObj(m_NestPage.m_CurrSelectedCurv)
      m_ProjectMgr.TestBtn.IsChecked = False
      m_MainWindow.m_CadCutPageUC.m_NestPage.m_bSelectCurv = False
      Select Case m_CadCutMode
         Case CadCutModes.Nest, CadCutModes.FastGrid
            ' Passo alla pagina Split
            If (m_MainWindow.m_OptionsPageUC.ThemesCmBx.SelectedIndex = 0) Then
               SplitImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/Nest.png", UriKind.Relative))
            Else
               SplitImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/NewIcons/Nest.png", UriKind.Relative))
            End If
            If m_CadCutMode = CadCutModes.Nest Then
               CadCutPageGrid.Children.Remove(m_NestPage)
            Else
               CadCutPageGrid.Children.Remove(m_FastGridSlabManager)
            End If
            CadCutPageGrid.Children.Add(m_SplitPage)
            m_CadCutMode = CadCutModes.Split
            ' Carico eventuale manipolatore pezzi
            EgtDisableModified()
            LoadVacuumCups()
            EgtEnableModified()
            ' Se nessuna modifica, entro in modalità visualizzazione
            If m_CurrProjPage.GetOrderMachiningFlag() Then
               ' Imposto flag di visualizzazione
               m_SplitPage.m_bShow = True
               ' altrimenti riparto da capo
            Else
               ' Cancello tutto
               EraseMachinings(GDB_ID.NULL)
               ' Reinserisco tutte le lavorazioni
               Dim nWarn As Integer = 0
               AddMachinings(GDB_ID.NULL, nWarn)
               If nWarn = 1 Then m_CurrProjPage.SetWarningMessage(EgtMsg(MSG_SPLITPAGEUC + 11)) ' Lama troppo grande per utilizzo ventosa
               ' Imposto flag di modifica
               m_SplitPage.m_bShow = False
            End If
            ' Aggiorno visualizzazione
            EgtDraw()
         Case CadCutModes.Split
            ' Cancello eventuale manipolatore pezzi
            EgtDisableModified()
            RemoveVacuumCups()
            EgtEnableModified()
            ' Passo alla pagina Nest
            If (m_MainWindow.m_OptionsPageUC.ThemesCmBx.SelectedIndex = 0) Then
               SplitImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/Split.png", UriKind.Relative))
            Else
               SplitImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/NewIcons/Split.png", UriKind.Relative))
            End If
            CadCutPageGrid.Children.Remove(m_SplitPage)
            If GetPrivateProfileInt(S_FASTGRID, K_FG_ENABLE, 0, m_MainWindow.GetIniFile()) <> 0 And
            GetPrivateProfileInt(S_FASTGRID, K_FG_ACTIVE, 0, m_MainWindow.GetIniFile()) <> 0 Then
               CadCutPageGrid.Children.Add(m_FastGridSlabManager)
               m_CadCutMode = CadCutModes.FastGrid
            Else
               CadCutPageGrid.Children.Add(m_NestPage)
               m_CadCutMode = CadCutModes.Nest
            End If
         Case CadCutModes.MoveRawPart
            ' consentito solo se in spostamento finale pezzi
            If Not m_SplitPage.m_bOnAuxTab Then Return
            ' Passo alla pagina Nest
            If (m_MainWindow.m_OptionsPageUC.ThemesCmBx.SelectedIndex = 0) Then
               SplitImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/Split.png", UriKind.Relative))
            Else
               SplitImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/NewIcons/Split.png", UriKind.Relative))
            End If
            CadCutPageGrid.Children.Remove(m_MoveRawPartPage)
            CadCutPageGrid.Children.Add(m_NestPage)
            m_CadCutMode = CadCutModes.Nest
      End Select
   End Sub

End Class
