Imports System.Windows.Forms.Integration
Imports System.Collections.ObjectModel
Imports System.Reflection
Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5
Imports EgtBEAMWALL.Core

Public Class MyToolDbWindowVM
   Inherits ToolDbWindowVM

   Private Const SETUP_FILEEXTENSION As String = ".stu"

#Region "CONSTRUCTOR"

   Sub New(sMachineDirPath As String, sMachineIniPath As String, ProjectSceneContext As Integer, Optional sMatType As String = "Wood", Optional bVisibleSysNotes As Boolean = False)
      MyBase.New(sMachineDirPath, sMachineIniPath, ProjectSceneContext, sMatType, bVisibleSysNotes)
   End Sub

#End Region    ' Constructor

#Region "METHODS"

   ' Funzione che aggiorna setup default con tool che hanno parametro Active
   Public Overrides Function UpdateDefSetup() As Boolean
      InitSetupFile()
      ' Creo Setup
      Dim DefSetup As New SetUpVM
      ' inizializzo ambiente setup
      DefSetup.InitSetUp()
      ' resetto lista utensili e posizioni
      DefSetup.ToolsList.Clear()
      DefSetup.ClearAllPos()
      DefSetup.LoadMachineTools()
      ' ciclo sugli utensili del db
      For Each ToolFamily In ToolsList
         For Each Tool In ToolFamily.Items
            Dim ToolItem As EgtWPFLib5.ToolTreeViewItem = DirectCast(Tool, EgtWPFLib5.ToolTreeViewItem)
            ' verifico se Active
            If ToolItem.Active Then
               For Each TF In DefSetup.ToolsList
                  For TIndex = TF.Items.Count - 1 To 0 Step -1
                     Dim T As ToolItem = DirectCast(TF.Items(TIndex), ToolItem)
                     If ToolItem.Name = T.Name Then
                        DefSetup.ToolDoubleClick(T)
                     End If
                  Next
               Next
            End If
         Next
      Next
      Dim DefSetupPath As String = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & CurrentMachine.sMachineName & "\SetUp"
      Dim DefSetupName As String = String.Empty
      EgtUILib.GetPrivateProfileString(S_SETUP, K_DEFAULT, "", DefSetupName, CurrentMachine.sMachIniFile)
      DefSetupPath += "\" & DefSetupName & SETUP_FILEEXTENSION
      If Not DefSetup.Save(DefSetupPath) Then EgtOutLog("Impossible to save default setup.")
      Return True
   End Function

   ' Inizializzo file Setup
   Private Sub InitSetupFile()
      ' verifico che il file di configurazione attrezzaggio (lua) della macchina esista
      If Not File.Exists(CurrentMachine.sMachScriptDir & "\" & SETUP_LUA) Then
         EgtOutLog("SetUp error: SetUp configuration file doesn't exist ")
         MessageBox.Show(EgtMsg(MSG_SETUPERRORS + 7), EgtMsg(MSG_SETUPERRORS + 1), MessageBoxButton.OK, MessageBoxImage.Error)
         Return
      End If
      ' carico Lua che contiene le funzioni per ottenere le posizioni valide dell'utensile selezionato,
      ' e testa e uscita dell'utensile attrezzato
      EgtLuaExecFile(CurrentMachine.sMachScriptDir & "\" & SETUP_LUA)
      ' verifico che le teste riportate in configurazione esistano
      Dim Index As Integer = 1
      Dim nErr As Integer = 0
      While nErr = 0
         Dim sHead As String = String.Empty
         nErr = 999
         EgtLuaSetGlobIntVar("STU.INDEX", Index)
         EgtLuaCallFunction("STU.GetTcPosHeadGroupFromPos")
         ' Leggo variabili
         EgtLuaGetGlobStringVar("STU.HEAD", sHead)
         EgtLuaGetGlobIntVar("STU.ERR", nErr)
         If nErr = 0 Then
            If EgtGetHeadExitCount(sHead) = 0 Then
               MessageBox.Show(EgtMsg(MSG_SETUPERRORS + 8), EgtMsg(MSG_SETUPERRORS + 1), MessageBoxButton.OK, MessageBoxImage.Error)
               Return
            End If
         End If
         Index += 1
      End While
      ' Verifico esistenza direttorio per attrezzaggi
      Dim sDir As String = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & CurrentMachine.sMachineName & "\SetUp"
      If Not Directory.Exists(sDir) Then
         Try
            Directory.CreateDirectory(sDir)
         Catch ex As Exception
            EgtOutLog("Error in SetupDir creation " & ex.ToString())
            Return
         End Try
      End If

   End Sub

   Public Overrides Sub OnCursorPos(ByVal sender As Object, ByVal sCursorPos As String) 'Handles m_ToolScene.OnCursorPos
      Map.refMyStatusBarVM.SetCurrPos(sCursorPos)
   End Sub

#End Region    ' Methods

End Class
