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

Public Module CurrentMachine

   ' Nome macchina corrente
   Private m_sMachineName As String = String.Empty
   ' Direttorio macchina corrente
   Private m_sMachDir As String = String.Empty
   ' File ini della macchina
   Private m_sMachIniFile As String = String.Empty
   ' File ini dei parametri macchina
   Private m_sMachParamIniFile As String = String.Empty
   ' File lettura Ts3Data
   Private m_sGetTs3DataFile As String = String.Empty
   ' File Ts3Data
   Private m_sTs3DataFile As String = String.Empty
   ' Cartella degli script
   Private m_sMachScriptDir As String = String.Empty
   ' Cartella dei setup
   Private m_sMachSetupDir As String = String.Empty
   ' Tipo macchina
   Private m_nType As MachineType

   ' Posizione vista
   Private m_nViewDir As VT
   Public ReadOnly Property ViewDir As VT
      Get
         Return m_nViewDir
      End Get
   End Property
   ' Posizione riferimento principale grezzo su tavola
   Private m_OrigCorner As MCH_CR
   Public ReadOnly Property OrigCorner As MCH_CR
      Get
         Return m_OrigCorner
      End Get
   End Property
   ' Offset da origine tavola verso l'interno in X e Y
   Private m_dOrigOffsX As Double = 0
   Public ReadOnly Property OrigOffsX As Double
      Get
         Return m_dOrigOffsX
      End Get
   End Property
   Private m_dOrigOffsY As Double = 0
   Public ReadOnly Property OrigOffsY As Double
      Get
         Return m_dOrigOffsY
      End Get
   End Property
   ' Posizione origine del grezzo per posizionamento pezzi
   Private m_NestingCorner As MCH_CR
   Public ReadOnly Property NestingCorner As MCH_CR
      Get
         Return m_NestingCorner
      End Get
   End Property
   ' Diametro fori lunghi
   Private m_dHorDrillDiam As Double = 0
   Public ReadOnly Property dHorDrillDiam As Double
      Get
         Return m_dHorDrillDiam
      End Get
   End Property
   ' Spessore minimo grezzo
   Private m_dMinRawH As Double = 0
   Public ReadOnly Property dMinRawH As Double
      Get
         Return m_dMinRawH
      End Get
   End Property
   ' Spessore massimo grezzo
   Private m_dMaxRawH As Double = 0
   Public ReadOnly Property dMaxRawH As Double
      Get
         Return m_dMaxRawH
      End Get
   End Property
   ' Offset minimo tra travi
   Private m_dMinOffset As Double = 0
   Public ReadOnly Property dMinOffset As Double
      Get
         Return m_dMinOffset
      End Get
   End Property
   ' Attivazione Priority pareti
   Private m_bIsEnabledPriority As Boolean = 0
   Public ReadOnly Property bIsEnabledPriority As Boolean
      Get
         Return m_bIsEnabledPriority
      End Get
   End Property

#Region "Proprietà che leggono e scrivono i valori anche da o su file ini"

   Friend ReadOnly Property sMachineName As String
      Get
         Return m_sMachineName
      End Get
   End Property

   Friend ReadOnly Property sMachDir As String
      Get
         Return m_sMachDir
      End Get
   End Property

   Friend ReadOnly Property sMachIniFile As String
      Get
         Return m_sMachIniFile
      End Get
   End Property

   Friend ReadOnly Property sMachParamIniFile As String
      Get
         Return m_sMachParamIniFile
      End Get
   End Property

   Friend ReadOnly Property sTs3DataFile As String
      Get
         Return m_sTs3DataFile
      End Get
   End Property

   Friend ReadOnly Property sMachScriptDir As String
      Get
         Return m_sMachScriptDir
      End Get
   End Property

   Friend ReadOnly Property sMachSetupDir As String
      Get
         Return m_sMachSetupDir
      End Get
   End Property

#End Region

   Friend ReadOnly Property nType As MachineType
      Get
         Return m_nType
      End Get
   End Property

   Private m_MachTableList As New ObservableCollection(Of MachTable)
   Public Property MachTableList As ObservableCollection(Of MachTable)
      Get
         Return m_MachTableList
      End Get
      Set(value As ObservableCollection(Of MachTable))
         m_MachTableList = value
      End Set
   End Property

#Region "Init"

   Sub InitCurrentMachine(sMachinesRootDir As String, sMachineName As String, nMachineType As MachineType)
      ' Nome macchina corrente
      m_sMachineName = sMachineName
      ' Impostazione direttorio macchina
      m_sMachDir = sMachinesRootDir & "\" & sMachineName
      ' Impostazione path MachIni file
      m_sMachIniFile = sMachinesRootDir & "\" & sMachineName & "\" & sMachineName & ".ini"
      ' Impostazione tipo macchina
      m_nType = nMachineType
      ' Impostazione path MachParamIni file
      If nType = MachineType.BEAM Then
         m_sMachParamIniFile = sMachinesRootDir & "\" & sMachineName & "\" & K_BEAM & "\" & MACH_INI_FILE_NAME
         m_sGetTs3DataFile = sMachinesRootDir & "\" & sMachineName & "\" & K_BEAM & "\" & GETTS3DATA_FILE_NAME
         m_sTs3DataFile = sMachinesRootDir & "\" & sMachineName & "\" & K_BEAM & "\" & TS3DATA_FILE
      ElseIf nType = MachineType.WALL Then
         m_sMachParamIniFile = sMachinesRootDir & "\" & sMachineName & "\" & K_WALL & "\" & MACH_INI_FILE_NAME
         m_sGetTs3DataFile = sMachinesRootDir & "\" & sMachineName & "\" & K_WALL & "\" & GETTS3DATA_FILE_NAME
         m_sTs3DataFile = sMachinesRootDir & "\" & sMachineName & "\" & K_WALL & "\" & TS3DATA_FILE
      Else
         ' Se macchina di tipo BOTH prendo quello presente in Beam, se esiste, altrimenti prendo quello in Wall
         Dim sBeamMachParamIniFile = sMachinesRootDir & "\" & sMachineName & "\" & K_BEAM & "\" & MACH_INI_FILE_NAME
         Dim sWallMachParamIniFile = sMachinesRootDir & "\" & sMachineName & "\" & K_WALL & "\" & MACH_INI_FILE_NAME
         Dim sBeamGetTs3File = sMachinesRootDir & "\" & sMachineName & "\" & K_BEAM & "\" & GETTS3DATA_FILE_NAME
         Dim sWallGetTs3File = sMachinesRootDir & "\" & sMachineName & "\" & K_WALL & "\" & GETTS3DATA_FILE_NAME
         Dim sBeamTs3DataFile = sMachinesRootDir & "\" & sMachineName & "\" & K_BEAM & "\" & TS3DATA_FILE
         Dim sWallTs3DataFile = sMachinesRootDir & "\" & sMachineName & "\" & K_WALL & "\" & TS3DATA_FILE
         If nType = MachineType.BOTH Then
            m_sMachParamIniFile = If(File.Exists(sBeamMachParamIniFile), sBeamMachParamIniFile, sWallMachParamIniFile)
            m_sGetTs3DataFile = If(File.Exists(sBeamGetTs3File), sBeamGetTs3File, sWallGetTs3File)
            m_sTs3DataFile = If(File.Exists(sBeamTs3DataFile), sBeamTs3DataFile, sWallTs3DataFile)
         End If
      End If
      ' Impostazione path Script dir
      m_sMachScriptDir = sMachinesRootDir & "\" & sMachineName & "\" & K_SCRIPT
      ' Impostazione path Setup dir
      m_sMachSetupDir = sMachinesRootDir & "\" & sMachineName & "\" & K_SETUP

      ' crea l'elenco dei parametri della macchina corrente
      CreateMachParams()

      ' leggo parametri macchina da BeamData
      If nType = MachineType.BEAM OrElse nType = MachineType.BOTH Then
         ' Assegno i dati
         EgtLuaCreateGlobTable("GWD")
         EgtLuaSetGlobStringVar("GWD.BASEDIR", Map.refMainWindowVM.MainWindowM.sBeamRoot)
         EgtLuaSetGlobStringVar("GWD.MACHINE", m_sMachineName)
         ' Eseguo lo script
         Dim sExecPath As String = Map.refMainWindowVM.MainWindowM.sBeamRoot & "\" & GETBEAMDATA_FILE_NAME
         Dim bOk As Boolean = False
         If EgtLuaExecFile(sExecPath) Then
            ' Recupero i risultati
            Dim nErr As Integer = 999
            EgtLuaGetGlobIntVar("GWD.ERR", nErr)
            bOk = (nErr = 0)
            If Not bOk Then EgtOutLog("GetBeamData Err=" & nErr.ToString())
         Else
            bOk = False
            EgtOutLog("Error executing GetBeamData script " & sExecPath)
         End If
         If bOk Then
            EgtLuaGetGlobNumVar("GWD.OVM_MID", m_dMinOffset)
            Dim nTemp As Integer = 0
            EgtLuaGetGlobIntVar("GWD.SIMUL_VIEW_DIR", nTemp)
            Select Case nTemp
               Case 0
                  m_nViewDir = VT.ISO_SW
               Case 1
                  m_nViewDir = VT.ISO_NW
               Case 2
                  m_nViewDir = VT.ISO_SW
               Case 3
                  m_nViewDir = VT.ISO_NE
               Case 4
                  m_nViewDir = VT.ISO_SE
            End Select
         Else
            m_nViewDir = VT.ISO_SW
            m_dMinOffset = 5.4
         End If
         ' Cancello tavola globale
         EgtLuaResetGlobVar("GWD")
         ' aggiorno parametro in gestore viste
         Core.ViewPanelVM.UpdateMachParam(m_nViewDir)
      
      ' leggo parametri macchina da WallData
      ElseIf nType = MachineType.WALL Then
         ' Assegno i dati
         EgtLuaCreateGlobTable("GWD")
         EgtLuaSetGlobStringVar("GWD.BASEDIR", Map.refMainWindowVM.MainWindowM.sWallRoot)
         EgtLuaSetGlobStringVar("GWD.MACHINE", m_sMachineName)
         ' Eseguo lo script
         Dim sExecPath As String = Map.refMainWindowVM.MainWindowM.sWallRoot & "\" & GETWALLDATA_FILE_NAME
         Dim bOk As Boolean = False
         If EgtLuaExecFile(sExecPath) Then
            ' Recupero i risultati
            Dim nErr As Integer = 999
            EgtLuaGetGlobIntVar("GWD.ERR", nErr)
            bOk = (nErr = 0)
            If Not bOk Then EgtOutLog("GetWallData Err=" & nErr.ToString())
         Else
            bOk = False
            EgtOutLog("Error executing GetWallData script " & sExecPath)
         End If
         If bOk Then
            EgtLuaGetGlobIntVar("GWD.HOR_DRILL_DIAM", m_dHorDrillDiam)
            Dim nTemp As Integer = 0
            EgtLuaGetGlobIntVar("GWD.SIMUL_VIEW_DIR", nTemp)
            Select Case nTemp
               Case 0
                  m_nViewDir = VT.ISO_SW
               Case 1
                  m_nViewDir = VT.ISO_NW
               Case 2
                  m_nViewDir = VT.ISO_SW
               Case 3
                  m_nViewDir = VT.ISO_NE
               Case 4
                  m_nViewDir = VT.ISO_SE
            End Select
            Dim sTemp As String = ""
            EgtLuaGetGlobStringVar("GWD.ORIG_CORNER", sTemp)
            Select Case sTemp
               Case "TL"
                  m_OrigCorner = MCH_CR.TL
               Case "TR"
                  m_OrigCorner = MCH_CR.TR
               Case "BL"
                  m_OrigCorner = MCH_CR.BL
               Case Else  ' "BR"
                  m_OrigCorner = MCH_CR.BR
            End Select
            m_dOrigOffsX = 0
            EgtLuaGetGlobNumVar("GWD.DELTA_X", m_dOrigOffsX)
            m_dOrigOffsY = 0
            EgtLuaGetGlobNumVar("GWD.DELTA_Y", m_dOrigOffsY)
            EgtLuaGetGlobStringVar("GWD.NESTING_CORNER", sTemp)
            Select Case sTemp
               Case "TL"
                  m_NestingCorner = MCH_CR.TL
               Case "TR"
                  m_NestingCorner = MCH_CR.TR
               Case "BL"
                  m_NestingCorner = MCH_CR.BL
               Case Else ' "BR"
                  m_NestingCorner = MCH_CR.BR
            End Select
            EgtLuaGetGlobNumVar("GWD.MIN_HEIGHT", m_dMinRawH)
            EgtLuaGetGlobNumVar("GWD.MAX_HEIGHT", m_dMaxRawH)
            If EgtLuaGetGlobNumVar("GWD.BTL_PRIORITY", nTemp) Then
               m_bIsEnabledPriority = nTemp > 0
            Else
               m_bIsEnabledPriority = False
            End If
         Else
            m_nViewDir = VT.ISO_SW
            m_OrigCorner = MCH_CR.BR
            m_dOrigOffsX = 0
            m_dOrigOffsY = 0
            m_NestingCorner = MCH_CR.BR
            m_dMinRawH = 5
            m_dMaxRawH = 500
            m_bIsEnabledPriority = False
         End If
         ' Cancello tavola globale
         EgtLuaResetGlobVar("GWD")
         ' aggiorno parametro in gestore viste
         Core.ViewPanelVM.UpdateMachParam(m_nViewDir)
         ' carico valori default opzioni nesting
         Map.refOptimizePanelVM.bLDIntersOther = GetMainPrivateProfileInt(S_NEST, K_LDINTERSOTHER, 0)
         Map.refOptimizePanelVM.dMinScore = GetMainPrivateProfileInt(S_NEST, K_MINSCORE, 0)
      End If

   End Sub

#End Region 'Init

#Region "Methods"

   ' funzione che crea l'elenco dei parametri Macchina
   Friend Sub CreateMachParams()
      MachTableList.Clear()
      If String.IsNullOrWhiteSpace(m_sMachParamIniFile) OrElse Not File.Exists(m_sMachParamIniFile) Then
         EgtOutLog("Impossible loading Mach parameters: MachData.ini file not found or not readable")
         Return
      End If
      Dim NewMachParam As MachParam = Nothing
      Dim MachParamList As New ObservableCollection(Of MachParam)
      Dim TableIndex = 1
      Dim ParamIndex As Integer = 1
      Dim TableName As String = String.Empty
      ' carico lua ts3data
      Dim bTs3DataLoaded As Boolean = False
      EgtLuaCreateGlobTable("GTSD")
      EgtLuaSetGlobStringVar("GTSD.TS3PATH", m_sTs3DataFile)
      Dim sExecPath As String = m_sGetTs3DataFile
      bTs3DataLoaded = File.Exists(m_sTs3DataFile) AndAlso EgtLuaExecFile(sExecPath)
      If Not bTs3DataLoaded Then
         EgtOutLog("Error executing Ts3Data file " & m_sTs3DataFile)
      End If

      ' verifico che ci sia una table con l'indice designato e ne leggo il nome
      While GetMachPrivateProfileString(TableIndex, K_NAME, "", TableName)
         ' leggo tutti i parametri della table
         While MachParamIniFile.GetMachPrivateProfileParam(TableIndex, ParamIndex, NewMachParam, TableName, bTs3DataLoaded)
            MachParamList.Add(NewMachParam)
            ParamIndex += 1
         End While
         MachTableList.Add(New MachTable(TableName, MachParamList))
         ' aggiorno indici e resetto lista per lettura dell'eventuale table successiva
         TableIndex += 1
         ParamIndex = 1
         MachParamList = New ObservableCollection(Of MachParam)
      End While
      ' Cancello tavola globale
      EgtLuaResetGlobVar("GTSD")
   End Sub

#End Region 'Methods

End Module
