''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright (c) Datacard Corporation.  All Rights Reserved.
'
' XPS Driver SDK vb.net print sample.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports dxp01sdk

Namespace print

    Class print
        Private Shared Function GetHopperStatus(ByVal bidiSpl As BidiSplWrap, ByVal hopperId As String) As String
            Dim hopperStatusXml As String = bidiSpl.GetPrinterData(strings.HOPPER_STATUS)
            Dim hopperStatus As String = Util.ParseHopperStatusXML(hopperStatusXml, hopperId)
            Return hopperStatus
        End Function

        Public Shared Sub Main(args As String())

            Dim commandLineOptions As CommandLineOptions = CommandLineOptions.CreateFromArguments(args)
            commandLineOptions.Validate()
            Dim checkHopperStatus As Boolean = commandLineOptions.hopperID.Length > 0 AndAlso commandLineOptions.checkHopper

            Dim bidiSpl As BidiSplWrap = Nothing
            Dim printerJobID As Integer = 0

            Try
                bidiSpl = New BidiSplWrap()
                bidiSpl.BindDevice(commandLineOptions.printerName)

                Dim driverVersionXml As String = bidiSpl.GetPrinterData(strings.SDK_VERSION)
                Console.WriteLine(Environment.NewLine & "driver version: " & Util.ParseDriverVersionXML(driverVersionXml) & Environment.NewLine)

                Dim printerOptionsXML As String = bidiSpl.GetPrinterData(strings.PRINTER_OPTIONS2)
                Dim printerOptionsValues As PrinterOptionsValues = Util.ParsePrinterOptionsXML(printerOptionsXML)

                If "Ready" <> printerOptionsValues._printerStatus AndAlso "Busy" <> printerOptionsValues._printerStatus Then
                    Throw New Exception(commandLineOptions.printerName + " is not ready. status: " + printerOptionsValues._printerStatus)
                End If

                If "Installed" <> printerOptionsValues._printHead AndAlso "Version1" <> printerOptionsValues._connectionProtocol Then
					Throw New Exception(commandLineOptions.printerName & " does not have a print head installed.")
                End If

                If commandLineOptions.magstripe AndAlso Not printerOptionsValues._optionMagstripe.Contains("ISO") Then
                    Throw New Exception(commandLineOptions.printerName + " does not have an ISO magnetic stripe unit installed.")
                End If

                If commandLineOptions.jobCompletion OrElse (commandLineOptions.hopperID.Length > 0) OrElse (commandLineOptions.cardEjectSide.Length > 0) Then
                    Dim hopperID As String = Util.GetInputHopper(printerOptionsValues._optionInputHopper, commandLineOptions.hopperID)
					If checkHopperStatus Then
						Dim hopperStatus As String = GetHopperStatus(bidiSpl, hopperID)
						If [String].Compare(hopperStatus, "Empty", StringComparison.OrdinalIgnoreCase) = 0 Then
							Throw New Exception("Hopper '" + hopperID + "' is empty.")
						End If
						Console.WriteLine((Convert.ToString("Status of hopper '" + hopperID + "': ") & hopperStatus) + ".")
					End If
					
                    Dim cardEjectSide As String = "default"

                    printerJobID = Util.StartJob(bidiSpl,
                        hopperID,
                        If((commandLineOptions.cardEjectSide.Length > 0), commandLineOptions.cardEjectSide, cardEjectSide))
                End If

                Dim printDocument As New SamplePrintDocument(commandLineOptions)
                printDocument.PrintController = New StandardPrintController()
                AddHandler printDocument.BeginPrint, New PrintEventHandler(AddressOf printDocument.OnBeginPrint)
                AddHandler printDocument.QueryPageSettings, New QueryPageSettingsEventHandler(AddressOf printDocument.OnQueryPageSettings)

                ' Get paper size index
                Dim pkSize As PaperSize
                Dim paperSizeIndexDTC As Integer = 0
                Dim paperSizeIndexRetransfer As Integer = 1

                For i As Integer = 0 To printDocument.PrinterSettings.PaperSizes.Count - 1
                    pkSize = printDocument.PrinterSettings.PaperSizes.Item(i)

                    If (pkSize.PaperName.Equals("ISO ID-1 Retransfer")) Then
                        paperSizeIndexRetransfer = i
                    ElseIf (pkSize.PaperName.Equals("ISO ID-1 (85.60 x 53.98 mm)")) Then
                        paperSizeIndexDTC = i
                    End If
                Next

                Dim pkResolution As PrinterResolution

                For i As Integer = 0 To printDocument.PrinterSettings.PrinterResolutions.Count - 1
                    pkResolution = printDocument.PrinterSettings.PrinterResolutions.Item(i)
                    If (pkResolution.X = 600 And pkResolution.Y = 600) Then
                        If printerOptionsValues._colorPrintResolution.Contains("600x600") Then
                            pkResolution.X = 600
                            pkResolution.Y = 600
                            printDocument.DefaultPageSettings.PrinterResolution = pkResolution
                            printDocument.DefaultPageSettings.PaperSize = printDocument.PrinterSettings.PaperSizes.Item(paperSizeIndexRetransfer)
                            commandLineOptions.scaleFactor = 2
                        End If
                    ElseIf (pkResolution.X = 300 And pkResolution.Y = 300) Then
                        If printerOptionsValues._colorPrintResolution.Contains("300x300") Then
                            pkResolution.X = 300
                            pkResolution.Y = 300
                            printDocument.DefaultPageSettings.PrinterResolution = pkResolution
                            printDocument.DefaultPageSettings.PaperSize = printDocument.PrinterSettings.PaperSizes.Item(paperSizeIndexDTC)
                        End If
                    End If
                Next

                AddHandler printDocument.PrintPage, New PrintPageEventHandler(AddressOf printDocument.OnPrintPage)
                printDocument.Print()

                If 0 <> printerJobID Then
                    ' wait for the print spooling to finish and then issue an EndJob():
                    Util.WaitForWindowsJobID(bidiSpl, commandLineOptions.printerName)
                    bidiSpl.SetPrinterData(strings.ENDJOB)
                End If

                If commandLineOptions.jobCompletion Then
                    Util.PollForJobCompletion(bidiSpl, printerJobID)
                End If
            Catch e As BidiException
                Console.WriteLine(e.Message)
                Util.CancelJob(bidiSpl, e.PrinterJobID, e.ErrorCode)
            Catch e As Exception
                Console.WriteLine(e.Message)
                If 0 <> printerJobID Then
                    Util.CancelJob(bidiSpl, printerJobID, 0)
                End If
            Finally
                bidiSpl.UnbindDevice()
            End Try
        End Sub
    End Class

End Namespace
