Imports System.IO

Public Class PluginMenu
    Dim rc As New RenderController
    Dim apppath As String = Application.StartupPath & "\"
    Private Sub PluginMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Left = GhostMenu.Left - Me.Width
        Me.Top = GhostMenu.Top
        Me.BackgroundImage = rc.LoadBitMap("plugin_background")
        Me.ForeColor = Color.FromArgb(ReadConfig("plugin.background.font.color.r"), ReadConfig("plugin.background.font.color.g"), ReadConfig("plugin.background.font.color.b"))
        Me.TopMost = True
        '///////////////////////
        CreateLabels(GetPluginList())
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If (ctrl.GetType() Is GetType(Label)) Then
                Dim txt As Label = CType(ctrl, Label)
                txt.ForeColor = Me.ForeColor
            End If
        Next
    End Sub
    Public Function ReadConfig(item As String)
        Dim arc As New ArConfig
        Dim al As String = Application.StartupPath & "\config\"
        Return arc.ReadConfig(al, item).ToString.Trim
    End Function

    Public Sub CreateLabels(plugins As String())
        Dim counter As Integer = 1
        Dim Topdis As Integer = 30
        For Each plugin As String In plugins
            Dim lblTask As New Label
            lblTask.Name = "Plugin" & counter
            lblTask.BackColor = Color.Transparent
            lblTask.Font = New Font(lblTask.Font.Name, 9)
            lblTask.Size = New Size(150, 13)
            lblTask.Cursor = Cursors.Hand
            lblTask.Location = New Point(5, Topdis + counter * 13)
            lblTask.Text = ">" & GetlastFolderName(plugin)
            Me.Controls.Add(lblTask)
            counter += 1
            AddHandler lblTask.Click, AddressOf lblTask_Click
        Next

    End Sub
    Private Sub lblTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim clickedLabel As Label = DirectCast(sender, Label)
        Dim getlabeltext As String = clickedLabel.Text.Replace(">", "")
        'MessageBox.Show("Label text Clicked was: " & clickedLabel.Text)
        'MsgBox(apppath & "plugins\" & getlabeltext & "\" & getlabeltext & ".exe")
        If My.Computer.FileSystem.FileExists(apppath & "plugins\" & getlabeltext & "\" & getlabeltext & ".exe") Then
            Process.Start(apppath & "plugins\" & getlabeltext & "\" & getlabeltext & ".exe")
        ElseIf My.Computer.FileSystem.FileExists(apppath & "plugins\" & getlabeltext & "\api.aro") Then
            ghost.expressid = 2
            ghost.UpdateRender()
            balloon.DisplayText("這是API文件檔啦智障！")
            ghost.ResetExpression.Enabled = True
        Else
            balloon.DisplayText("找不到插件的說。你肯定插件沒有安裝錯誤嗎？")
        End If
        GhostMenu.Close()
        Me.Close()
    End Sub

    Public Function GetlastFolderName(dir As String)
        Dim tempdir As String = ""
        If dir.Substring(dir.Length - 1, 1) = "\" Then
            dir.Substring(0, dir.Length - 1)
        End If
        tempdir = dir.Substring(dir.LastIndexOf("\") + 1, dir.Length - dir.LastIndexOf("\") - 1)
        Return tempdir
    End Function
    Public Function GetPluginList()
        Dim plugins As String()
        plugins = Directory.GetDirectories(apppath & "plugins\")
        Return plugins
    End Function

    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
        GhostMenu.Timer1.Enabled = True
        Me.Close()
    End Sub
End Class