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

namespace kiosk {

    internal class kiosk {

        private static void Main(string[] args) {
            CommandLineOptions commandLineOptions = CommandLineOptions.CreateFromArguments(args);
            commandLineOptions.Validate();

            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 ("Installed" != printerOptionsValues._kioskOption) {
                    throw new Exception(commandLineOptions.printerName + " does not have kiosk option installed.");
                }

                if (commandLineOptions.emboss && "Installed" != printerOptionsValues._moduleEmbosser) {
                    throw new Exception(commandLineOptions.printerName + " does not have emboss module installed.");
                }


                if (commandLineOptions.hopperID == "r1" && "Installed" != printerOptionsValues._rearPickOption)
                    throw new Exception("printer" + commandLineOptions.printerName + " does not have a rear pick option installed.");

                printerJobID = Util.StartJob(
                    bidiSpl,
                    commandLineOptions.hopperID,
                    commandLineOptions.cardEjectSide,
                    commandLineOptions.outputHopperLocation);


                Util.PrintTextAndGraphics(bidiSpl, commandLineOptions.printerName, commandLineOptions.emboss);
                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();
            }
        }
    }
}
 