Imports System.Management
Imports System.Drawing.Printing

Module BrotherPrint

    Public Enum PrintList
        BoxCode
        Warranty
        ProdLabel
        maxPrint
    End Enum

    Public PrintActive(PrintList.maxPrint - 1) As Boolean
    Public PrintName(PrintList.maxPrint - 1) As String
    Public PrintAddress(PrintList.maxPrint - 1) As String
    Public PrintUseCode(PrintList.maxPrint - 1) As String
    Public PrintUseText(PrintList.maxPrint - 1) As String
    Public PrintUseDate As String
    Public PrintUseCkb(PrintList.maxPrint - 1) As CheckBox
    Public PrintUseTxb(PrintList.maxPrint - 1) As TextBox
    Public PrintUseZip(PrintList.maxPrint - 1) As TextBox


    Private Function PrintState_Check(printer As String) As Boolean
        Dim scope As ManagementScope = New ManagementScope("\root\cimv2")
        scope.Connect()
        Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_Printer")

        Dim CheckName As String = String.Empty

        For Each printobj As ManagementObject In searcher.Get
            CheckName = printobj("Name").ToString
            If CheckName.Equals(printer) Then
                If printobj("WorkOffline").ToString.ToLower.Equals("true") Then
                    Return False
                Else
                    Return True
                End If
            End If
        Next

        Return False
    End Function

    Public Sub PrintRun(PrintText As String)
        Try
            Dim doc As bpac.Document = New bpac.Document

            For i = 0 To PrintList.maxPrint - 1
                If PrintActive(i) = True Then
                    If PrintState_Check(PrintName(i)) Then
                        doc.Open(PrintAddress(i))
                        Dim PrintCheck As Boolean = doc.SetPrinter(PrintName(i), True)

                        If PrintCheck Then

                            Select Case i
                                Case PrintList.BoxCode
                                    doc.GetObject(PrintUseCode(i)).Text = PrintText
                                    doc.GetObject(PrintUseText(i)).Text = PrintText
                                Case PrintList.Warranty
                                    doc.GetObject(PrintUseText(i)).Text = PrintText
                                Case PrintList.ProdLabel
                                    doc.GetObject(PrintUseCode(i)).Text = PrintText
                                    doc.GetObject(PrintUseText(i)).Text = PrintText
                                    doc.GetObject(PrintUseDate).Text = Format(Now, "yyyy") & "." & Format(Now, "MM")
                                Case Else
                                    MsgBox("잘못된 경로입니다. (라벨 인쇄)", vbCritical)
                                    Exit Sub
                            End Select

                            doc.StartPrint("", bpac.PrintOptionConstants.bpoDefault)
                            doc.PrintOut(1, bpac.PrintOptionConstants.bpoDefault)
                            doc.EndPrint()
                            doc.Close()

                        Else
                            MsgBox("인쇄 중 경로 이탈 오류 발생", vbCritical)
                            Exit Sub
                        End If
                    Else
                        MsgBox(PrintName(i) & "의 연결 문제 발생" & vbCrLf & "연결을 확인하여 주십시오.", vbCritical)
                        Exit Sub
                    End If
                End If
            Next

        Catch ex As Exception
            MsgBox("인쇄 중 오류 발생 !" & vbCrLf & ex.Message, vbCritical)
        End Try
    End Sub

    Public Sub List_Refreash(ApplyDgv As DataGridView)
        Dim PrintName As String
        Dim PrintList As System.Collections.ArrayList = New System.Collections.ArrayList(PrinterSettings.InstalledPrinters)
        PrintList.Sort()

        ApplyDgv.Rows.Clear()

        For i = 0 To PrintList.Count - 1
            PrintName = PrintList(i).ToString
            ApplyDgv.Rows.Add(PrintName)
        Next
    End Sub
End Module
