/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
/*browser:true*/
/*global define*/
define([
    'jquery',
    'Magento_Checkout/js/view/payment/default',
    'Magento_Paypal/js/action/set-payment-method',
    'Magento_Checkout/js/action/place-order',
    'Magento_Checkout/js/model/quote',
    'Magento_Customer/js/customer-data'
], function ($, Component, setPaymentMethodAction, placeOrderAction, quote, customerData) {
        'use strict';

        return Component.extend({           
            defaults: {
                template: 'Sts_SmartRoute/payment/form' 
            },
            getCode: function() {
                return 'smartroute';
            },           
            /** Redirect to smartroute */
            continueToSmartRoute: function () {         
                        
            
                //update payment method information if additional data was changed
                this.selectPaymentMethod();
                var mydata = this.getData();
                setPaymentMethodAction(this.messageContainer).done(
                    function () {
                        customerData.invalidate(['cart']);
                        placeOrderAction(mydata, this.messageContainer).done(
                                function (){
                        var timeStampInMs = window.performance && window.performance.now && window.performance.timing && window.performance.timing.navigationStart ? window.performance.now() + window.performance.timing.navigationStart : Date.now();
                        console.log(window.checkoutConfig.payment.smartroute);                        
                        console.log(window.checkoutConfig.payment.smartroute.redirectUrl);                        
                        $.mage.redirect(window.checkoutConfig.payment.smartroute.redirectUrl+"?t="+timeStampInMs);                                    
                                });

                    }
                );

                return false;            
        
            }            
        });
    }
);
