Imports System.Text

Public Class ghost
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer
    Dim debugger As DebugWindow
    Dim screenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
    Dim screenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
    Dim eyeclosed As Boolean = False
    Dim ghostallignment As String = ReadConfig("ghost.alignmenttodesktop")
    '////Control clothes and expression
    Public clothid As Integer = 0
    Public clothid2 As Integer = 0
    Public expressid As Integer = 0
    Dim fileloc As String = ""
    '/////Declare library
    Dim cc As New ChatController
    Dim rc As New RenderController


    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseMove
        If IsFormBeingDragged Then
            Dim temp As Point = New Point()
            If ghostallignment = "fixed" Then
                temp.X = Me.Location.X
            Else
                temp.X = Me.Location.X + (e.X - MouseDownX)
            End If

            If ghostallignment = "toground" Or ghostallignment = "fixed" Then
                temp.Y = Me.Location.Y
            Else
                temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            End If
            Me.Location = temp
            temp = Nothing
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs)
        TCPComm.Show()
    End Sub

    Private Sub ghost_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'SyncBox.Show()
        Me.Left = screenWidth - Me.Width
        Me.Top = screenHeight - Me.Height
        '//initiation clothes id and backdrop
        ApplyLastLayer()
        Me.TopMost = True
        If ReadConfig("ghost.transparent.enable") = True Then
            Me.TransparencyKey = Color.FromArgb(ReadConfig("ghost.transparent.r"), ReadConfig("ghost.transparent.g"), ReadConfig("ghost.transparent.b"))
        End If
        Me.AllowDrop = True
    End Sub

    Private Sub ghost_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDoubleClick
        'GhostMenu.Show()
        rc.delay(0.1)
        ResetExpression.Enabled = False
        IsFormBeingDragged = False 'Prevent ArOZ get stuck with the mouse
        cc.RandomChat("chat") 'default chat library
    End Sub

    Public Sub StartCountDownReset()
        ResetExpression.Enabled = True
    End Sub
    Public Sub UpdateRender()
        rc.UpdateRender(clothid, clothid2, expressid)
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'Eye Blinking Control System
        Dim rc As New RenderController
        If eyeclosed = True And Timer1.Interval = 30 Then
            rc.UpdateRender(clothid, clothid2, expressid)
            Timer1.Interval = 4500
            eyeclosed = False
            'DebugWindow.TextBox1.AppendText("clothid=" & clothid & vbNewLine)

        ElseIf eyeclosed = False And Timer1.Interval = 4500 Then
            rc.UpdateRender(clothid, clothid2, 100)
            Timer1.Interval = 30
        ElseIf eyeclosed = True And Timer1.Interval = 400 Then
            rc.UpdateRender(clothid, clothid2, 100)
            Timer1.Interval = 30
        Else
            rc.UpdateRender(clothid, clothid2, 101)
            eyeclosed = True
            Timer1.Interval = 400
            'DebugWindow.TextBox1.AppendText("clothid=" & clothid & vbNewLine)
        End If
        GC.Collect()
    End Sub

    Public Sub ApplyLastLayer()
        On Error Resume Next
        Dim rconfig As New ArConfig
        Dim al As String = Application.StartupPath & "\Config\"
        Dim rc As New RenderController
        Dim clothesid As Integer = rconfig.ReadConfig(al, "currentl1").ToString.Replace("surface", "")
        Dim clothesidl2 As Integer = rconfig.ReadConfig(al, "currentl2").ToString.Replace("surface", "")
        clothid = clothesid
        clothid2 = clothesidl2
        expressid = 0
        rc.UpdateRender(clothid, clothesidl2, 0)

    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 Function ReadAPI(aroname As String, apiReleativeDirectory As String, item As String)
        Dim arc As New ArConfig
        Dim al As String = Application.StartupPath & "\" & apiReleativeDirectory
        Return arc.ReadAPI(aroname, al, item).ToString.Trim
    End Function
    Public Sub WriteAPI(aroname As String, apiReleativeDirectory As String, item As String, newcontent As String)
        Dim arc As New ArConfig
        Dim al As String = Application.StartupPath & "\" & apiReleativeDirectory
        arc.WriteAPI(aroname, al, item, newcontent)
    End Sub
    Private Sub DropPanel_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
        Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
        Dim sb As New StringBuilder
        For Each path In files
            sb.Append(path & "|")
        Next
        fileloc = sb.ToString()
        TCPComm.Show()
        TCPComm.TextBox7.Text = fileloc
        ' TCPComm.WindowState = FormWindowState.Minimized
        balloon.DisplayText("如要把這些檔案傳送到伺服器，按 Send File")
    End Sub
    Private Sub DropPanel_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub

    Private Sub LocControl_Tick(sender As Object, e As EventArgs) Handles LocControl.Tick
        If ghostallignment = "free" Then

        ElseIf ghostallignment = "toground" Then
            If Me.Top <> screenHeight - Me.Height Then
                Me.Top = screenHeight - Me.Height
            End If
        ElseIf ghostallignment = "fixed" Then
            Me.Left = screenWidth - Me.Width
            Me.Top = screenHeight - Me.Height

        End If
    End Sub

    Private Sub ghost_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick
        On Error Resume Next
        If e.Button = Me.MouseButtons.Right Then
            GhostMenu.Show()
        End If
    End Sub

    Private Sub ResetExpression_Tick(sender As Object, e As EventArgs) Handles ResetExpression.Tick
        expressid = 0
        ResetExpression.Enabled = False
    End Sub

    Private Sub APIUpdate_Tick(sender As Object, e As EventArgs) Handles APIUpdate.Tick
        Dim pluginapirx As String = ReadAPI("api", "plugins\API\", "balloon.rx")
        If pluginapirx <> "" Then
            balloon.DisplayText(pluginapirx)
            WriteAPI("api", "plugins\API\", "balloon.rx", "")
        End If

    End Sub

End Class
