Imports System.Text
Imports HomeSeer.Jui.Types
    Imports HomeSeer.Jui.Views
    Imports HomeSeer.PluginSdk
    Imports HomeSeer.PluginSdk.Logging
    Imports Newtonsoft.Json

''' <inheritdoc cref="AbstractPlugin"/>
''' <summary>
''' The plugin class for tenScripting4 that implements the <see cref="AbstractPlugin"/> base class.
''' </summary>
''' <remarks>
''' This class is accessed by HomeSeer and requires that its name be "HSPI" and be located in a namespace
'''  that corresponds to the name of the executable. 
''' <para>
''' If HomeSeer is unable to find this class, the plugin will not start.
''' </para>
''' </remarks>
Public Class HSPI
    Inherits AbstractPlugin
    ''' <inheritdoc />
    ''' <remarks>
    ''' This ID is used to identify the plugin and should be unique across all plugins
    ''' </remarks>
    Public MainForm As tenScripting
    Delegate Sub delAutoScriptEvent(parms As String)
    Public Overrides ReadOnly Property Id As String
        Get
            Return "tenScripting4"
        End Get
    End Property

    ''' <inheritdoc />
    ''' <remarks>
    ''' This is the readable name for the plugin that is displayed throughout HomeSeer
    ''' </remarks>
    Public Overrides ReadOnly Property Name As String
        Get
            Return "tenScripting4"
        End Get
    End Property

    Public Sub New()
        'Initialize the plugin 
        'Enable internal debug logging to console
        LogDebug = True
        'Setup anything that needs to be configured before a connection to HomeSeer is established
        ' like initializing the starting state of anything needed for the operation of the plugin
        'Such as initializing the settings pages presented to the user (currently saved state is loaded later)
        'InitializeSettingsPages()

        'Or adding an event action or trigger type definition to the list of types supported by your plugin
        'ActionTypes.AddActionType(GetType(WriteLogSampleActionType))
        'TriggerTypes.AddTriggerType(GetType(SampleTriggerType))
    End Sub

    ''' <summary>
    ''' Initialize the starting state of the settings pages for tenScripting4.
    '''  This constructs the framework that the user configurable settings for the plugin live in.
    '''  Any saved configuration options are loaded later in <see cref="Initialize"/> using
    '''  <see cref="AbstractPlugin.LoadSettingsFromIni"/>
    ''' </summary>
    ''' <remarks>
    ''' For ease of use throughout the plugin, all of the view IDs, names, and values (non-volatile data)
    ''' </remarks>
    Private Sub InitializeSettingsPages()
    End Sub
    Protected Overrides Sub Initialize()
        Console.WriteLine("HSPI.Initialize Method Called")
        Status = PluginStatus.Ok()
    End Sub
    Protected Overrides Function OnSettingChange(pageId As String, currentView As AbstractView, changedView As AbstractView) As Boolean
        Return True
    End Function

    ''' <inheritdoc />
    ''' <remarks>
    ''' This plugin does not have a shifting operational state; so this method is not used.
    ''' </remarks>
    Protected Overrides Sub BeforeReturnStatus()
    End Sub

    ''' <inheritdoc />
    ''' <remarks>
    ''' Process any HTTP POST requests targeting pages registered to your plugin.
    ''' <para>
    ''' This is a very flexible process that does not have a predefined structure. The form <see cref="data"/> sends
    '''  from a page is entirely up to you and what works for you.  JSON and Base64 strings are encouraged because
    '''  of how readily available resources are to translate to/from these types. In Javascript, see JSON.stringify();
    '''  and window.btoa();
    ''' </para>
    ''' </remarks>
    Public Overrides Function PostBackProc(page As String, data As String, user As String, userRights As Integer) As String
        Return ""
    End Function

    Public Function AutoRunScript(parm1 As Object) As Object
        ''
        '' This is run from an HS script, to request tenScripting4 to run a script within tenScripting4
        ''
        MainForm.Invoke(New delAutoScriptEvent(AddressOf MainForm.AutoScriptEvent), parm1.ToString)   'run AutoScriptEvent on the UI thread
        Return Nothing
    End Function

End Class