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

Public Class SpecialPanelVM

   Private m_ButtonList As New List(Of ButtonItem)
   Public ReadOnly Property ButtonList As List(Of ButtonItem)
      Get
         Return m_ButtonList
      End Get
   End Property

   Sub New()
      ' Creo riferimento a questa classe in Map
      Map.SetRefSpecialPanelVM(Me)
      ' se attivo, inizializzo i bottoni leggendoli da file ini
      If Map.refMainWindowVM.MainWindowM.bSpecialPanel Then
         Dim BtnIndex As Integer = 1
         Dim CurrBtn As ButtonItem = Nothing
         While GetPrivateProfileButton(S_SPECIAL, K_BUTTON & BtnIndex, "", CurrBtn)
            m_ButtonList.Add(CurrBtn)
            BtnIndex += 1
         End While
      End If
   End Sub

#Region "METHODS"

   Public Function SetSpecialPanelButtonsVisibility(IsMachMode As Boolean) As Boolean
      Dim bSpecialPanel_Visible As Boolean = False
      For Each BtnItem In m_ButtonList
         ' verifico il valore di nDrawMachOrBoth del bottone e IsMachMode per rendere visibile o meno il bottone in ButtonList
         Select Case BtnItem.nDrawMachOrBoth
            Case 0 ' bottone nascosto
               BtnItem.m_Btn_Visibility = Visibility.Collapsed
            Case 1 ' bottone visibile solo in Draw
               BtnItem.m_Btn_Visibility = If(Not IsMachMode, Visibility.Visible, Visibility.Collapsed)
            Case 2 ' bottone visibile solo in Machining
               BtnItem.m_Btn_Visibility = If(IsMachMode, Visibility.Visible, Visibility.Collapsed)
            Case 3 ' bottone visibile sia in Draw che in Machining
               BtnItem.m_Btn_Visibility = Visibility.Visible
         End Select
         ' se anche uno solo è visibile anche lo SpecialPanel dovrà esserlo
         If BtnItem.Btn_Visibility = Visibility.Visible Then bSpecialPanel_Visible = True
         'BtnItem.OnPropertyChanged(NameOf(BtnItem.Btn_Visibility))
         BtnItem.NotifyPropertyChanged(NameOf(BtnItem.Btn_Visibility))
      Next
      Return bSpecialPanel_Visible
   End Function

   Friend Sub SpecialPanelIsEnabled(SpecialPanelBtn_IsEnabled As Boolean)
      For Each BtnItem In m_ButtonList
         BtnItem.m_Btn_IsEnabled = SpecialPanelBtn_IsEnabled
         'BtnItem.OnPropertyChanged(NameOf(BtnItem.Btn_IsEnabled))
         BtnItem.NotifyPropertyChanged(NameOf(BtnItem.Btn_IsEnabled))
      Next
   End Sub

   Public Function GetPrivateProfileButton(sSection As String, sKey As String, sBaseDir As String, ByRef ReadButtonItem As ButtonItem) As Boolean
      ReadButtonItem = Nothing
      Dim sVal As String = String.Empty
      GetMainPrivateProfileString(sSection, sKey, "", sVal)
      If String.IsNullOrWhiteSpace(sVal) Then Return False
      Dim sItems() As String = sVal.Split(","c)
      If sItems.Count() >= 1 Then
         Dim sLuaPath As String = sItems(0)
         Dim sImagePath As String = If(sItems.Count() >= 2, sItems(1), "")
         Dim sToolTip As String = If(sItems.Count() >= 3, sItems(2), "")
         Dim sDrawMachOrBoth As String = If(sItems.Count() >= 4 AndAlso Not String.IsNullOrWhiteSpace(sItems(3)), sItems(3), "1")
         If Not String.IsNullOrWhiteSpace(sBaseDir) And Not String.IsNullOrWhiteSpace(sLuaPath) Then
            If sLuaPath.Contains(".lua") Then sLuaPath = sBaseDir & "\" & sLuaPath
            If Not String.IsNullOrWhiteSpace(sImagePath) Then sImagePath = sBaseDir & "\" & sImagePath
         End If
         ReadButtonItem = New ButtonItem(sSection, sLuaPath, sImagePath, sToolTip, sDrawMachOrBoth)
         Return True
      End If
      Return False
   End Function

#End Region    ' Methods

End Class

Public Class ButtonItem
   Inherits VMBase

   Friend Shared WithEvents m_ProjectVM As ProjectVM
   Private Shared m_sCurrBarName As String

   Private m_sBarName As String
   Private m_sImagePath As String
   Public ReadOnly Property ImagePath As String
      Get
         Return m_sImagePath
      End Get
   End Property
   Private m_sLuaCmdPath As String
   Private m_sToolTip As String
   Public ReadOnly Property ToolTip As String
      Get
         Return m_sToolTip
      End Get
   End Property
   Private m_nDrawMachOrBoth As Integer
   Public ReadOnly Property nDrawMachOrBoth As Integer
      Get
         Return m_nDrawMachOrBoth
      End Get
   End Property
   Friend m_Btn_Visibility As Visibility
   Public ReadOnly Property Btn_Visibility As Visibility
      Get
         Return m_Btn_Visibility
      End Get
   End Property

   Friend m_Btn_IsEnabled As Boolean = True
   Public ReadOnly Property Btn_IsEnabled As Boolean
      Get
         Return m_Btn_IsEnabled
      End Get
   End Property

   ' Definizione comandi
   Private m_cmdLuaExec As ICommand

   Sub New( sBarName As String, sLuaCmdPath As String, sImagePath As String, sToolTip As String, sDrawMachOrBoth As String)
      m_sBarName = sBarName
      If File.Exists(sImagePath) Then
         ' per lasciare libere le immagini le copio (potrebbe fallire perchè bloccate da altro eseguibile)
         Dim sNewPath As String = Path.Combine(Map.refMainWindowVM.MainWindowM.sResourcesRoot, sBarName & "_" & Path.GetFileName(sImagePath))
         Try
            File.Copy( sImagePath, sNewPath, True)
         Catch ex As Exception
         End Try
         m_sImagePath = sNewPath
      Else
         m_sImagePath = Map.refMainWindowVM.MainWindowM.sResourcesRoot & "\" & sImagePath
      End If
      m_sLuaCmdPath = sLuaCmdPath
      m_sToolTip = sToolTip
      If Not Integer.TryParse(sDrawMachOrBoth, m_nDrawMachOrBoth) Then m_nDrawMachOrBoth = 0
   End Sub

#Region "COMMANDS"

#Region "LuaExecCommand"

   ''' <summary>
   ''' Returns a command that do New.
   ''' </summary>
   Public ReadOnly Property LuaExecCommand As ICommand
      Get
         If m_cmdLuaExec Is Nothing Then
            m_cmdLuaExec = New Command(AddressOf LuaExec)
         End If
         Return m_cmdLuaExec
      End Get
   End Property

   ''' <summary>
   ''' Execute the New.  This method is invoked by the NewCommand.
   ''' </summary>
   Public Sub LuaExec(ByVal param As Object)
      If String.IsNullOrWhiteSpace(m_sLuaCmdPath) Then Return
      If Not File.Exists(m_sLuaCmdPath) Then Return
      If Not Path.GetExtension(m_sLuaCmdPath).ToLower = ".lua" Then Return
      ' Abilito eventi se comando lua termina con Beam\Process.lua
      m_sCurrBarName = m_sBarName
      Dim bRaiseEvent As Boolean = ( m_sBarName = "Beam"  OrElse m_sBarName = "Wall")
      ' eseguo file Lua
      EgtBEAMWALL.ViewerOptimizer.LuaExec.ExecScript(m_sLuaCmdPath, bRaiseEvent)
      m_sCurrBarName = Nothing
   End Sub

   'Private Shared Sub OnPreControllerExec(sFilePath As String) Handles m_ProjectVM.OnPreControllerExec
   '   If m_sCurrBarName = "Beam" Then
   '      EgtLuaCreateGlobTable("BEAM")
   '      EgtLuaSetGlobStringVar("BEAM.BASEDIR", IniFile.m_sBeamDirPath)
   '      If EgtGetUserLevel() >= 9 AndAlso GetPrivateProfileInt(S_LUA, K_BWSIM, 0) = 1 then EgtLuaSetGlobBoolVar("BEAM.BW", true)
   '   ElseIf m_sCurrBarName = "Wall" Then
   '      EgtLuaCreateGlobTable("WALL")
   '      EgtLuaSetGlobStringVar("WALL.BASEDIR", IniFile.m_sWallDirPath)
   '      If EgtGetUserLevel() >= 9 AndAlso GetPrivateProfileInt(S_LUA, K_BWSIM, 0) = 1 then EgtLuaSetGlobBoolVar("WALL.BW", true)
   '   End If
   'End Sub

   'Private Shared Sub OnPostControllerExec() Handles m_ProjectVM.OnPostControllerExec
   '   If m_sCurrBarName = "Beam" Then
   '      EgtLuaResetGlobVar("BEAM")
   '   ElseIf m_sCurrBarName = "Wall" Then
   '      EgtLuaResetGlobVar("WALL")
   '   End If
   'End Sub

#End Region ' LuaExecCommand

#End Region    ' Commands

End Class