﻿/**
 * Print handler class
 *
 * @author Mandarin Drummond
 */
RP.Printer = Class.create(); 
RP.Printer.setDeviceId = function(devId) {

    //document.printerDeviceId = devId;
    RP.Util.setGlobal('printerDeviceId', devId);
}
RP.Printer.getDeviceId = function() {
    //putting this in the document ns for now, want to clean this up for later..
    return RP.Util.getGlobal('printerDeviceId');
}
/**
 * Returns the printer instance, which is intended to be a singleton.
 *
 * @returns RP.Printer
 */
RP.Printer.getHandle = function() {

    //hardcoding the variable name for now...
    if (RP.Util.getGlobal('printHandler') == undefined) {

        RP.Util.setGlobal('printHandler', new RP.Printer());
        //document.ci_printer = new RP.Printer();       
    }
    return RP.Util.getGlobal('printHandler');
}

RP.Printer.prototype = {

  /**
  * Class constructor
  *
  * @param string printIFrame element to use to invoke the print URL (see this._print)
  * @return NULL
  */
  initialize: function () {

    /**
    * @todo set default failure/error callback and default
    * timeout callback.
    */

  },

  /**
  * Starts the printing process
  *
  * @return NULL
  */
  start: function (devId, zipCode, printSource, remoteUserId, cpnIds, callBack) {
    this._devId = devId;
    this._zipCode = zipCode;
    this._printSource = printSource;
    this._remoteUserId = remoteUserId;
    this._cpnIds = cpnIds;
    this._callBack = callBack;
    //this function triggers the sequence...
    this._setToken(RP.Printer.getHandle()._cpnIds);
  },
  /**
  * Set the print token
  *
  * @param array cpnIds array of coupons Ids
  * @return Boolean
  */
  _setToken: function (cpnIds) {
    var printstr = cpnIds.join(",");
    var prox = RP.Util.WebService.Factory("PrintService");
    //get print token
    prox.GetPrintToken(printstr, this._devId, this._zipCode, this._printSource, this._remoteUserId, RP.Printer.getHandle()._handleTokenResponse);
  },

  /**
  * Handles the token response...passes along to the print method
  *
  * @param TokenWithMsg resp
  * @return NULL
  */
  _handleTokenResponse: function (resp) {

    //alert(resp.PrintCartToken);
    var token = resp.PrintTokenResponse.PrintCartToken;

    /**
    * @todo check for resp.ErrorCoupons and
    * throw appropriate exception (or display message) if some
    * are found.
    */

    if (resp.Status == "Failed.") {
      var ex = new $RP$EX("Failed to print: " + resp.Message + ".");
      ex.alert();
      return;
    }

    var hndl = RP.Printer.getHandle();

    if (resp.PrintTokenResponse.ErrorCoupons &&
        resp.PrintTokenResponse.ErrorCoupons.size()) {
      hndl._errorCoupons = resp.PrintTokenResponse.ErrorCoupons;
      if (hndl._cpnIds.size() == resp.PrintTokenResponse.ErrorCoupons.size()) {
        hndl._token = token;
      }
    }
    hndl._token = token;
    hndl._printUrl = resp.PrintUrl;
    hndl._callBack();

  },

  /**
  * Return any errors available after calling start();
  *
  */
  getErrors: function () {
    return RP.Printer.getHandle()._errorCoupons;
  },

  /**
  * Checks to see if any valid coupons are available for printing
  *
  */
  hasValidCoupons: function () {

    if (!this._token) {
      return false;
    }
    if (!this._errorCoupons || (this._errorCoupons && (this._cpnIds.size() > this._errorCoupons.size()))) {
      return true;
    }
    return false;
  },

  /**
  * print the stuff.. call this after start(); and only if hasValidCoupons returns true
  */
  print: function () {
    if (!this._token) {
      throw new $RP$EX('No print token available.');
    }

    //var prox = RP.Util.WebService.Factory("PrintService");
    //prox.set_defaultSucceededCallback(RP.Printer.getHandle()._redirect);
    //prox.GetPrintUrl(this._token);
    RP.Printer.getHandle()._redirect(this._printUrl);
  },

  /**
  * Sends the request to the printer
  *
  * @param string url URL of the printmanager @coupons inc.
  * @return null
  */
  _redirect: function (url) {
    //if no errors, send along to PrintManager.aspx @ coupons.com
    $('printWindow').src = url;
  }

};
