package beer.brew.vendingmachine.ui.beer;

import com.alipay.sdk.app.PayTask;
import java.util.Map;

import javax.inject.Inject;

import beer.brew.vendingmachine.data.model.Beer;
import beer.brew.vendingmachine.data.remote.PaymentService;
import beer.brew.vendingmachine.payment.OrderManager;
import beer.brew.vendingmachine.payment.data.AlipayStatus;
import beer.brew.vendingmachine.payment.data.Order;
import beer.brew.vendingmachine.payment.data.PayResult;
import beer.brew.vendingmachine.payment.data.PayStatus;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

import static rx.android.schedulers.AndroidSchedulers.mainThread;
import static rx.schedulers.Schedulers.io;

public class BeerInteractor {

    private PaymentService paymentService;
    private OrderManager orderManager;

    @Inject
    public BeerInteractor(PaymentService paymentService, OrderManager orderManager) {
        this.paymentService = paymentService;
        this.orderManager = orderManager;
    }

    public Observable<PayStatus> pay(Beer beer, Order.PayType payType) throws Exception {
        Order order = orderManager.generateOrder(beer, payType);
        return paymentService.getOrderInfo(order)
                .flatMap(new Func1<String, Observable<PayStatus>>() {
                    @Override
                    public Observable<PayStatus> call(String orderInfo) {
                        final PayTask payTask = new PayTask(null);
                        return Observable.just(orderInfo)
                                .subscribeOn(io())
                                .map(new Func1<String, PayStatus>() {
                                    @Override
                                    public PayStatus call(String orderInfo) {
                                        Map<String, String> payResult = payTask.payV2(orderInfo, true);
                                        return new AlipayStatus(payResult.get("resultStatus"),
                                                payResult.get("result"),
                                                payResult.get("memo"));
                                    }
                                })
                                .observeOn(mainThread());
                    }
                });
    }

    public Observable<PayResult> getPayResult(String orderId) {
        return paymentService.getPayResult(orderId)
                .subscribeOn(io())
                .observeOn(mainThread());
    }
}

@@ -47,87 +47,78 @@
 ort 
-com.alipay.sdk.app.PayTask;%0Aimport java.util.Map;%0A%0Aimport javax.inject.Inject;%0A
+javax.inject.Inject;%0A%0Aimport beer.brew.vendingmachine.data.model.Beer;
 %0Aimp
@@ -147,34 +147,43 @@
 achine.data.
-model.Beer
+remote.OrderService
 ;%0Aimport bee
@@ -223,23 +223,25 @@
 .Payment
-Service
+Processor
 ;%0Aimport
@@ -270,28 +270,46 @@
 ine.
-payment.OrderManager
+data.remote.PaymentProcessor.PayStatus
 ;%0Aim
@@ -350,25 +350,20 @@
 ent.
-data.AlipayStatus
+OrderManager
 ;%0Aim
@@ -479,106 +479,21 @@
 ort 
-beer.brew.vendingmachine.payment.data.PayStatus;%0Aimport rx.Observable;%0Aimport rx.functions.Action1
+rx.Observable
 ;%0Aim
@@ -675,37 +675,80 @@
 ate 
-PaymentService paymentService
+OrderService orderService;%0A    private PaymentProcessor paymentProcessor
 ;%0A  
@@ -827,95 +827,170 @@
 tor(
-PaymentService paymentService, OrderManager orderManager) %7B%0A        this.paymentService
+OrderService orderService, PaymentProcessor paymentProcessor, OrderManager orderManager) %7B%0A        this.orderService = orderService;%0A        this.paymentProcessor
  = p
@@ -995,23 +995,25 @@
  payment
-Service
+Processor
 ;%0A      
@@ -1102,16 +1102,22 @@
 er beer,
+ final
  Order.P
@@ -1223,39 +1223,37 @@
 %0A        return 
-payment
+order
 Service.getOrder
@@ -1257,21 +1257,16 @@
 derInfo(
-order
 )%0A      
@@ -1461,439 +1461,35 @@
     
-final PayTask payTask = new PayTask(null);%0A                        return Observable.just(orderInfo)%0A                                .subscribeOn(io())%0A                                .map(new Func1%3CString, PayStatus%3E() %7B%0A                                    @Override%0A                                    public PayStatus call(String orderInfo) %7B%0A                                        Map%3CString, String%3E payResult = payTask
+return paymentProcessor
 .pay
-V2
 (ord
@@ -1500,383 +1500,15 @@
 fo, 
-true);%0A                                        return new AlipayStatus(payResult.get(%22resultStatus%22),%0A                                                payResult.get(%22result%22),%0A                                                payResult.get(%22memo%22));%0A                                    %7D%0A                                %7D)%0A                                .observeOn(mainThread()
+payType
 );%0A 
@@ -1634,23 +1634,21 @@
  return 
-payment
+order
 Service.

add Payment Processor