////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Datacard Corporation.  All Rights Reserved.
//
// populate a CommandLineOptions data structure from parsed command line args.
////////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using dxp01sdk;

namespace kiosk {

    internal class CommandLineOptions {
        public bool jobCompletion;
        public bool emboss;
        public string printerName;
        public string hopperID = "1";
        public string cardEjectSide = "Default";
        public string outputHopperLocation = "";
        private static void Usage()
        {
            string thisExeName = Path.GetFileName(Environment.GetCommandLineArgs()[0]);
            Console.WriteLine(thisExeName + " demonstrates print functionality of the printer and driver.");
            Console.WriteLine();
            Console.WriteLine(thisExeName + " -n <printername> [-i <input hopper>] [-f <side>]");
            Console.WriteLine();
            Console.WriteLine("options:");
            Console.WriteLine("  -n <printername>. Required. Try -n \"XPS Card Printer\".");
            Console.WriteLine("  -i <input hopper> Defaults to input hopper #1.");
            Console.WriteLine("  -c Poll for job completion.");
            Console.WriteLine("  -e Emboss and Indent.");
            Console.WriteLine("  -f <Front | Back>. Specify the front or back side the card will eject from.");
            Console.WriteLine("  -k Card will be ejected to the front output tray.");
            Console.WriteLine("         If - k is not specified, the card will be dispensed to the rear of the printer");
            Console.WriteLine();
            Console.WriteLine(thisExeName + " -n \"XPS Card Printer\"");
            Console.WriteLine("  Pick card from input hopper 1, print a one-sided landscape card and eject card to rear of the printer");
            Console.WriteLine();
            Console.WriteLine(thisExeName + " -n \"XPS Card Printer\" -i r1 -c");
            Console.WriteLine("  Pick card from rear input, print a one-sided landscape card, eject card to rear of the printer and poll for job status.");
            Console.WriteLine();
            Console.WriteLine(thisExeName + " -n \"XPS Card Printer\" -i 2 -k -e");
            Console.WriteLine("  Pick card from from input hopper 2, print and emboss a one-sided landscape card , eject to the front output tray.");

            Environment.Exit(-1);
        }

        public void Validate() {
            if (string.IsNullOrEmpty(printerName)) {
                Console.WriteLine("printer name is required");
                Environment.Exit(-1);
            }

            // if hopperID is an empty string, that is OK
            if (hopperID != "") {
                if (
                    hopperID != "1" &&
                    hopperID != "2" &&
                    hopperID != "3" &&
                    hopperID != "4" &&
                    hopperID != "5" &&
                    hopperID != "6" &&
                    hopperID != "r1" &&
                    hopperID != "exception")
                {
                    Console.WriteLine("invalid hopperID: {0}", hopperID);
                    Environment.Exit(-1);
                }
            }

            // if cardEjectSide is an empty string, that is OK
            if (cardEjectSide != "") {
                if ( cardEjectSide != "front" && cardEjectSide != "back" && cardEjectSide != "Default")
                {
                    Console.WriteLine("invalid cardEjectSide: {0}", cardEjectSide);
                    Environment.Exit(-1);
                }
            }
        }

        static public CommandLineOptions CreateFromArguments(string[] args) {
            if (args.Length == 0) Usage();

            CommandLineOptions commandLineOptions = new CommandLineOptions();
            CommandLine.Utility.Arguments arguments = new CommandLine.Utility.Arguments(args);

            if (!string.IsNullOrEmpty(arguments["h"])) Usage();

            commandLineOptions.printerName = arguments["n"];
            commandLineOptions.jobCompletion = !string.IsNullOrEmpty(arguments["c"]);
            commandLineOptions.emboss = !string.IsNullOrEmpty(arguments["e"]);
            commandLineOptions.hopperID =
                string.IsNullOrEmpty(arguments["i"]) ? string.Empty : arguments["i"].ToLower();
            commandLineOptions.cardEjectSide =
                string.IsNullOrEmpty(arguments["f"]) ? string.Empty : arguments["f"].ToLower();
            commandLineOptions.outputHopperLocation =
                string.IsNullOrEmpty(arguments["k"]) ? string.Empty : "FrontOutputTray";

            return commandLineOptions;
        }
    }
}