package com.sheep.gamegroup.presenter;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.jiuyan.samllsheep.SheepApp;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ljy on 2018/3/18.
 */

public class WithdrawalPresenter implements WithdrawalContract.Presenter {

    private WithdrawalContract.View view;
    private ApiService apiService;

    @Inject
    public WithdrawalPresenter(WithdrawalContract.View view, ApiService apiService) {
        this.view = view;
        this.apiService = apiService;
    }

    @Override
    public void goWithdrawal(JSONObject jsonObject) {
        apiService.goWithdrawal(jsonObject)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.failData(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.updateView(baseMessage);
                    }
                });
    }

    @Override
    public void getCanWithdrawAmount() {
        apiService.getCanWithdrawAmount()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.failBalanceData(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.updateBalanceView(baseMessage);
                    }
                });
    }

    @Override
    public void getWithdrawAmountOption() {
        apiService.getWithdrawAmountOption()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.failWithdrawAmountOptionData(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.updateWithdrawAmountOptionView(baseMessage);
                    }
                });
    }
}
