Imports System.IO
Imports System.Collections.ObjectModel
Imports EgtUILib
Imports EgtWPFLib

Public Class ModifStartEndWD

   ' Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Private m_sMsg As String = "Value"
   Private m_sElongation As String = ""
   Private m_bHole As Boolean = False
   Private m_sLeadIn As IdNameStruct = Nothing
   Private m_sLeadOut As IdNameStruct = Nothing
   Private m_sLiTangDist As String = ""
   Private m_sLiPerpDist As String = ""
   Private m_sLoTangDist As String = ""
   Private m_sLoPerpDist As String = ""
   Private m_nOperId As Integer = 0

   Private m_bIsLeadIn As Boolean
   Public Property bIsLeadIn As Boolean
      Get
         Return m_bIsLeadIn
      End Get
      Set(value As Boolean)
         If value <> m_bIsLeadIn Then
            m_bIsLeadIn = value
         End If
      End Set
   End Property

   'Array che contengono le variabili per Combobox
   Dim LeadInType As ObservableCollection(Of IdNameStruct)
   Dim LeadOutType As ObservableCollection(Of IdNameStruct)

   Sub New(Owner As Window, Optional sMsg As String = "", Optional bIsLeadIn As Boolean = False, Optional nOperId As Integer = 0)
      Me.Owner = Owner
      m_sMsg = sMsg
      m_bIsLeadIn = bIsLeadIn
      m_nOperId = nOperId
      InitializeComponent()
   End Sub

   Private Sub EditValueWD_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

      ' Imposto i messaggi letti dal file dei messaggi
      ElongationTxbl.Text = EgtMsg(MSG_SPLITPAGEUC + 35)
      HoleTxbl.Text = EgtMsg(90875)
      LeadInTxbl.Text = EgtMsg(90769)
      LeadOutTxbl.Text = EgtMsg(90771)
      TangDistTxbl.Text = EgtMsg(90890)
      PerpDistTxbl.Text = EgtMsg(90891)

      ' Mostro/Nascondo elementi in base a Inizio/Fine Modif.
      If bIsLeadIn Then
         TitleTxbl.Text = EgtMsg(MSG_SPLITPAGEUC + 33)
         HoleTxbl.Visibility = Windows.Visibility.Visible
         HoleChBx.Visibility = Windows.Visibility.Visible
         LeadInTxbl.Visibility = Windows.Visibility.Visible
         LeadInCmBx.Visibility = Windows.Visibility.Visible
         LeadOutTxbl.Visibility = Windows.Visibility.Hidden
         LeadOutCmBx.Visibility = Windows.Visibility.Hidden
         LeadInType = New ObservableCollection(Of IdNameStruct)(
                                          {New IdNameStruct(MCH_MIL_LI.NONE, EgtMsg(MSG_COMBOBOXPARAM + 42)),
                                           New IdNameStruct(MCH_MIL_LI.LINEAR, EgtMsg(MSG_COMBOBOXPARAM + 46)),
                                           New IdNameStruct(MCH_MIL_LI.TANGENT, EgtMsg(MSG_COMBOBOXPARAM + 47))})
         LeadInCmBx.ItemsSource = LeadInType
         LiTangDistTxBx.Visibility = Windows.Visibility.Visible
         LiPerpDistTxBx.Visibility = Windows.Visibility.Visible
         LoTangDistTxBx.Visibility = Windows.Visibility.Hidden
         LoPerpDistTxBx.Visibility = Windows.Visibility.Hidden
      Else
         TitleTxbl.Text = EgtMsg(MSG_SPLITPAGEUC + 34)
         HoleTxbl.Visibility = Windows.Visibility.Hidden
         HoleChBx.Visibility = Windows.Visibility.Hidden
         LeadInTxbl.Visibility = Windows.Visibility.Hidden
         LeadInCmBx.Visibility = Windows.Visibility.Hidden
         LeadOutTxbl.Visibility = Windows.Visibility.Visible
         LeadOutCmBx.Visibility = Windows.Visibility.Visible
         LeadOutType = New ObservableCollection(Of IdNameStruct)(
                                         {New IdNameStruct(MCH_MIL_LO.NONE, EgtMsg(MSG_COMBOBOXPARAM + 42)),
                                          New IdNameStruct(MCH_MIL_LO.LINEAR, EgtMsg(MSG_COMBOBOXPARAM + 46)),
                                          New IdNameStruct(MCH_MIL_LO.TANGENT, EgtMsg(MSG_COMBOBOXPARAM + 47)),
                                          New IdNameStruct(MCH_MIL_LO.AS_LI, EgtMsg(MSG_COMBOBOXPARAM + 50))})
         LeadOutCmBx.ItemsSource = LeadOutType
         LiTangDistTxBx.Visibility = Windows.Visibility.Hidden
         LiPerpDistTxBx.Visibility = Windows.Visibility.Hidden
         LoTangDistTxBx.Visibility = Windows.Visibility.Visible
         LoPerpDistTxBx.Visibility = Windows.Visibility.Visible
      End If

   End Sub

   Friend Function SetElongation(dVal As Double) As Boolean
      m_sElongation = LenToString(dVal, 3)
      ElongationTxBx.Text = m_sElongation
      Return True
   End Function

   Friend Function GetElongation() As Double
      Dim dVal As Double = 0
      StringToLen(m_sElongation, dVal)
      Return dVal
   End Function

   Friend Function SetHole(dVal As Boolean) As Boolean
      m_bHole = dVal
      HoleChBx.IsChecked = m_bHole
      Return True
   End Function

   Friend Function GetHole() As Boolean
      Dim bVal As Boolean = False
      bVal = m_bHole
      Return bVal
   End Function

   Friend Function SetLiTangDist(dVal As Double) As Boolean
      m_sLiTangDist = LenToString(dVal, 3)
      LiTangDistTxBx.Text = m_sLiTangDist
      Return True
   End Function

   Friend Function GetLiTangDist() As Double
      Dim dVal As Double = 0
      StringToLen(m_sLiTangDist, dVal)
      Return dVal
   End Function

   Friend Function SetLiPerpDist(dVal As Double) As Boolean
      m_sLiPerpDist = LenToString(dVal, 3)
      LiPerpDistTxBx.Text = m_sLiPerpDist
      Return True
   End Function

   Friend Function GetLiPerpDist() As Double
      Dim dVal As Double = 0
      StringToLen(m_sLiPerpDist, dVal)
      Return dVal
   End Function

   Friend Function SetLoTangDist(dVal As Double) As Boolean
      m_sLoTangDist = LenToString(dVal, 3)
      LoTangDistTxBx.Text = m_sLoTangDist
      Return True
   End Function

   Friend Function GetLoTangDist() As Double
      Dim dVal As Double = 0
      StringToLen(m_sLoTangDist, dVal)
      Return dVal
   End Function

   Friend Function SetLoPerpDist(dVal As Double) As Boolean
      m_sLoPerpDist = LenToString(dVal, 3)
      LoPerpDistTxBx.Text = m_sLoPerpDist
      Return True
   End Function

   Friend Function GetLoPerpDist() As Double
      Dim dVal As Double = 0
      StringToLen(m_sLoPerpDist, dVal)
      Return dVal
   End Function

   Friend Function SetLeadInType(nLeadType As Integer) As Boolean
      m_sLeadIn = LeadInType.FirstOrDefault(Function(x) x.Id = nLeadType)
      LeadInCmBx.SelectedItem = m_sLeadIn
      Return True
   End Function

   Friend Function GetLeadInType() As IdNameStruct
      Dim LeadType As IdNameStruct = Nothing
      LeadType = m_sLeadIn
      Return LeadType
   End Function

   Friend Function SetLeadOutType(nLeadType As Integer) As Boolean
      m_sLeadOut = LeadOutType.FirstOrDefault(Function(x) x.Id = nLeadType)
      LeadOutCmBx.SelectedItem = m_sLeadOut
      Return True
   End Function

   Friend Function GetLeadOutType() As IdNameStruct
      Dim LeadType As IdNameStruct = Nothing
      LeadType = m_sLeadOut
      Return LeadType
   End Function

   Private Sub OkBtn_Click(sender As Object, e As RoutedEventArgs) Handles OkBtn.Click
      Dim nMachiningType As Integer
      nMachiningType = EgtGetOperationType(m_nOperId)
      If nMachiningType = MCH_MY.WATERJETTING Then
         m_bHole = HoleChBx.IsChecked
         m_sLeadIn = LeadInCmBx.SelectedItem
         m_sLeadOut = LeadOutCmBx.SelectedItem
         If bIsLeadIn Then
            m_sLiTangDist = LiTangDistTxBx.Text
            m_sLiPerpDist = LiPerpDistTxBx.Text
         Else
            m_sLoTangDist = LoTangDistTxBx.Text
            m_sLoPerpDist = LoPerpDistTxBx.Text
         End If
      End If
      m_sElongation = ElongationTxBx.Text
      DialogResult = True
   End Sub

   Private Sub ExitBtn_Click(sender As Object, e As RoutedEventArgs) Handles ExitBtn.Click
      Dim nMachiningType As Integer
      nMachiningType = EgtGetOperationType(m_nOperId)
      If nMachiningType = MCH_MY.WATERJETTING Then
         m_bHole = False
         m_sLeadIn = Nothing
         m_sLeadOut = Nothing
         If bIsLeadIn Then
            m_sLiTangDist = ""
            m_sLiPerpDist = ""
         Else
            m_sLoTangDist = ""
            m_sLoPerpDist = ""
         End If
      End If
      m_sElongation = ""
      DialogResult = False
   End Sub

End Class
