////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Datacard Corporation.  All Rights Reserved.
////////////////////////////////////////////////////////////////////////////////
package com.datacard.xpsprinter.sdk.examples;

import common_java.XPS_Java_SDK;
import common_java.XPS_Java_SDK.XpsDriverInteropLib;
import common_java.PrinterStatusXML;
import common_java.JavaPrint;
import common_java.PrinterOptions;
import common_java.Util;

public class Kiosk_Main {
    
    public static void main(String[] args) {
        int i = 0;
        String arg;
        
    	String  printerName     = "";
        boolean nameGiven       = false;
        boolean duplex          = false;	// needed by JavaPrint
        boolean portrait        = false;	// needed by JavaPrint
        boolean doMagstripe     = false;	// needed by JavaPrint
        String  blockingFront   = null;		// needed by JavaPrint
        String  blockingBack    = null;		// needed by JavaPrint
        String  inputHopper     = "1";
        boolean pollCompletion  = false;
        String  cardEjectSide   = "Default";        
        boolean doTactileImpressFront = false;	// needed by JavaPrint
        boolean doTactileImpressBack  = false;	// needed by JavaPrint
        int     tactileImpressLocation = 2;		// needed by JavaPrint
        String  outputHopperLocation   = "";
        boolean emboss          = false;
        boolean doSampleText    = true;

        new XPS_Java_SDK(); // construct and extract interop dll

        // parse the command line arguments
        while (i < args.length) {
            arg = args[i++];

            if (arg.equalsIgnoreCase("-n")) {
                if (i < args.length) {
                    printerName = args[i++];
                    nameGiven = true;
                } else {
                    System.err.println("-n requires a printername");
                    break;
                }
            } else if (arg.equals("-i") && nameGiven) {           	
                if (i < args.length) {
                	inputHopper = args[i++];
                    ValidateHopperID(inputHopper);
                } else {
                    System.err.println("-i requires a 'hopper'.");
                    System.exit(-1);
                    break;
                }
            } else if (arg.equals("-c") && nameGiven) {
                pollCompletion = true;
            } else if (arg.equals("-e") && nameGiven) {
                emboss = true;
            } else if (arg.equals("-f") && nameGiven) {
                if (i < args.length) {
                    cardEjectSide = args[i++].toLowerCase();
                    ValidateCardEjectSide(cardEjectSide);
                } else {
                    System.err.println("-f requires a 'side'.");
                    System.exit(-1);
                    break;
                }
            } else if (arg.equals("-k") && nameGiven) {
                outputHopperLocation = "FrontOutputTray";
            } else {
                usage();
                System.exit(0);
            }
        }

        if (i < 2) {
            usage();
            System.exit(0);
        }
        
        // get printer options        
        byte printerOptionsReturnXML[] = new byte[XPS_Java_SDK.BUFFSIZE];
        int  printerOptionsSizeOfReturnXML[] = new int[1];
        printerOptionsSizeOfReturnXML[0] = XPS_Java_SDK.BUFFSIZE;
        final int S_OK = 0;

    	PrinterOptions printerOptions = new PrinterOptions(printerName);
	    if (S_OK == XpsDriverInteropLib.INSTANCE.GetPrinterOptions2(printerName, printerOptionsReturnXML, printerOptionsSizeOfReturnXML)) {
	      	printerOptions.ParsePrinterOptions(printerOptionsReturnXML, printerOptionsSizeOfReturnXML);
	  	} else {
	  		System.out.println("GetPrinterOptions2() call failed."); 
	        System.exit(0);
	  	}
		
		if (!printerOptions._printHead.equalsIgnoreCase("Installed") && !printerOptions._connectionProtocol.equalsIgnoreCase("Version1")){
			System.out.println("print head not installed"); 
	        System.exit(0);
		}	
	      	
	    if (emboss && !printerOptions._moduleEmbosser.equalsIgnoreCase("Installed")) {
	  		System.out.println("Emboss module not installed"); 
	        System.exit(0);
	    }	
	      
	    if ( !printerOptions._kioskOption.equalsIgnoreCase("Installed")) {
	  		System.out.println("kiosk option not installed"); 
	        System.exit(0);
	    }

        // get printer status
        byte returnXML[] = new byte[XPS_Java_SDK.BUFFSIZE];
        int sizeOfReturnXML[] = new int[1];
        sizeOfReturnXML[0] = XPS_Java_SDK.BUFFSIZE;
        PrinterStatusXML printerStatusXml = new PrinterStatusXML();
        boolean bJobStarted = false;
            	            
        if (S_OK == XpsDriverInteropLib.INSTANCE.StartJob2(printerName, 
            inputHopper, 
    	    cardEjectSide,
    	    outputHopperLocation,
    	    returnXML, sizeOfReturnXML)) 
        {
	          bJobStarted = true;
	          // get PrintJobID
	          printerStatusXml.Parse(returnXML, sizeOfReturnXML, false);
	    }      	             	
        
       JavaPrint javaPrint = new JavaPrint(
      		printerName,
      		duplex,
      		portrait,
      		doMagstripe,
      		blockingFront,
      		blockingBack,
            doSampleText,

      		doTactileImpressFront,
      		doTactileImpressBack,
      		tactileImpressLocation,
      		emboss);

       javaPrint.PrintDemo();
      
       // ensure windows print job is completely spooled
       Util.WaitUntilJobSpooled(printerName);
       XpsDriverInteropLib.INSTANCE.EndJob(printerName);
      
       if (pollCompletion && bJobStarted) {
           Util.PollForJobCompletion(printerName, printerStatusXml.GetPrintJobID());
       }

   }


    private static void ValidateHopperID(String hopperID)
    {
        if (!hopperID.equals("")) {
            if (
                !hopperID.equals("1") &&
                !hopperID.equals("2") &&
                !hopperID.equals("3") &&
                !hopperID.equals("4") &&
                !hopperID.equals("5") &&
                !hopperID.equals("6") &&
                !hopperID.equals("r1") &&
                !hopperID.equals("exception") ) {
                System.out.println( "invalid hopperID=" + hopperID + ".");
                System.exit(-1);
            }
        }
    }

    private static void ValidateCardEjectSide(String cardEjectSide)
    {
        if (!cardEjectSide.equals(""))  {
            if ( !cardEjectSide.equals("front") && !cardEjectSide.equals("back") ) {
                System.out.println("invalid cardEjectSide=" + cardEjectSide);
                System.exit(-1);
            }
        }
    }    

    public static void usage() {
        System.out.println();
        System.out.println("kiosk.jar demonstrates print functionality of the printer and driver.");
        System.out.println();
        System.out.println("Uses hardcoded data for printing");
        System.out.println();
        System.out.println("Overrides the driver printing preference settings.");
        System.out.println();
        System.out.println("java -jar kiosk.jar -n <printername> [-i <input hopper>] [-c] [-e] [-f <Front | Back>] [-k <FrontOutputTray>]");
        System.out.println();
        System.out.println("options: ");
        System.out.println("  -n <printername>. Required. Try -n \"XPS Card Printer\".");
        System.out.println("  -i <input hopper>. Defaults to input hopper #1");
        System.out.println("  -c Poll for job completion.");
        System.out.println("  -e Emboss and Indent.");
        System.out.println("  -f <Front | Back>. Specify the front or back side the card will eject from.");
        System.out.println("  -k Card will be ejected to the front output tray. ");
        System.out.println("      If -k is not specified, the card will be dispensed to the rear of the printer");
        System.out.println();
        System.out.println("  java -jar kiosk.jar -n \"XPS Card Printer\"");
        System.out.println("  Pick card from input hopper 1, print a one-sided landscape card and eject card to rear of the printer");
        System.out.println();
        System.out.println("  java -jar kiosk.jar -n \"XPS Card Printer\" -i r1 -c");
        System.out.println("  Pick card from rear input, print a one-sided landscape card, eject card to rear of the printer and poll for job status.");
        System.out.println();
        System.out.println("  java -jar kiosk.jar -n \"XPS Card Printer\" -i 2 -k -e");
        System.out.println("  Pick card from from input hopper 2, print and emboss a one-sided landscape card , eject to the front output tray.");
        System.out.println();
    }
}
