Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports System.IO

Friend Class ImportManager
    Inherits System.Windows.Forms.Form
    Public WorkDirectory As String
    Private pixelarray() As Byte


    'UPGRADE_NOTE: graphics was upgraded to graphics_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    Public Sub generatepixelarray()
        Dim pic As Bitmap = CType(Graphic.Image, Bitmap)

        ReDim pixelarray((pic.Width) * (pic.Height))

        Dim i As Integer
        Dim X As Integer
        Dim Y As Integer

        i = 1
        For Y = 1 To pic.Height
            For X = 1 To pic.Width

                'UPGRADE_WARNING: Couldn't resolve default property of object X. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                'UPGRADE_ISSUE: PictureBox method Graphic.Point was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
                'UPGRADE_WARNING: Couldn't resolve default property of object graphics.gfindcolor. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                pixelarray(i) = graphics.gfindcolor(pic.GetPixel(X - 1, Y - 1)) And &HF

                i = i + 1
            Next
        Next


    End Sub
    'UPGRADE_NOTE: mapfile was upgraded to mapfile_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    Public Sub init()
        WorkDirectory = IO.Path.Combine(App.apppath, "work")
        GWorkDirectory.Text = WorkDirectory

        EP.Clear()
        EPinf.Clear()

        IO.Directory.CreateDirectory(WorkDirectory)

        Dim i As Short

        CGFileList.Items.Clear()
        For i = 0 To graphics.numitems - 1
            Dim lvi As ListViewItem = CGFileList.Items.Add(zerostr(i, 4))
            lvi.SubItems.Add(zerohex(i, 4))
            lvi.SubItems.Add(mapfile.gettype_Renamed(i))
            lvi.SubItems.Add(mapfile.getname(i))
            lvi.SubItems.Add("")
        Next
        makeimportlist()

    End Sub

    Public Sub makeimportlist()
        Dim filenum As Integer
        Dim i As Integer

        For i = 0 To graphics.numitems - 1
            'UPGRADE_WARNING: Couldn't resolve default property of object CGFileList.List(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            CGFileList.Items(i).SubItems(4).Text = ""
        Next

        Dim objFile As String

        For Each objFile In IO.Directory.GetFiles(WorkDirectory)
            filenum = Val(IO.Path.GetFileName(objFile))
            If filenum = 0 And Mid(IO.Path.GetFileName(objFile), 1, 1) <> "0" Then
                filenum = -1
            End If
            If filenum >= 0 And filenum <= graphics.numitems - 1 Then
                CGFileList.Items(filenum).SubItems(4).Text = IO.Path.GetFileName(objFile)
            End If
        Next objFile

    End Sub
    Private Sub Cancel_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub


    Private Sub ExportAll_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ExportAll.Click
        Dim i As Short

        EP.Clear()
        Directory.CreateDirectory(GWorkDirectory.Text)
        If Not TryRefresh() Then Exit Sub

        dlg_progresswindow.Text = "Exporting graphics..."
        dlg_progresswindow.Title.Text = "Exporting graphics..."
        dlg_progresswindow.Show()
        dlg_progresswindow.setcurrent((0))
        dlg_progresswindow.updateinterval = 8
        dlg_progresswindow.setmax(graphics.numitems - 1)
        For i = 0 To graphics.numitems - 1
            dlg_progresswindow.setcurrent((i))
            Try
                If mapfile.gettype_Renamed(i) = "IMG1" Then
                    App.graphics.kkspec_gexport_img1_type2(i, IO.Path.Combine(WorkDirectory, CStr((zerostr(i, 4) & " ") + mapfile.getname(i) + (".bmp"))), 0)
                Else
                    App.graphics.gexport_raw(i, IO.Path.Combine(WorkDirectory, CStr((zerostr(i, 4) & " ") + mapfile.getname(i) + (".dat"))))
                End If
            Catch e As PathTooLongException
                CGFileList.Items(i).Focused = True
                CGFileList.Items(i).Selected = True
                CGFileList.Items(i).EnsureVisible()
                MsgBox(My.Resources.ItsPathTooLongException, MsgBoxStyle.Exclamation)
                EP.SetError(GWorkDirectory, My.Resources.ShortenIt)
                EP.SetIconAlignment(GWorkDirectory, ErrorIconAlignment.MiddleLeft)
                Exit Sub
            End Try
        Next
        dlg_progresswindow.Hide()
        makeimportlist()
    End Sub

    Private Sub ExportSelected_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ExportSelected.Click
        Dim i As Short
        Dim count As Short

        EP.Clear()
        Directory.CreateDirectory(GWorkDirectory.Text)
        If Not TryRefresh() Then Exit Sub

        dlg_progresswindow.Text = "Exporting graphics..."
        dlg_progresswindow.Title.Text = "Exporting graphics..."
        dlg_progresswindow.setcurrent((0))
        dlg_progresswindow.updateinterval = 1
        count = CGFileList.CheckedIndices.Count
        If count = 0 Then
            Exit Sub
        End If
        dlg_progresswindow.setmax((count))
        dlg_progresswindow.Show()
        count = 0
        For i = 0 To graphics.numitems - 1
            If CGFileList.Items(i).Checked = True Then
                dlg_progresswindow.setcurrent((count))
                count = count + 1
                Try
                    If mapfile.gettype_Renamed(i) = "IMG1" Then
                        App.graphics.kkspec_gexport_img1_type2(i, IO.Path.Combine(WorkDirectory, CStr((zerostr(i, 4) & " ") + mapfile.getname(i) + (".bmp"))), False)
                    Else
                        App.graphics.gexport_raw(i, IO.Path.Combine(WorkDirectory, CStr((zerostr(i, 4) & " ") + mapfile.getname(i) + (".dat"))))
                    End If
                Catch e As PathTooLongException
                    CGFileList.Items(i).Focused = True
                    CGFileList.Items(i).Selected = True
                    CGFileList.Items(i).EnsureVisible()
                    MsgBox(My.Resources.ItsPathTooLongException, MsgBoxStyle.Exclamation)
                    EP.SetError(GWorkDirectory, My.Resources.ShortenIt)
                    EP.SetIconAlignment(GWorkDirectory, ErrorIconAlignment.MiddleLeft)
                    Exit Sub
                End Try
            End If
        Next
        dlg_progresswindow.Hide()
        makeimportlist()
    End Sub

    'Private Sub GWorkDirectory_Change()
    '    Dim lastchar As String
    '    lastchar = Right(GWorkDirectory.Text, 1)
    '    If lastchar = "\" Or lastchar = "/" Then
    '    WorkDirectory = GWorkDirectory.Text
    '    Else
    '        WorkDirectory = GWorkDirectory.Text + "\"
    '    End If
    'End Sub

    'UPGRADE_NOTE: graphics was upgraded to graphics_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    Private Sub ImportAll_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ImportAll.Click
        Dim fname As String

        Dim i As Short
        Dim newlen As Short
        Dim newstr As MemoryStream = Nothing
        Dim count As Short

        EP.Clear()
        If Not TryRefresh() Then Exit Sub

        dlg_progresswindow.Text = "Importing graphics..."
        dlg_progresswindow.Title.Text = "Importing graphics..."
        dlg_progresswindow.setcurrent((0))
        count = 0
        For i = 0 To graphics.numitems - 1
            'UPGRADE_WARNING: Couldn't resolve default property of object CGFileList.List(i, 4). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            If CGFileList.Items(i).SubItems(4).Text <> "" Then
                count = count + 1
            End If
        Next
        If count = 0 Then
            Exit Sub
        End If
        dlg_progresswindow.setmax((count))
        dlg_progresswindow.updateinterval = 1
        count = 0
        dlg_progresswindow.Show()
        For i = 0 To graphics.numitems - 1
            'UPGRADE_WARNING: Couldn't resolve default property of object CGFileList.List(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            'UPGRADE_WARNING: Couldn't resolve default property of object fname. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            fname = CGFileList.Items(i).SubItems(4).Text
            'UPGRADE_WARNING: Couldn't resolve default property of object fname. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            If fname <> "" Then
                dlg_progresswindow.setcurrent((count))
                count = count + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object fname. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                If VB.Right(fname, 4) = ".dat" Then
                    'UPGRADE_WARNING: Couldn't resolve default property of object fname. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object graphics.import_raw. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    newlen = graphics.import_raw(newstr, IO.Path.Combine(WorkDirectory, fname))
                    'UPGRADE_WARNING: Couldn't resolve default property of object graphics.setgitemdata. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    graphics.setgitemdata(i, newstr)
                    'UPGRADE_WARNING: Couldn't resolve default property of object graphics.setgitemsize. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    graphics.setgitemsize(i, newlen)
                Else
                    ' picture ?
                    If mapfile.gettype_Renamed(i) = "IMG1" Then
                        'load it
                        Graphic.Image = MyPicUt.LoadPicFromFile(IO.Path.Combine(WorkDirectory, fname))

                        graphics.kkspec_selPal4Item(i)

                        'sfile = App.apppath + "\temp.bmp"

                        generatepixelarray()
                        newstr = graphics.encode_img1(pixelarray, Graphic.Width - 4, Graphic.Height - 4)
                        graphics.setgitemdata(i, newstr)
                        graphics.setgitemsize(i, newstr.Length)
                        ReDim pixelarray(0)
                        'rebuild the bmp
                        graphics.buildbmp(i)
                    Else
                        ' not a dat file? raw it
                        newlen = graphics.import_raw(newstr, IO.Path.Combine(WorkDirectory, fname))
                        graphics.setgitemdata(i, newstr)
                        graphics.setgitemsize(i, newlen)
                    End If
                End If
            End If
        Next
        dlg_progresswindow.Hide()
    End Sub

    'UPGRADE_NOTE: graphics was upgraded to graphics_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    Private Sub ImportSelected_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ImportSelected.Click
        Dim fname As String

        Dim i As Short
        Dim newlen As Short
        Dim newstr As MemoryStream = Nothing
        Dim count As Short

        EP.Clear()
        If Not TryRefresh() Then Exit Sub

        dlg_progresswindow.Text = "Importing graphics..."
        dlg_progresswindow.Title.Text = "Importing graphics..."
        dlg_progresswindow.setcurrent((0))
        count = 0
        For i = 0 To graphics.numitems - 1
            'UPGRADE_WARNING: Couldn't resolve default property of object CGFileList.List(i, 4). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            If CGFileList.Items(i).SubItems(4).Text <> "" And CGFileList.Items(i).Checked = True Then
                count = count + 1
            End If
        Next
        If count = 0 Then
            Exit Sub
        End If
        dlg_progresswindow.setmax((count))
        dlg_progresswindow.updateinterval = 1
        count = 0
        dlg_progresswindow.Show()
        For i = 0 To graphics.numitems - 1
            'UPGRADE_WARNING: Couldn't resolve default property of object CGFileList.List(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            'UPGRADE_WARNING: Couldn't resolve default property of object fname. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            fname = CGFileList.Items(i).SubItems(4).Text
            'UPGRADE_WARNING: Couldn't resolve default property of object fname. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            If fname <> "" And CGFileList.Items(i).Checked = True Then
                dlg_progresswindow.setcurrent((count))
                count = count + 1
                'UPGRADE_WARNING: Couldn't resolve default property of object fname. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                If VB.Right(fname, 4) = ".dat" Then
                    'UPGRADE_WARNING: Couldn't resolve default property of object fname. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    'UPGRADE_WARNING: Couldn't resolve default property of object graphics.import_raw. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    newlen = graphics.import_raw(newstr, IO.Path.Combine(WorkDirectory, fname))
                    'UPGRADE_WARNING: Couldn't resolve default property of object graphics.setgitemdata. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    graphics.setgitemdata(i, newstr)
                    'UPGRADE_WARNING: Couldn't resolve default property of object graphics.setgitemsize. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    graphics.setgitemsize(i, newlen)
                Else
                    ' picture ?
                    If mapfile.gettype_Renamed(i) = "IMG1" Then
                        'load it
                        Graphic.Image = MyPicUt.LoadPicFromFile(IO.Path.Combine(WorkDirectory, fname))

                        graphics.kkspec_selPal4Item(i)

                        'sfile = App.apppath + "\temp.bmp"
                        generatepixelarray()
                        newstr = graphics.encode_img1(pixelarray, Graphic.Width - 4, Graphic.Height - 4)
                        graphics.setgitemdata(i, newstr)
                        graphics.setgitemsize(i, newstr.Length)
                        ReDim pixelarray(0)
                        'rebuild the bmp
                        graphics.buildbmp(i)
                    Else
                        ' not a dat file? raw it
                        newlen = graphics.import_raw(newstr, IO.Path.Combine(WorkDirectory, fname))
                        graphics.setgitemdata(i, newstr)
                        graphics.setgitemsize(i, newlen)
                    End If
                End If
            End If
        Next
        dlg_progresswindow.Hide()
    End Sub

    Private Sub Refresh_Renamed_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Refresh_Renamed.Click
        'Dim lastchar As String
        'lastchar = VB.Right(GWorkDirectory.Text, 1)
        'If lastchar = "\" Or lastchar = "/" Then
        '    'WorkDirectory = GWorkDirectory.Text
        'Else
        '    GWorkDirectory.Text = GWorkDirectory.Text & "\"
        'End If

        TryRefresh()
    End Sub

    Private Sub ImportManager_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If e.CloseReason = CloseReason.UserClosing Then
            e.Cancel = True
            Hide()
        End If
    End Sub

    Private Sub ButtonExplorer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExplorer.Click
        System.Diagnostics.Process.Start(GWorkDirectory.Text)
    End Sub

    Private Sub OpenHighlightedFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenHighlightedFileToolStripMenuItem.Click
        For Each lvi As ListViewItem In CGFileList.SelectedItems
            Try
                System.Diagnostics.Process.Start(IO.Path.Combine(GWorkDirectory.Text, lvi.SubItems(4).Text))
            Catch ex As Exception
                MsgBox("Sorry. failed to open that file!", MsgBoxStyle.Exclamation)
            End Try
            Exit For
        Next
    End Sub

    Private Sub ExplorerWithHighlightedFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExplorerWithHighlightedFileToolStripMenuItem.Click
        For Each lvi As ListViewItem In CGFileList.SelectedItems
            System.Diagnostics.Process.Start("explorer.exe", " /select,""" & IO.Path.Combine(GWorkDirectory.Text, lvi.SubItems(4).Text) & """")
            Exit For
        Next
    End Sub

    Private Sub GWorkDirectory_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GWorkDirectory.TextChanged
        EPinf.SetError(Refresh_Renamed, "Click [SetDir Refresh] to activate new work directory!")
    End Sub

    Private Function TryRefresh() As Boolean
        If IO.Directory.Exists(GWorkDirectory.Text) <> True Then
            MsgBox("Invalid Directory")
            TryRefresh = False
        Else
            WorkDirectory = GWorkDirectory.Text
            makeimportlist()

            TryRefresh = True
            EPinf.Clear()
        End If
    End Function
End Class