angular.module('orderCloud')
	.config(checkoutPaymentConfig)
	.controller('CheckoutPaymentCtrl', CheckoutPaymentController)
    .factory('CheckoutPaymentService', CheckoutPaymentService)
;

function checkoutPaymentConfig($stateProvider) {
	$stateProvider
		.state('checkout.payment', {
			url: '/payment',
			templateUrl: 'checkout/payment/templates/checkout.payment.tpl.html',
			controller: 'CheckoutPaymentCtrl',
			controllerAs: 'checkoutPayment',
            resolve: {
                InitializeTaxes: function(TaxIntegration, CurrentOrder, OrderCloud, $rootScope){
                    return OrderCloud.LineItems.List(CurrentOrder.ID, null, null, 100)
                        .then(function(LineItemList){
                            return TaxIntegration.Get(CurrentOrder.BillingAddress, LineItemList)
                                .then(function(data){
                                    return OrderCloud.Orders.Patch(CurrentOrder.ID, {TaxCost: data.Data.TotalTax})
                                        .then(function(){
                                            $rootScope.$broadcast('OC:UpdateOrder', CurrentOrder.ID);
                                        });
                                });
                        });
				}
            }
		})
    ;
}

function CheckoutPaymentController(rebateCode) {
	var vm = this;

    vm.rebateCode = rebateCode;
}

function CheckoutPaymentService($q, OrderCloud) {
    var service = {
        PaymentsExceedTotal: _paymentsExceedTotal,
        RemoveAllPayments: _removeAllPayments
    };

    function _paymentsExceedTotal(payments, orderTotal) {
        var paymentTotal = 0;
        angular.forEach(payments.Items, function(payment) {
            paymentTotal += payment.Amount;
        });

        return paymentTotal.toFixed(2) > orderTotal.toFixed(2);
    }

    function _removeAllPayments(payments, order) {
        var deferred = $q.defer();

        var queue = [];
        angular.forEach(payments.Items, function(payment) {
            queue.push(OrderCloud.Payments.Delete(order.ID, payment.ID));
        });

        $q.all(queue).then(function() {
            deferred.resolve();
        });

        return deferred.promise;
    }

    return service;
}

@@ -443,17 +443,92 @@
 Payment'
-,
+%0A            //taxes not being returned from integration comment out for now
 %0A       
@@ -531,16 +531,19 @@
         
+ //
  resolve
@@ -553,24 +553,27 @@
 %0A           
+ //
      Initial
@@ -649,32 +649,35 @@
 pe)%7B%0A           
+ //
          return 
@@ -739,32 +739,35 @@
 100)%0A           
+ //
              .th
@@ -796,32 +796,35 @@
 st)%7B%0A           
+ //
                 
@@ -896,32 +896,35 @@
 ist)%0A           
+ //
                 
@@ -953,32 +953,35 @@
 ta)%7B%0A           
+ //
                 
@@ -1071,32 +1071,35 @@
 ax%7D)%0A           
+ //
                 
@@ -1132,32 +1132,35 @@
 n()%7B%0A           
+ //
                 
@@ -1250,43 +1250,10 @@
     
-                            %7D);%0A   
+//
     
@@ -1285,32 +1285,73 @@
 %7D);%0A            
+//                     %7D);%0A            //
             %7D);%0A
@@ -1350,15 +1350,19 @@
     
+ 
 %7D);%0A%09%09%09
+// 
 %09%7D%0A 
@@ -1371,16 +1371,19 @@
         
+ //
  %7D%0A%09%09%7D)%0A

remove tax integration for now, client is not sure if they want