Imports EgtUILib

Public Class SecondaryComponentPageUC

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

   'Riferimento alla MainComponentPage
   Private m_MainComponentPage As MainComponentPageUC

   ' Variabile che contiene il nome del componente selezionato nella MainPage
   Private m_sMainCompoName As String

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

   ' dopo aver definito la forma della componente definisco quali parametri usare per la sua definizione
   Private Sub SecondaryComponentPage_Loaded(sender As Object, e As RoutedEventArgs)

      m_MainComponentPage = m_MainWindow.m_DrawPageUC.m_MainComponentPage

      'Riferimento al Button della MainComponentPage che ha aperto la SecondaryComponentPage
      Dim CurrentMainBtn As Button = m_MainComponentPage.CurrentBtn

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

      m_sMainCompoName = m_MainComponentPage.m_sCurrCompoFam

      'Assegnazione immagine e testo ai Button
      Dim nCount As Integer = GetPrivateProfileInt(m_sMainCompoName, K_CMP_COUNT, 0, m_MainWindow.GetIniFile())
      ClearButton(nCount)
      For index As Integer = 1 To nCount
         Dim CustomThickness As Thickness = ThicknessConverter.ConvertFromString("0")
         Dim nCompoName As Integer = GetPrivateProfileInt(m_sMainCompoName, S_EXTCOMPO & index, 0, m_MainWindow.GetIniFile())
         Dim sCompo As String = ""
         Dim sCompoImage As String = ""
         m_MainComponentPage.GetPrivateProfileCompo(m_sMainCompoName, S_EXTCOMPO & index, nCompoName, sCompo, sCompoImage, m_MainWindow.GetIniFile())
         Dim sCompoImageSource As ImageSource

         'verifico presenza immagine e la aggiungo
         If sCompoImage.Length <> 0 Then
            Try
               sCompoImageSource = ImageConverter.ConvertFromString(m_MainWindow.GetResourcesDir() & "\" & sCompoImage)
               GetImage(index).Height = 65
               GetImage(index).Width = 65
               GetImage(index).Source = sCompoImageSource
               GetLabel(index).SetValue(Grid.ColumnProperty, 1)
               GetLabel(index).SetValue(Grid.ColumnSpanProperty, 1)
            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
               GetLabel(index).SetValue(Grid.ColumnProperty, 0)
               GetLabel(index).SetValue(Grid.ColumnSpanProperty, 2)
            End Try
         Else
            'Se non c'è l'immagine azzero la distanza tra testo e immagine
            CustomThickness.Right = 0
            GetImage(index).Margin = CustomThickness
            GetLabel(index).SetValue(Grid.ColumnProperty, 0)
            GetLabel(index).SetValue(Grid.ColumnSpanProperty, 2)
         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 20 lo taglio
               If sCompoName.Length > 20 Then
                  sCompoName = sCompoName.Substring(0, 20)
               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 30 lo taglio
               If sCompoName.Length > 30 Then
                  sCompoName = sCompoName.Substring(0, 30)
               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

      ' Disabilito bottoni angoli di fianco e tagli da sotto
      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

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

   Private Sub ClearButton(nCount As Integer)
      For index As Integer = 1 To nCount + 1
         GetLabel(index).Text = String.Empty
         GetImage(index).Source = Nothing
         GetButton(index).Visibility = Windows.Visibility.Visible
      Next
      For index As Integer = nCount + 1 To 9
         GetLabel(index).Text = String.Empty
         GetImage(index).Source = Nothing
         GetButton(index).Visibility = Windows.Visibility.Hidden
      Next
   End Sub

   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, Compo9.Click
      Dim CurrentBtn As Button = e.Source
      Dim nCompoName As Integer
      Dim sCompo As String = String.Empty
      Dim sCompoImage As String = String.Empty
      m_MainComponentPage.GetPrivateProfileCompo(m_sMainCompoName, "Compo" & GetButtonNumber(CurrentBtn.Name), nCompoName, sCompo, sCompoImage, m_MainWindow.GetIniFile())

      m_MainWindow.m_DrawPageUC.LeftButtonGrd.Children.Remove(Me)
      ' Imposto componente esterno
      m_MainWindow.m_DrawPageUC.m_bInternalCompo = False

      ' Carico componente
      m_MainWindow.m_DrawPageUC.SelectedComponent(sCompo)
      m_MainWindow.m_DrawPageUC.m_sMainCompo = sCompo
      m_sCurrCompo = "Compo" & GetButtonNumber(CurrentBtn.Name)
      m_MainWindow.m_DrawPageUC.VariablesGrd.Children.Add(m_MainWindow.m_DrawPageUC.m_CompoDimension)

      'm_MainWindow.m_DrawPageUC.DimensionBtn.IsChecked = True
      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

   End Sub

   Private Sub BackBtn_Click(sender As Object, e As RoutedEventArgs) Handles BackBtn.Click
      m_MainWindow.m_DrawPageUC.LeftButtonGrd.Children.Remove(Me)
      m_MainWindow.m_DrawPageUC.m_ActiveComponentPage = DrawPageUC.Pages.MainComponent
      m_MainWindow.m_DrawPageUC.LeftButtonGrd.Children.Add(m_MainComponentPage)
   End Sub

    
End Class
