Imports System.ComponentModel
Imports System.IO
Imports CMSControl.App.Runtime

Imports System.Security

Namespace Config
    Class Options

        Public Shared CurrentOptions As Options
        Public Shared sPathFile As String

        Private _Machine_Enabled As Boolean
        ''' <summary>Maintenance Machine</summary>
        Public ReadOnly Property Machine_Enabled() As Boolean
            Get
                Return Me._Machine_Enabled
            End Get
        End Property

        Private _Maintenance_Enabled As Boolean
        ''' <summary>Opzione Maintenance</summary>
        Public ReadOnly Property Maintenance_Enabled() As Boolean
            Get
                Return Me._Maintenance_Enabled
            End Get
        End Property

        Private _OpPanel_Enabled As Boolean
        ''' <summary>Opzione OpPanel</summary>
        Public ReadOnly Property OpPanel_Enabled() As Boolean
            Get
                Return Me._OpPanel_Enabled
            End Get
        End Property

        Private _Launch_Enabled As Boolean
        ''' <summary>Opzione Launch</summary>
        Public ReadOnly Property Launch_Enabled() As Boolean
            Get
                Return Me._Launch_Enabled
            End Get
        End Property
        Private _TcpSocket_Enabled As Boolean
        ''' <summary>Opzione TcpSocket</summary>
        Public ReadOnly Property TcpSocket_Enabled() As Boolean
            Get
                Return Me._TcpSocket_Enabled
            End Get
        End Property
        Private _QsMonitor_Enabled As Boolean
        ''' <summary>Opzione QsMonitor</summary>
        Public ReadOnly Property QsMonitor_Enabled() As Boolean
            Get
                Return Me._QsMonitor_Enabled
            End Get
        End Property
        Private _ToolTable_Enabled As Boolean
        ''' <summary>Opzione ToolTable</summary>
        Public ReadOnly Property ToolTable_Enabled() As Boolean
            Get
                Return Me._ToolTable_Enabled
            End Get

        End Property

        Private _ToolTable_Famiglie As Boolean
        ''' <summary>Opzione Famiglie</summary>
        Public ReadOnly Property ToolTable_Famiglie() As Boolean
            Get
                Return Me._ToolTable_Famiglie
            End Get
        End Property

        Private _ToolTable_Vita As Boolean
        ''' <summary>Opzione Vita</summary>
        Public ReadOnly Property ToolTable_Vita() As Boolean
            Get
                Return (Me._ToolTable_Vita_S Or Me._ToolTable_Vita_N Or Me._ToolTable_Vita_m)
            End Get
        End Property

        Private _ToolTable_Vita_S As Boolean
        ''' <summary>Opzione Vita a Tempo</summary>
        Public ReadOnly Property ToolTable_Vita_S() As Boolean
            Get
                Return Me._ToolTable_Vita_S
            End Get
        End Property

        Private _ToolTable_Vita_N As Boolean
        ''' <summary>Opzione Vita a Cicli o Colpi</summary>
        Public ReadOnly Property ToolTable_Vita_N() As Boolean
            Get
                Return Me._ToolTable_Vita_N
            End Get
        End Property

        Private _ToolTable_Vita_M As Boolean
        ''' <summary>Opzione Vita a Profilo</summary>
        Public ReadOnly Property ToolTable_Vita_M() As Boolean
            Get
                Return Me._ToolTable_Vita_M
            End Get
        End Property

        Private _ToolTable_AttrezzaggioAutomatico As Boolean
        ''' <summary>Opzione Attrezzaggio Automatico</summary>
        Public ReadOnly Property ToolTable_AttrezzaggioAutomatico() As Boolean
            Get
                Return Me._ToolTable_AttrezzaggioAutomatico
            End Get
        End Property
        Private _ToolTable_AttrezzaggioDaFile_Path As String
        ''' <summary>Opzione Percorso Attrezzaggio Da File </summary>
        Public ReadOnly Property ToolTable_AttrezzaggioDaFile_Path() As String
            Get
                Return Me._ToolTable_AttrezzaggioDaFile_Path
            End Get
        End Property

        Private _ToolTable_PercorsoAutoadattativo As Boolean
        ''' <summary>Opzione Percorso Autoadattativo</summary>
        Public ReadOnly Property ToolTable_PercorsoAutoadattativo() As Boolean
            Get
                Return Me._ToolTable_PercorsoAutoadattativo
            End Get
        End Property

        Private _ToolTable_Multiprofilo As Boolean
        ''' <summary>Opzione Multiprofilo</summary>
        Public ReadOnly Property ToolTable_Multiprofilo() As Boolean
            Get
                Return Me._ToolTable_Multiprofilo
            End Get
        End Property

        Private _ToolTable_CompensazioneDinamica As Boolean
        ''' <summary>Opzione Compensazione Dinamica</summary>
        Public ReadOnly Property ToolTable_CompensazioneDinamica() As Boolean
            Get
                Return Me._ToolTable_CompensazioneDinamica
            End Get
        End Property

        Private _ToolTable_Offset As Boolean
        ''' <summary>Opzione Offset</summary>
        Public ReadOnly Property ToolTable_Offset() As Boolean
            Get
                Return Me._ToolTable_Offset
            End Get
        End Property

        Private _ToolTable_FeedMax As Boolean
        ''' <summary>Opzione FeedMax</summary>
        Public ReadOnly Property ToolTable_FeedMax() As Boolean
            Get
                Return Me._ToolTable_FeedMax
            End Get
        End Property

        Private _ToolTable_Gamme As Boolean
        ''' <summary>Opzione Gamme</summary>
        Public ReadOnly Property ToolTable_Gamme() As Boolean
            Get
                Return Me._ToolTable_Gamme
            End Get
        End Property

        Private _ToolTable_Manine As Boolean
        ''' <summary>Opzione Manine</summary>
        Public ReadOnly Property ToolTable_Manine() As Boolean
            Get
                Return Me._ToolTable_Manine
            End Get
        End Property

        Private _ToolTable_GeometryManagement As Control.ToolTable.GeometryManagement
        ''' <summary>Tipo di gestione dati dimensioni utensili</summary>
        Public ReadOnly Property ToolTable_GeometryManagement() As Control.ToolTable.GeometryManagement
            Get
                Return Me._ToolTable_GeometryManagement
            End Get
        End Property

        Private _ToolTable_MaxToolsManaged As Integer
        ''' <summary>Massimo numero di utensili gestibili</summary>
        Public ReadOnly Property ToolTable_MaxToolsManaged() As Integer
            Get
                Return Me._ToolTable_MaxToolsManaged
            End Get
        End Property

        Private _ToolTable_Inch As Boolean
        ''' <summary>Opzione gestione in Pollici</summary>
        Public ReadOnly Property ToolTable_Inch() As Boolean
            Get
                Return Me._ToolTable_Inch
            End Get
        End Property

        Private _ToolTable_Diameter As Boolean
        ''' <summary>Opzione gestione Diametro</summary>
        Public ReadOnly Property ToolTable_Diameter() As Boolean
            Get
                Return Me._ToolTable_Diameter
            End Get
        End Property

        Private _ToolTable_AttrezzaggioInRun As Boolean
        ''' <summary>Opzione modifica con macchina in RUN</summary>
        Public ReadOnly Property ToolTable_AttrezzaggioInRun() As Boolean
            Get
                Return Me._ToolTable_AttrezzaggioInRun
            End Get
        End Property

        Private _ToolTable_AttrezzaggioAssistito As Boolean
        ''' <summary>Opzione Attrezzaggio magazzini inaccessibili tramite macchina</summary>
        Public ReadOnly Property ToolTable_AttrezzaggioAssistito() As Boolean
            Get
                Return Me._ToolTable_AttrezzaggioAssistito
            End Get
        End Property

        Private _ToolTable_SharedDB As Boolean
        ''' <summary>Opzione magazzino utensili condiviso tra più macchine</summary>
        Public ReadOnly Property ToolTable_SharedDB() As Boolean
            Get
                Return Me._ToolTable_SharedDB
            End Get
        End Property
        Private _ToolTable_AllowRemote As Boolean
        ''' <summary>Opzione AllowRemote Tool Table</summary>
        Public ReadOnly Property ToolTable_AllowRemote() As Boolean
            Get
                Return Me._ToolTable_AllowRemote
            End Get
        End Property
        Public Shared Sub CreateOptions(ByVal sPathF As String)
            If CurrentOptions Is Nothing Then
                CurrentOptions = New Options
            End If

            ' Memorizzo il percorso del file INI
            sPathFile = sPathF

            CurrentOptions.LoadOptions()

            ' Gestione QsMonitor (in funzione dell'opzione QsMonitor_Enabled)
            Control.QsMonitor.GestioneQsMonitor()
        End Sub

        'carica le opzioni da file
        Private Sub LoadOptions()
            ''leggo la matricola dal cn
            ''se non è possibile disabilito tutto e chiedo che venga attivata la connessione
            ''se leggo 0 avviso che la metricola deve essere popolata

            ''leggo il codice dal file
            ''valore = decodifica(valoreletto, matricola, passwordpubblica)

            Dim szFileIni As String
            Dim Ini As App.Native.Ini

            ' imposto il nome del file
            szFileIni = sPathFile & "\" & My.Application.Info.AssemblyName & ".ini"

            ' verifico che esista il file
            If My.Computer.FileSystem.FileExists(szFileIni) Then
                'istanzio la classe di gestione del file
                Ini = New App.Native.Ini(szFileIni)
                'leggo i settaggi dal file
                Me._Machine_Enabled = Ini.GetBoolean("OPTIONS", "MACHINE_ENABLED", 0)
                Me._Maintenance_Enabled = Ini.GetBoolean("OPTIONS", "MAINTENANCE_ENABLED", 0)
                Me._OpPanel_Enabled = Ini.GetBoolean("OPTIONS", "OPPANEL_ENABLED", 0)
                Me._Launch_Enabled = Ini.GetBoolean("OPTIONS", "LAUNCH_ENABLED", 0)
                Me._TcpSocket_Enabled = Ini.GetBoolean("OPTIONS", "TCPSOCKET_ENABLED", 0)
                Me._ToolTable_Enabled = Ini.GetBoolean("OPTIONS", "TOOLTABLE_ENABLED", 0)
                Me._ToolTable_Famiglie = Ini.GetBoolean("OPTIONS", "TOOLTABLE_FAMIGLIE", 0)
                Me._ToolTable_Vita_S = Ini.GetBoolean("OPTIONS", "TOOLTABLE_VITA_S", 0)
                Me._ToolTable_Vita_N = Ini.GetBoolean("OPTIONS", "TOOLTABLE_VITA_N", 0)
                Me._ToolTable_Vita_M = Ini.GetBoolean("OPTIONS", "TOOLTABLE_VITA_M", 0)
                Me._ToolTable_Multiprofilo = Ini.GetBoolean("OPTIONS", "TOOLTABLE_MULTIPROFILO", 0)
                Me._ToolTable_AttrezzaggioAutomatico = Ini.GetBoolean("OPTIONS", "TOOLTABLE_ATTREZZAGGIOAUTOMATICO", 0)
                Me._ToolTable_AttrezzaggioDaFile_Path = Ini.GetString("OPTIONS", "TOOLTABLE_ATTREZZAGGIODAFILE_PATH", "")
                Me._ToolTable_PercorsoAutoadattativo = Ini.GetBoolean("OPTIONS", "TOOLTABLE_PERCORSOAUTOADATTATIVO", 0)
                Me._ToolTable_CompensazioneDinamica = Ini.GetBoolean("OPTIONS", "TOOLTABLE_COMPENSAZIONEDINAMICA", 0)
                Me._ToolTable_Offset = Ini.GetBoolean("OPTIONS", "TOOLTABLE_OFFSET", 0)
                Me._ToolTable_FeedMax = Ini.GetBoolean("OPTIONS", "TOOLTABLE_FEEDMAX", 0)
                Me._ToolTable_Gamme = Ini.GetBoolean("OPTIONS", "TOOLTABLE_GAMME", 0)
                Me._ToolTable_Manine = Ini.GetBoolean("OPTIONS", "TOOLTABLE_MANINE", 0)
                Me._ToolTable_GeometryManagement = Ini.GetInteger("OPTIONS", "TOOLTABLE_GEOMETRYMANAGEMENT", 0)
                Me._ToolTable_MaxToolsManaged = Ini.GetInteger("OPTIONS", "TOOLTABLE_MAXTOOLSMANAGED", 0)
                Me._ToolTable_Inch = Ini.GetBoolean("OPTIONS", "TOOLTABLE_INCH", 0)
                Me._ToolTable_Diameter = IIf(CNC.Runtime.NC.Type = CNC.CNC.NcType.OSAI, True, False) 'Ini.GetBoolean("OPTIONS", "TOOLTABLE_DIAMETER", 0)
                Me._ToolTable_AttrezzaggioInRun = Ini.GetBoolean("OPTIONS", "TOOLTABLE_ATTREZZAGGIO_IN_RUN", 0)
                Me._ToolTable_AttrezzaggioAssistito = Ini.GetBoolean("OPTIONS", "TOOLTABLE_ATTREZZAGGIO_ASSISTITO", 0)
                Me._QsMonitor_Enabled = Ini.GetBoolean("OPTIONS", "QSMONITOR_ENABLED", 0)

                ' Se è attiva l'opzione ToolTable_Remote, l'AllowRemote e SharedDB sono ignorati
                If Config.Settings.Settings.ToolTable_Remote Then
                    Me._ToolTable_AllowRemote = False
                    Me._ToolTable_SharedDB = False
                Else
                    Me._ToolTable_AllowRemote = Ini.GetBoolean("OPTIONS", "TOOLTABLE_ALLOWREMOTE", 0)
                    Me._ToolTable_SharedDB = Ini.GetBoolean("OPTIONS", "TOOLTABLE_SHAREDDB", 0)
                End If
            Else
            Log.WW("ERR LoadOptions", String.Format("File {0} not found", szFileIni), True)
            End If

        End Sub

        'verifica che le opzioni sul cn siano coerenti, se no le corregge
        Private Sub CheckOptions()
            ''leggo le opzioni dal cn

            ''verifico che siano uguali a quelle del file, se no modifico il cn e avviso l'utente della modifica
        End Sub


    End Class
End Namespace
