////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Datacard Corporation.  All Rights Reserved.
//
// XPS Driver SDK csharp print sample.
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using dxp01sdk;

namespace print {

    internal class print {

        private static string GetHopperStatus(
            BidiSplWrap bidiSpl,
            string hopperId)
        {
            string hopperStatusXml = bidiSpl.GetPrinterData(strings.HOPPER_STATUS);
            string hopperStatus = Util.ParseHopperStatusXML(hopperStatusXml, hopperId);
            return hopperStatus;
        }
        private static void Main(string[] args) {
            CommandLineOptions commandLineOptions = CommandLineOptions.CreateFromArguments(args);
            commandLineOptions.Validate();
            bool checkHopperStatus = commandLineOptions.hopperID.Length > 0 && 
                                     commandLineOptions.checkHopper;

            BidiSplWrap bidiSpl = null;
            int printerJobID = 0;

            try {
                bidiSpl = new BidiSplWrap();
                bidiSpl.BindDevice(commandLineOptions.printerName);

                string driverVersionXml = bidiSpl.GetPrinterData(strings.SDK_VERSION);
                Console.WriteLine(Environment.NewLine + "driver version: " + Util.ParseDriverVersionXML(driverVersionXml) + Environment.NewLine);

                string printerOptionsXML = bidiSpl.GetPrinterData(strings.PRINTER_OPTIONS2);
                PrinterOptionsValues printerOptionsValues = Util.ParsePrinterOptionsXML(printerOptionsXML);

                if ("Ready" != printerOptionsValues._printerStatus && "Busy" != printerOptionsValues._printerStatus) {
                    throw new Exception(commandLineOptions.printerName + " is not ready. status: " + printerOptionsValues._printerStatus);
                }

                if (("Installed" != printerOptionsValues._printHead) && ("Version1" != printerOptionsValues._connectionProtocol)) {
					throw new Exception(commandLineOptions.printerName +
									" does not have a print head installed.");

				}

                if (commandLineOptions.magstripe && !printerOptionsValues._optionMagstripe.Contains("ISO")) {
                    throw new Exception(commandLineOptions.printerName + " does not have an ISO magnetic stripe unit installed.");
                }


                if (commandLineOptions.jobCompletion ||
                    (commandLineOptions.hopperID.Length > 0) ||
                    (commandLineOptions.cardEjectSide.Length > 0))
                {
                    string hopperID = Util.GetInputHopper(printerOptionsValues._optionInputHopper, commandLineOptions.hopperID);
					
					if (checkHopperStatus) {
						string hopperStatus = GetHopperStatus(bidiSpl, hopperID);
						if (String.Compare(hopperStatus, "Empty", StringComparison.OrdinalIgnoreCase) == 0)
							throw new Exception("Hopper '" + hopperID + "' is empty.");

						Console.WriteLine("Status of hopper '" + hopperID + "': " + hopperStatus + ".");
					}
                    string cardEjectSide = "default";

                    printerJobID = Util.StartJob(
                        bidiSpl,
                        hopperID,
                        (commandLineOptions.cardEjectSide.Length > 0) ? commandLineOptions.cardEjectSide : cardEjectSide);
                }

                SamplePrintDocument printDocument = new SamplePrintDocument(commandLineOptions);
                printDocument.PrintController = new StandardPrintController();
                printDocument.BeginPrint += new PrintEventHandler(printDocument.OnBeginPrint);
                printDocument.QueryPageSettings += new QueryPageSettingsEventHandler(printDocument.OnQueryPageSettings);
                

                // Get paper size index
                PaperSize pkSize;
                int paperSizeIndexDTC = 0;
                int paperSizeIndexRetransfer = 1;
                for (int i = 0; i < printDocument.PrinterSettings.PaperSizes.Count; i++)
                {
                    pkSize = printDocument.PrinterSettings.PaperSizes[i];
                    
                    if (pkSize.PaperName.Equals("ISO ID-1 Retransfer"))
                        paperSizeIndexRetransfer = i;
                    else if (pkSize.PaperName.Equals("ISO ID-1 (85.60 x 53.98 mm)"))
                         paperSizeIndexDTC = i;
                         
                }

                // Set the printer resoultion and papaer size
                PrinterResolution pkResolution;
                for (int i = 0; i < printDocument.PrinterSettings.PrinterResolutions.Count; i++)
                {
                    pkResolution = printDocument.PrinterSettings.PrinterResolutions[i];
                    if (pkResolution.X == 600 && pkResolution.Y == 600)
                    {
                        if (printerOptionsValues._colorPrintResolution.Contains("600x600"))
                        {
                            // 600 DPI printer
                            pkResolution.X = 600;
                            pkResolution.Y = 600;
                            printDocument.DefaultPageSettings.PrinterResolution = pkResolution;
                            // Set the page size to"ISO ID-1 Retransfer"
                            printDocument.DefaultPageSettings.PaperSize = printDocument.PrinterSettings.PaperSizes[paperSizeIndexRetransfer];
                            commandLineOptions.scaleFactor = 2;
                            break;
                        }

                    }
                    else if (pkResolution.X == 300 && pkResolution.Y == 300)
                    {
                        if (printerOptionsValues._colorPrintResolution.Contains("300x300"))
                        {
                            // 300 DPI printer
                            printDocument.DefaultPageSettings.PrinterResolution = pkResolution;
                            // Set the page size to "ISO ID-1 (85.60 x 53.98 mm)"
                            printDocument.DefaultPageSettings.PaperSize = printDocument.PrinterSettings.PaperSizes[paperSizeIndexDTC];
                            break;
                        }
                    }
                }

                printDocument.PrintPage += new PrintPageEventHandler(printDocument.OnPrintPage);
                printDocument.Print();

                if (0 != printerJobID) {
                    // wait for the print spooling to finish and then issue an EndJob():
                    Util.WaitForWindowsJobID(bidiSpl, commandLineOptions.printerName);
                    bidiSpl.SetPrinterData(strings.ENDJOB);
                }

                if (commandLineOptions.jobCompletion) {
                    Util.PollForJobCompletion(bidiSpl, printerJobID);
                }
            }
            catch (BidiException e) {
                Console.WriteLine(e.Message);
                Util.CancelJob(bidiSpl, e.PrinterJobID, e.ErrorCode);
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
                if (0 != printerJobID) {
                    Util.CancelJob(bidiSpl, printerJobID, 0);
                }
            }
            finally {
                bidiSpl.UnbindDevice();
            }
        }
    }
}
 