Imports HomeSeer.PluginSdk
Public Class frmScriptExport
    'Private Sub frmScriptExport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '    ' Me.tmrStart.Start()
    'End Sub
    'Private Sub tmrStart_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    ' Me.tmrStart.Stop()


    'End Sub
    Private Sub clbScripts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clbScripts.SelectedIndexChanged
        Dim s As String

        s = Me.clbScripts.Items(Me.clbScripts.SelectedIndex).ToString       'selected script name
        Me.lblScript.Text = s
        Me.txtScript.Text = ""
        Me.txtScript.Visible = False
        For Each l As String In Exports(s)
            Me.txtScript.AppendText(l & vbCrLf)
        Next
        Me.txtScript.Visible = True
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
        For i As Integer = 0 To Me.clbScripts.Items.Count - 1
            Me.clbScripts.SetItemCheckState(i, CheckState.Unchecked)
        Next
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Hide()
    End Sub
    Private Sub btnSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectAll.Click
        For i As Integer = 0 To Me.clbScripts.Items.Count - 1
            Me.clbScripts.SetItemCheckState(i, CheckState.Checked)
        Next
    End Sub
    Private Sub btnExportScripts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportScripts.Click
        ExportTheScripts()
    End Sub
    Private Sub ExportTheScripts()
        Dim s, p, m, ss As String
        Dim aScript(5) As String
        Dim SomeChecked As Boolean = False
        Dim ii As Integer
        Dim fName As String, aDate As Date, Duration As TimeSpan, StartTime As DateTime, Msecs As Double

        StartTime = Now
        p = My.Settings.ExportDirectory
        m = "The following scripts have been exported to " & My.Settings.ExportDirectory & vbCrLf & vbCrLf
        theMainForm.MyLog(m, False, Logging.ELogType.info)
        '' Loop through all selected Scripts
        For i As Integer = 0 To Me.clbScripts.Items.Count - 1
            If Me.clbScripts.GetItemChecked(i) Then
                SomeChecked = True
                '' Export this Script
                s = Me.clbScripts.Items(i).ToString
                ii = 0
                For j As Integer = 0 To Exports(s).Count - 1
                    ss = Exports(s)(j)
                    If ss.ToUpper.Contains("@DONOTEXPORT") Then
                        '' Skip Exporting this line
                    Else
                        '' Export this line
                        ReDim Preserve aScript(ii)
                        aScript(ii) = ss
                        ii = ii + 1
                    End If
                Next j
                ss = cboFiles.Text
                ss = ss.Substring(ss.Length - 3)
                fName = p & s & ss
                Try
                    IO.File.WriteAllLines(fName, aScript)
                    m = m & "      " & s & ss & vbCrLf
                    theMainForm.MyLog("      " & s & ss, False, Logging.ELogType.Info)
                Catch ex As Exception
                    MsgBox("Write Failed for: " & fName & vbCrLf & "EX: " & ex.Message)
                    Exit Sub
                End Try
                '' Check destination modified date/time to see if it seems that it was copied
                aDate = IO.File.GetLastWriteTime(fName)
                Duration = aDate - StartTime
                Msecs = Math.Abs(Duration.TotalMilliseconds)
                If Msecs > 10000 Then
                    MsgBox("File may not have been copied, LastModifiedDate looks hinky: " & p & s & ".vb" & vbCrLf)
                    MsgBox("    Destination File Time is now: " & aDate.ToString)
                End If
            End If
        Next
        If SomeChecked Then
            MsgBox(m, , "tenScripting4 Export")
        Else
            MsgBox("NO SCRIPTS SELECTED FOR EXPORT", , "tenScripting4 Export")
        End If
        'If FastExport Then
        '    '' If request was to just export the main selected script, then go do it, don't wait for button press

        '    Me.Hide()
        'End If
    End Sub
    Private Sub cboFiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFiles.SelectedIndexChanged
        Dim sFile, dFile As String, Found As Boolean
        Dim s1 As String

        Me.clbScripts.Items.Clear()
        sFile = Me.cboFiles.Items(Me.cboFiles.SelectedIndex).ToString
        For Each sd As ScriptData In Scripts.Values
            dFile = IO.Path.GetFileName(sd.Filename)
            If dFile = sFile Then
                '' Only add if not there already (multiple methods for same class)
                Found = False
                For Each s As String In Me.clbScripts.Items
                    If s = sd.ClassName Then
                        Found = True
                        Exit For
                    End If
                Next
                If Not Found Then
                    '' Add Script to Selection Box
                    Me.clbScripts.Items.Add(sd.ClassName)
                    s1 = theMainForm.cboClasses.Items(theMainForm.cboClasses.SelectedIndex).ToString
                    If s1 = Me.clbScripts.Items(Me.clbScripts.Items.Count - 1).ToString Then
                        '' This Script is selected on Main Window, so select it here
                        If FastExport Then
                            '' Only select it for Fast Export button, not when dialog is displayed
                            Me.clbScripts.SetItemCheckState(Me.clbScripts.Items.Count - 1, CheckState.Checked)   'check it
                            Me.clbScripts.SelectedIndex = Me.clbScripts.Items.Count - 1   'select it, force script source to be displayed
                            s1 = s1
                        End If
                    End If
                    s1 = s1
                End If
            End If
        Next
        If FastExport Then
            '' If request was to just export the main selected script, then go do it, don't wait for button press
            ExportTheScripts()
            tmrCloseForm.Start()
        End If
    End Sub
    Private Sub btnScriptFolder_Click(sender As System.Object, e As System.EventArgs) Handles btnScriptFolder.Click
        Dim frmSF As frmScriptFolder
        frmSF = New frmScriptFolder
        frmSF.ShowDialog()
        frmSF = Nothing
    End Sub

    Private Sub tmrCloseForm_Tick(sender As Object, e As EventArgs) Handles tmrCloseForm.Tick
        tmrCloseForm.Stop()
        Me.Hide()
    End Sub
End Class