/* globals Windows: true */

/*
    Copyright 2013-2016 appPlant GmbH

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
*/

var PrintManager = Windows.Graphics.Printing.PrintManager;

/**
 * Verifies if printing is supported on the device.
 *
 * @param {Function} success
 *      Success callback function
 * @param {Function} error
 *      Error callback function
 * @param {Array} args
 *      Interface arguments
 */
exports.isAvailable = function (success, fail, args) {
    success(MSApp.hasOwnProperty('getHtmlPrintDocumentSourceAsync'), 0);
};

/**
 * Displays system interface for selecting a printer.
 *
 * @param {Function} success
 *      Success callback function
 * @param {Function} error
 *      Error callback function
 * @param {Array} args
 *      Interface arguments
 */
exports.pick = function (success, fail, args) {
    success(); // Not supported :(
};

/**
 * Sends the content to the Printing Framework.
 *
 * @param {Function} success
 *      Success callback function
 * @param {Function} error
 *      Error callback function
 * @param {Array} args
 *      Interface arguments
 */
exports.print = function (success, fail, args) {
    var page    = document.createDocumentFragment(),
        content = document.createElement('html');

    content.innerHTML = args[0];
    page.appendChild(content);

    exports._func = success;
    exports._args = args[1];

    MSApp.getHtmlPrintDocumentSourceAsync(page).then(function (source) {
        exports._page = source;
        PrintManager.showPrintUIAsync();
    });
};

/**
 * Raised when a request to print has occurred.
 * Create, configure and schedule the print task.
 *
 * @param {PrintTaskRequestedEventArgs} event
 *      Event arguments associated with the request.
 */
exports.onPrintTaskRequested = function (event) {
    var config = exports._args,
        task;

    task = event.request.createPrintTask(config.name, function (args) {
        args.setSource(exports._page);
    });

    task.oncompleted = function (e) {
        exports._func(e.detail[0].completion == 3);
    };
};

PrintManager.getForCurrentView()
    .onprinttaskrequested = exports.onPrintTaskRequested;

require('cordova/exec/proxy').add('Printer', exports);

@@ -882,16 +882,62 @@
 */%0A%0Avar 
+Printing     = Windows.Graphics.Printing,%0A    
 PrintMan
@@ -2765,24 +2765,752 @@
 );%0A    %7D);%0A%0A
+    if (config.graystyle) %7B%0A        task.options.colorMode = Printing.PrintColorMode.grayscale;%0A    %7D else %7B%0A        task.options.colorMode = Printing.PrintColorMode.color;%0A    %7D%0A%0A    if (config.landscape) %7B%0A        task.options.orientation = Printing.PrintOrientation.landscape;%0A    %7D else %7B%0A        task.options.orientation = Printing.PrintOrientation.portrait;%0A    %7D%0A%0A    if (config.duplex == 'long') %7B%0A        task.options.duplex = Printing.PrintDuplex.twoSidedLongEdge;%0A    %7D else%0A    if (config.duplex == 'short') %7B%0A        task.options.duplex = Printing.PrintDuplex.twoSidedShortEdge;%0A    %7D else %7B%0A        task.options.duplex = Printing.PrintDuplex.oneSided;%0A    %7D%0A%0A    task.options.numberOfCopies = config.copies %7C%7C 1;%0A%0A
     task.onc

Support print options on windows