Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5
Imports EgtBEAMWALL.Core

Public Class MacroFeatureWndVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   ' File template per scrittura Macro
   Friend Const MACROTEMPLATE_FILE As String = "MacroTemplate.ini"
   Friend Const DATETIME As String = "%DATE_TIME%"
   Friend Const MACRO_NAME As String = "%MACRO_NAME%"
   Friend Const MACRO_ORIG As String = "%x,y,z%"
   Friend Const MACRO_DIRX As String = "%xx,xy,xz%"
   Friend Const MACRO_DIRY As String = "%yx,yy,yz%"
   Friend Const MACRO_DIRZ As String = "%zx,zy,zz%"
   'Friend Const MACRO_L As String = "%L%"
   'Friend Const MACRO_W As String = "%W%"
   'Friend Const MACRO_H As String = "%H%"
   'Friend Const MACRO_ID As String = "%Id%"
   Friend Const MACRO_VPAR As String = "%vPar%"
   Friend Const MACRO_SPAR As String = "%sPar%"
   Friend Const MACRO_VPARQ As String = "%vParQ%"
   Friend Const MACRO_ADDFEATURE As String = "%AddFeature%"

   Friend Event m_CloseWindow(bDialogResult As Boolean)

   Private m_FeatureList As New ObservableCollection(Of BTLFeatureVM)
   Public ReadOnly Property FeatureList As ObservableCollection(Of BTLFeatureVM)
      Get
         Return m_FeatureList
      End Get
   End Property

   Private m_nSelFeature As BTLFeatureVM = Nothing
   Public Property nSelFeature As BTLFeatureVM
      Get
         Return m_nSelFeature
      End Get
      Set(value As BTLFeatureVM)
         If value IsNot m_nSelFeature Then
            m_nSelFeature = value
         End If
      End Set
   End Property

   Private m_sFeatureName As String
   Public Property sFeatureName As String
      Get
         Return m_sFeatureName
      End Get
      Set(value As String)
         If value <> m_sFeatureName Then
            m_sFeatureName = value
         End If
      End Set
   End Property

   ' Definizione comandi
   Private m_cmdOk As ICommand

#End Region    ' FIELDS & PROPERTIES

#Region "CONSTRUCTOR"

   Sub New()
      For Each FeatureItem In Map.refProjectVM.BTLStructureVM.SelBTLPart.BTLFeatureVMList
         FeatureList.Add(FeatureItem)
      Next
   End Sub

#End Region    ' CONSTRUCTOR

#Region "COMMANDS"

#Region "Ok"

   Public ReadOnly Property Ok_Command As ICommand
      Get
         If m_cmdOk Is Nothing Then
            m_cmdOk = New Command(AddressOf Ok)
         End If
         Return m_cmdOk
      End Get
   End Property

   Public Sub Ok()
      'verifico che tutti i campi contengano un valore valido
      If Not IsNothing(nSelFeature) AndAlso Not String.IsNullOrEmpty(sFeatureName) Then
         WriteMacro(sFeatureName)
         RaiseEvent m_CloseWindow(True)
      Else
         MessageBox.Show(EgtMsg(61870), EgtMsg(30007))
      End If
   End Sub

#End Region ' Ok

#End Region    ' COMMANDS 

#Region "METHODS"

#Region "Ok"

   Friend Sub WriteMacro(sMacroName As String)
      ' inizio routine di scrittura
      Dim FileContent As String() = File.ReadAllLines(Map.refMainWindowVM.MainWindowM.sMacroDir & "\" & MACROTEMPLATE_FILE)
      Dim FileName As String = CalcBeamPrivateProfileGRP(nSelFeature.nSelGRP) & "." & nSelFeature.nPRC & FILENAMESEPARATOR & sMacroName
      For LineIndex As Integer = 0 To FileContent.Count - 1
         Dim sCurrLine As String = FileContent(LineIndex)
         FileContent(LineIndex) = FileContent(LineIndex).Replace(DATETIME, System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"))
         FileContent(LineIndex) = FileContent(LineIndex).Replace(MACRO_NAME, FileName)
         FileContent(LineIndex) = FileContent(LineIndex).Replace(MACRO_ORIG, LenToString(nSelFeature.frFRAME.Orig.x, 6) & ", " &
                                                                             LenToString(nSelFeature.frFRAME.Orig.y, 6) & ", " &
                                                                             LenToString(nSelFeature.frFRAME.Orig.z, 6))
         FileContent(LineIndex) = FileContent(LineIndex).Replace(MACRO_DIRX, DoubleToString(nSelFeature.frFRAME.VersX.x, 6) & ", " &
                                                                             DoubleToString(nSelFeature.frFRAME.VersX.y, 6) & ", " &
                                                                             DoubleToString(nSelFeature.frFRAME.VersX.z, 6))
         FileContent(LineIndex) = FileContent(LineIndex).Replace(MACRO_DIRY, DoubleToString(nSelFeature.frFRAME.VersY.x, 6) & ", " &
                                                                             DoubleToString(nSelFeature.frFRAME.VersY.y, 6) & ", " &
                                                                             DoubleToString(nSelFeature.frFRAME.VersY.z, 6))
         FileContent(LineIndex) = FileContent(LineIndex).Replace(MACRO_DIRZ, DoubleToString(nSelFeature.frFRAME.VersZ.x, 6) & ", " &
                                                                             DoubleToString(nSelFeature.frFRAME.VersZ.y, 6) & ", " &
                                                                             DoubleToString(nSelFeature.frFRAME.VersZ.z, 6))
         If FileContent(LineIndex).Contains(MACRO_VPAR) Or FileContent(LineIndex).Contains(MACRO_SPAR) Or FileContent(LineIndex).Contains(MACRO_VPARQ) Then
            ' recupero i valori di vPar e vParQ (e sPar) e li separo opportunamente
            Dim vPar() As Double = Nothing
            Dim sPar As String = String.Empty
            Dim vParQ() As String = Nothing
            Dim s_vPar As String = String.Empty
            Dim s_vParQ As String = String.Empty
            nSelFeature.BTLFeatureM.CalcParamArray(vPar, sPar, vParQ)
            For Each vParItem In vPar
               s_vPar = s_vPar & " " & DoubleToString(vParItem, 3) & ","
            Next
            s_vPar = s_vPar.TrimStart()
            s_vPar = s_vPar.TrimEnd(", ")
            For Each vParQItem In vParQ
               s_vParQ = s_vParQ & " '" & vParQItem & "',"
            Next
            s_vParQ = s_vParQ.TrimStart()
            s_vParQ = s_vParQ.TrimEnd(", ")
            FileContent(LineIndex) = FileContent(LineIndex).Replace(MACRO_VPAR, s_vPar)
            FileContent(LineIndex) = FileContent(LineIndex).Replace(MACRO_SPAR, If(String.IsNullOrWhiteSpace(sPar), "''", sPar))
            FileContent(LineIndex) = FileContent(LineIndex).Replace(MACRO_VPARQ, s_vParQ)
         End If
         FileContent(LineIndex) = FileContent(LineIndex).Replace(MACRO_ADDFEATURE, nSelFeature.nSelGRP & ", " &
                                                                                   nSelFeature.nPRC & ", " &
                                                                                   nSelFeature.nSelSIDE & ", " &
                                                                                   "'" & nSelFeature.sDES & "', " &
                                                                                   "MACRO.PROCID") 'nSelFeature.ParentPart.NewProcId() &
      Next
      File.WriteAllLines(Map.refMainWindowVM.MainWindowM.sMacroDir & "\" & FileName & ".lua", FileContent, Text.Encoding.UTF8)
   End Sub

#End Region ' Ok

#End Region    ' METHODS 

End Class