Imports System.IO

Public Class ClothesManager
    Dim ghostlocation As String = Application.StartupPath & "\ghost\" & ReadConfig("ghostfolder") & "\"
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer
    Dim rc As New RenderController
    Dim layer1temp As String = ReadConfig("currentl1")
    Dim layer2temp As String = ReadConfig("currentl2")

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Panel1.MouseDown, Label4.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 Panel1.MouseUp, Label4.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 Panel1.MouseMove, Label4.MouseMove
        If IsFormBeingDragged Then
            Dim temp As Point = New Point()
            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    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
    Private Sub ClothesManager_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim cl1 As String = ReadConfig("currentl1")
        Dim cl2 As String = ReadConfig("currentl2")
        If cl1 = "" Then
            cl1 = "TR"
        End If
        If cl2 = "" Then
            cl2 = "TR"
        End If
        CreatePreview(cl1, cl2)
        GetAllClothes()
        Label3.Text = "Current Setting: " & vbNewLine & "Layer1 = " & cl1 & vbNewLine & "Layer2 = " & cl2

    End Sub
    Private Sub CreatePreview(l1 As String, l2 As String)
        Dim BMP As New Bitmap(175, 278)
        Dim bg As Bitmap = rc.LoadBitMap("surface0")
        Dim layer1 As Bitmap = rc.LoadBitMap(l1)
        Dim layer2 As Bitmap = rc.LoadBitMap(l2)
        bg = ResizeBitmap(bg)
        layer1 = ResizeBitmap(layer1)
        layer2 = ResizeBitmap(layer2)
        'create a graphic base on that
        Dim GR As Graphics = Graphics.FromImage(BMP)

        'draw onto your bmp starting from the background
        GR.DrawImage(My.Resources.TR, 0, 0)

        'set X,y to the coordinate you want your girl to appear
        GR.DrawImage(bg, 0, 0)
        GR.DrawImage(layer1, 0, 0)
        GR.DrawImage(layer2, 0, 0)
        'clear the picturebox
        PictureBox1.BackgroundImage = BMP

        'now that we have draw all our image onto the same bitmap, assign it to your picturebox element
    End Sub
    Function ResizeBitmap(ByVal bitmapToResize As Bitmap)
        Dim bm_dest As New Bitmap(175, 278)

        ' Make a Graphics object for the result Bitmap.
        Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

        ' Copy the source image into the destination bitmap.
        gr_dest.DrawImage(bitmapToResize, 0, 0, 175, 278)

        ' Display the result.
        Return bm_dest
    End Function
    Private Function GetAllClothes()
        Dim fileEntries As String() = Directory.GetFiles(ghostlocation)
        ' Process the list of files found in the directory.
        Dim fileName As String
        For Each fileName In fileEntries
            If fileName.Contains("surface") = True Then
                If fileName.Contains(".png") = True Then
                    fileName = Path.GetFileName(fileName).ToString.Replace(".png", "")
                    If fileName.Replace("surface", "") > 999 Then
                        ListBox1.Items.Add(fileName)
                    End If


                End If

            End If
        Next
    End Function

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
        Me.Close()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        layer1temp = ListBox1.SelectedItem
        CreatePreview(layer1temp, layer2temp)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        layer2temp = ListBox1.SelectedItem
        CreatePreview(layer1temp, layer2temp)
    End Sub

    Private Sub ListBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseClick
        PictureBox2.BackgroundImage = rc.LoadBitMap(ListBox1.SelectedItem)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim arc As New ArConfig
        Dim al As String = Application.StartupPath & "\config\"
        arc.WriteConfig(al, "currentl1", layer1temp)
        arc.WriteConfig(al, "currentl2", layer2temp)
        Me.Hide()
        balloon.DisplayText("請等一下。")
        ghost.Hide()
        rc.delay(3)
        ghost.ApplyLastLayer()
        rc.delay(1)
        ghost.Show()
        balloon.DisplayText("衣服已經換好了。")
        Me.Close()

    End Sub
End Class