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

import common_java.PrinterOptions;
import common_java.FirmwareVersion;
import common_java.XPS_Java_SDK;
import common_java.XPS_Java_SDK.XpsDriverInteropLib;

public class ColorMode
{
   private String mPrinterName;
   private String[] SigmaColormodeValues = {
           "TrueColor (ICC)",
           "Vivid",
           "SD/CD",
           "Custom"
       };

   // CR805 supported colorModes
   private String[] CR805ColormodeValues = {
       "TrueColor (ICC)",
       "Vivid",
       "Legacy SR/CR",
       "Legacy SR wLUT"
   };


   public ColorMode(String aPrinterName)
   {
      mPrinterName = aPrinterName;
   }
   
   public void SetColorMode(String colorMode) throws Exception
   {
      int S_OK = 0;
      long hResult = -1; // not S_OK

      // FYI - IsColorModeCapable() throws exceptions if something is not correct.
      //     - do not need to check return code of IsColorModeCapable()
      IsColorModeCapable(colorMode);
      
      // call driver to set color adjust values for red color channel
      hResult = XpsDriverInteropLib.INSTANCE.SetColorMode(mPrinterName, colorMode);

      if (S_OK == hResult)
      {
         System.out.format("'%s' SetColorMode: success", mPrinterName);
      }
      else
      {
         System.out.format("'%s' SetColorMode: failed, hResult = %d", mPrinterName, hResult);
      }
   }
   
   private Boolean IsColorModeCapable(String colorMode) throws Exception {
       // 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(mPrinterName);
       if (S_OK == XpsDriverInteropLib.INSTANCE.GetPrinterOptions2(mPrinterName, printerOptionsReturnXML, printerOptionsSizeOfReturnXML)) {
           printerOptions.ParsePrinterOptions(printerOptionsReturnXML, printerOptionsSizeOfReturnXML);
       } else {
           System.out.println("GetPrinterOptions2() call failed."); 
           System.exit(0);
       }
           
       if ( !printerOptions._printerStatus.equalsIgnoreCase("Ready")) {
           System.out.println("Printer is not in READY state"); 
           System.exit(0);
       }


       // verify that the user has passed in valid colormode value
       FirmwareVersion fwRev = new FirmwareVersion();
       fwRev.ParseFirmwareRev(printerOptions._printerVersion);


       boolean bFound = false;
       String exceptionText = "";
       if (fwRev._printerBase.equals("R4")) {  // CR805
           for (int i = 0; i < CR805ColormodeValues.length; i++) {
               if (colorMode.equals(CR805ColormodeValues[i])) {
                   bFound = true;
                   break;
               }
           }
           if (!bFound) {
               exceptionText = "ERROR: '" + colorMode + "' is not a valid CR805 colormode.";
               throw new Exception(exceptionText);
           }
       }
       else if (fwRev._printerBase.contentEquals("D4")) {     // Sigma
           for (int i = 0; i < SigmaColormodeValues.length; i++) {
               if (colorMode.equals(SigmaColormodeValues[i])) {
                   bFound = true;
                   break;
               }
           }
           if (!bFound) {
               exceptionText = "ERROR: '" + colorMode + "' is not a valid Sigma colormode.";
               throw new Exception(exceptionText);
           }
       }
       else {
           exceptionText = "ERROR: invalid printer type.";
           throw new Exception(exceptionText);
       }

       return true;
   }
}
