Imports EgtUILib

Public Class MainComponentPageUC

   'Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)

   'Dichiarazione delle Page UserControl
   Friend m_SecondaryComponentPage As SecondaryComponentPageUC

   ' Flag che indica se è attiva la prima o la seconda pagina
   Friend m_bIsFirstPage As Boolean

   Friend CurrentBtn As Button = Nothing

   ' Variabile che contiene il nome del componente selezionato
   Friend m_sCurrCompoFam As String = String.Empty

   Private Sub MainComponentPage_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized

      'Creazione delle Page UserControl
      m_SecondaryComponentPage = New SecondaryComponentPageUC
      'Posizionemento nella griglia delle Page UserControl
      m_SecondaryComponentPage.SetValue(Grid.ColumnSpanProperty, 2)
      m_SecondaryComponentPage.SetValue(Grid.RowSpanProperty, 4)

      ' Leggo messaggi da file ini
      LabelCompo9.Text = EgtMsg(MSG_COMPONENTPAGEUC + 9)

   End Sub

   Private Sub MainComponentPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      m_bIsFirstPage = True
      LoadPage()
      CurrentBtn = Nothing
      Compo9.SetValue(Grid.ColumnProperty, 0)
      Compo9.SetValue(Grid.ColumnSpanProperty, 2)
      ImageCompo9.Visibility = Windows.Visibility.Hidden
      LabelCompo9.Visibility = Windows.Visibility.Visible
      m_MainWindow.m_DrawPageUC.SideAngleBtn.IsEnabled = False
      m_MainWindow.m_DrawPageUC.FiloTopBtn.IsEnabled = False
      m_MainWindow.m_DrawPageUC.EngraveBtn.IsEnabled = False
      m_MainWindow.m_DrawPageUC.DripCutBtn.IsEnabled = False
   End Sub

   Private Function GetButton(ByVal nInd As Integer) As Button
      Select Case nInd
         Case 1
            Return Compo1
         Case 2
            Return Compo2
         Case 3
            Return Compo3
         Case 4
            Return Compo4
         Case 5
            Return Compo5
         Case 6
            Return Compo6
         Case 7
            Return Compo7
         Case 8
            Return Compo8
         Case Else
            Return Compo9
      End Select
   End Function

   Private Function GetLabel(ByVal nInd As Integer) As TextBlock
      Select Case nInd
         Case 1
            Return LabelCompo1
         Case 2
            Return LabelCompo2
         Case 3
            Return LabelCompo3
         Case 4
            Return LabelCompo4
         Case 5
            Return LabelCompo5
         Case 6
            Return LabelCompo6
         Case 7
            Return LabelCompo7
         Case 8
            Return LabelCompo8
         Case Else
            Return LabelCompo9
      End Select
   End Function

   Private Function GetImage(ByVal nInd As Integer) As Image
      Select Case nInd
         Case 1
            Return ImageCompo1
         Case 2
            Return ImageCompo2
         Case 3
            Return ImageCompo3
         Case 4
            Return ImageCompo4
         Case 5
            Return ImageCompo5
         Case 6
            Return ImageCompo6
         Case 7
            Return ImageCompo7
         Case 8
            Return ImageCompo8
         Case Else
            Return ImageCompo9
      End Select
   End Function

   Friend Function GetIndexFromButton(Button As Button) As Integer
      Select Case Button.Name
         Case "Compo1"
            If m_bIsFirstPage Then
               Return 1
            Else
               Return 9
            End If
         Case "Compo2"
            If m_bIsFirstPage Then
               Return 2
            Else
               Return 10
            End If
         Case "Compo3"
            If m_bIsFirstPage Then
               Return 3
            Else
               Return 11
            End If
         Case "Compo4"
            If m_bIsFirstPage Then
               Return 4
            Else
               Return 12
            End If
         Case "Compo5"
            If m_bIsFirstPage Then
               Return 5
            Else
               Return 13
            End If
         Case "Compo6"
            If m_bIsFirstPage Then
               Return 6
            Else
               Return 14
            End If
         Case "Compo7"
            If m_bIsFirstPage Then
               Return 7
            Else
               Return 15
            End If
         Case Else
            If m_bIsFirstPage Then
               Return 8
            Else
               Return 16
            End If
      End Select
   End Function

   Private Sub Compo_Click(sender As Object, e As RoutedEventArgs) Handles Compo1.Click, Compo2.Click, Compo3.Click, Compo4.Click, Compo5.Click, Compo6.Click, Compo7.Click, Compo8.Click
      CurrentBtn = DirectCast(e.Source, Button)
      Dim CompoName As String = S_COMPO & GetIndexFromButton(CurrentBtn).ToString
      m_sCurrCompoFam = CompoName
      m_SecondaryComponentPage.m_sCurrCompo = String.Empty
      Dim nCount As Integer = GetPrivateProfileInt(CompoName, K_CMP_COUNT, 0, m_MainWindow.GetIniFile())
      If nCount > 1 Then
         m_MainWindow.m_DrawPageUC.LeftButtonGrd.Children.Remove(Me)
         m_MainWindow.m_DrawPageUC.m_ActiveComponentPage = DrawPageUC.Pages.SecondaryComponent
         m_MainWindow.m_DrawPageUC.LeftButtonGrd.Children.Add(m_SecondaryComponentPage)
      Else
         Dim nCompoName As Integer
         Dim sCompo As String = String.Empty
         Dim sCompoImage As String = String.Empty
         GetPrivateProfileCompo(CompoName, "Compo1", nCompoName, sCompo, sCompoImage, m_MainWindow.GetIniFile())

         ' Imposto componente esterno
         m_MainWindow.m_DrawPageUC.m_bInternalCompo = False

         m_MainWindow.m_DrawPageUC.LeftButtonGrd.Children.Remove(Me)
         m_MainWindow.m_DrawPageUC.VariablesGrd.Children.Add(m_MainWindow.m_DrawPageUC.m_CompoDimension)
         m_MainWindow.m_DrawPageUC.SideAngleBtn.IsChecked = False
         m_MainWindow.m_DrawPageUC.FiloTopBtn.IsChecked = False
         m_MainWindow.m_DrawPageUC.EngraveBtn.IsChecked = False
         m_MainWindow.m_DrawPageUC.DripCutBtn.IsChecked = False
         m_MainWindow.m_DrawPageUC.SideAngleBtn.IsEnabled = m_MainWindow.m_DrawPageUC.bEnableSideAngle
         m_MainWindow.m_DrawPageUC.FiloTopBtn.IsEnabled = m_MainWindow.m_CurrentMachine.bMilling
         m_MainWindow.m_DrawPageUC.EngraveBtn.IsEnabled = True
         m_MainWindow.m_DrawPageUC.DripCutBtn.IsEnabled = m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.UNDER_CUT) And
                                                          Not String.IsNullOrWhiteSpace(m_MainWindow.m_CurrentMachine.sCurrDripSaw)
         m_MainWindow.m_DrawPageUC.MessageGrid.Visibility = Windows.Visibility.Visible
         m_MainWindow.m_DrawPageUC.BackBtn.Visibility = Windows.Visibility.Visible
         m_MainWindow.m_DrawPageUC.TopGrd.Visibility = Windows.Visibility.Visible
         m_MainWindow.m_DrawPageUC.PartNameGrd.Visibility = Windows.Visibility.Visible
         m_MainWindow.m_DrawPageUC.PartNumberGrd.Visibility = Windows.Visibility.Visible
         ' Carico componente
         m_MainWindow.m_DrawPageUC.SelectedComponent(sCompo)
      End If
   End Sub

   Public Function GetPrivateProfileCompo(
                            ByVal lpAppName As String,
                            ByVal lpKeyName As String,
                            ByRef lpCompoName As Integer,
                            ByRef lpCompo As String,
                            ByRef lpCompoImage As String,
                            ByVal lpFileName As String) As Boolean

      Dim sVal As String = String.Empty
      GetPrivateProfileString(lpAppName, lpKeyName, "", sVal, lpFileName)
      Dim sItems() As String = sVal.Split(",".ToCharArray)
      If sItems.Count() >= 2 Then
         lpCompoName = 0
         StringToInt(sItems(0), lpCompoName)
         lpCompo = sItems(1)
         If sItems.Count() >= 3 Then
            lpCompoImage = sItems(2)
         Else
            lpCompoImage = String.Empty
         End If
         Return True
      End If
      Return False

   End Function

   Private Sub ClearButton(nCount As Integer)
      Dim nEndIndex As Integer
      If m_bIsFirstPage Then
         nEndIndex = If(nCount < 8, nCount, 8)
      Else
         nEndIndex = If(nCount < 16, nCount - 8, 8)
      End If
      For index As Integer = 1 To nEndIndex
         GetLabel(index).Text = String.Empty
         GetImage(index).Source = Nothing
         GetButton(index).Visibility = Windows.Visibility.Visible
      Next
      For index As Integer = nEndIndex + 1 To 8
         GetLabel(index).Text = String.Empty
         GetImage(index).Source = Nothing
         GetButton(index).Visibility = Windows.Visibility.Hidden
      Next
   End Sub

   Private Sub LoadPage()
      Dim nDeltaSecondPage As Integer = 8
      ' Leggo numero di componenti presenti
      Dim nCompoNumber As Integer = GetPrivateProfileInt(S_COMPO, K_CMP_COUNT, 0, m_MainWindow.GetIniFile())
      ' Calcolo indici a seconda della pagina in cui sono
      If m_bIsFirstPage Then
         ClearButton(nCompoNumber)
      nDeltaSecondPage = 0
      Else
         ClearButton(nCompoNumber)
         nDeltaSecondPage = 8
      End If

      ' Creazione converter da String a ImageSource
      Dim ImageConverter As New ImageSourceConverter
      Dim ThicknessConverter As New ThicknessConverter

      'Assegnazione immagine e testo ai Button
      For index As Integer = 1 To 8
         Dim CustomThickness As New Thickness(0)
         Dim nCompoName As Integer = GetPrivateProfileInt("Compo" & index + nDeltaSecondPage, "Name", 0, m_MainWindow.GetIniFile())
         Dim sCompoImage As String = ""
         Dim sCompoImageSource As ImageSource

         'verifico presenza immagine e la aggiungo
         If GetPrivateProfileString("Compo" & index + nDeltaSecondPage, "Image", "", sCompoImage, m_MainWindow.GetIniFile()) <> 0 Then
            Try
               sCompoImageSource = ImageConverter.ConvertFromString(m_MainWindow.GetResourcesDir() & "\" & sCompoImage)
               GetImage(index).Height = 65
               GetImage(index).Width = 65
               GetImage(index).Source = sCompoImageSource
            Catch ex As Exception
               EgtOutLog("Error loading image " & sCompoImage)
               sCompoImage = String.Empty
               GetImage(index).Height = 0
               GetImage(index).Width = 0
               CustomThickness.Right = 0
               GetImage(index).Margin = CustomThickness
            End Try
         Else
            'Se non c'è l'immagine azzero la distanza tra testo e immagine
            CustomThickness.Right = 0
            GetImage(index).Margin = CustomThickness
         End If

         'verifico presenza testo
         If nCompoName > 0 Then
            Dim sCompoName As String = EgtMsg(MSG_COMPONENTPAGEUC + nCompoName)
            'verifico presenza immagine
            If sCompoImage.Length > 0 Then
               'se la lunghezza del testo è maggiore di 15 lo taglio
               If sCompoName.Length > 15 Then
                  sCompoName = sCompoName.Substring(0, 15)
               End If
               GetLabel(index).Text = sCompoName
               'Se ci sono sia testo che immagine imposto un margine di 10
               CustomThickness.Right = 10
               GetImage(index).Margin = CustomThickness
            Else
               'se l'immagine non c'è e il testo è maggiore di 20 lo taglio
               If sCompoName.Length > 20 Then
                  sCompoName = sCompoName.Substring(0, 20)
               End If
               GetLabel(index).Text = sCompoName
            End If
         Else
            'Se non c'è testo azzero la distanza tra testo e immagine
            CustomThickness.Right = 0
            GetImage(index).Margin = CustomThickness
         End If

      Next

   End Sub

   ' Gestione bottone altri componenti
   Private Sub Compo9_Click(sender As Object, e As RoutedEventArgs) Handles Compo9.Click
      If m_bIsFirstPage Then
         m_bIsFirstPage = False
         LoadPage()
         'Compo9.SetValue(Grid.ColumnProperty, 1)
         'Compo9.SetValue(Grid.ColumnSpanProperty, 1)
         'ImageCompo9.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/NumericKeyboardArrow.png", UriKind.Relative))
         'ImageCompo9.Visibility = Windows.Visibility.Visible
         'LabelCompo9.Visibility = Windows.Visibility.Hidden
         Compo9.SetValue(Grid.ColumnProperty, 0)
         Compo9.SetValue(Grid.ColumnSpanProperty, 2)
         ImageCompo9.Visibility = Windows.Visibility.Hidden
         LabelCompo9.Visibility = Windows.Visibility.Visible
      Else
         m_bIsFirstPage = True
         LoadPage()
         Compo9.SetValue(Grid.ColumnProperty, 0)
         Compo9.SetValue(Grid.ColumnSpanProperty, 2)
         ImageCompo9.Visibility = Windows.Visibility.Hidden
         LabelCompo9.Visibility = Windows.Visibility.Visible
      End If
   End Sub

End Class
