package tech.starwin.mvp.presenter;


import android.text.TextUtils;

import java.util.List;

import io.reactivex.Observable;
import okhttp3.ResponseBody;
import tech.starwin.LibConfig;
import tech.starwin.base.BasePresenter;
import tech.starwin.impl.HttpObserver;
import tech.starwin.mvp.api.LoanApi;

import com.common.bean.HarvestGrantBean;
import com.common.bean.HistoryLoanAppInfoBean;
import com.common.bean.LatestLoanAppBean;
import com.common.bean.ProductBean;
import com.common.toolbox.tracker.TrackEvent;

import tech.starwin.utils.LoginManager;
import tech.starwin.utils.PreferencesManager;
import tech.starwin.utils.TrackEventHelper;

/**
 * Created by SiKang on 2018/9/20.
 */
public class LoanPresenter extends BasePresenter<LoanApi> {
    public LoanPresenter() {
        super(LoanApi.class);
    }

    /**
     * 获取贷款额度、贷款周期 可选范围
     */
    public void getLoanRange(String action) {
        handleRequest(action, apiService.getLoanRange());
    }

    /**
     * 验证贷款资格
     */
    public void checkQualification(String action) {
        handleRequest(action, apiService.isQualification(LoginManager.get().getToken()));
    }

    /**
     * 获取贷款进度
     */
    public void getCertifyProgress(String action) {
        boolean tdFlag = !TextUtils.isEmpty(LibConfig.TONGDUN_PARENT_KEY);
        handleRequest(action, apiService.getCertifyProgress(LoginManager.get().getToken(), tdFlag));
    }

    /**
     * 获取贷款详情
     */
    public void getLoanDetail(String action, ProductBean productBean) {
        handleRequest(action, apiService.getLoanAmoutData(productBean.getCheckedAmount(), productBean.getCheckedPeriod(), productBean.getPeriodUnit(), productBean.getId()));
    }

    /**
     * 获取最新的贷款申请信息
     */
    public void getLatestLoanApp(String action) {
        handleRequest(action, apiService.getLatestLoanApp(LoginManager.get().getToken()));
    }

    /**
     * 获取贷款记录
     */
    public void getLoanHistory(String action) {
        handleRequest(apiService.getLoanAppAll(LoginManager.get().getToken()), new HttpObserver<List<HistoryLoanAppInfoBean>>() {
            @Override
            public void onStart() {
                view.onHttpStart(action, true);
            }

            @Override
            public void onSuccess(List<HistoryLoanAppInfoBean> data) {
                view.onHttpSuccess(action, data);
            }

            @Override
            public void onError(int code, String msg) {
                view.onHttpError(action, msg);
            }

            @Override
            public void onFinish() {
                view.onHttpFinish(action);
            }
        });
    }


    /**
     * 取消贷款
     */
    public void cancelLoan(String action, String loanAppId) {
        handleRequest(apiService.cancelLoan(loanAppId, LoginManager.get().getToken()), new HttpObserver<ResponseBody>() {
            @Override
            public void onStart() {
                view.onHttpStart(action, true);
            }

            @Override
            public void onSuccess(ResponseBody data) {
                TrackEventHelper.logEvent(TrackEvent.Api.LOAN_CANNELED);
                view.onHttpSuccess(action, data);
            }

            @Override
            public void onError(int code, String msg) {
                view.onHttpError(action, msg);
            }

            @Override
            public void onFinish() {
                view.onHttpFinish(action);
            }
        });
    }


    /**
     * 商品列表
     */
    public void getProductList(String action) {
        if (TextUtils.isEmpty(LoginManager.get().getToken())) {
            handleRequest(action, apiService.getProductsDefault());
        } else {
            handleRequest(action, apiService.getProducts());
        }
    }

    /**
     * 获取银行列表
     */

    public void getBankList(String action) {
        handleRequest(action, apiService.getBankList());
    }


    /**
     * 申请贷款
     */
    public void applyLoan(String action, LatestLoanAppBean loanInfo) {
        Observable<ResponseBody> observable = apiService.addBank(loanInfo.getLoanAppId(),
                loanInfo.getBankCode(),
                loanInfo.getCardNo(),
                loanInfo.getApplyFor(),
                loanInfo.getApplyChannel(),
                "ANDROID",
                loanInfo.getCouponId(),
                LoginManager.get().getToken());

        handleRequest(observable, new HttpObserver<ResponseBody>() {
            @Override
            public void onStart() {
                view.onHttpStart(action, true);
            }

            @Override
            public void onSuccess(ResponseBody data) {
                TrackEventHelper.logEvent(TrackEvent.Api.BANK_INFO_SUBMITED);
                view.onHttpSuccess(action, data);
            }

            @Override
            public void onError(int code, String msg) {
                view.onHttpError(action, msg);
            }

            @Override
            public void onFinish() {
                view.onHttpFinish(action);
            }
        });
    }

    /**
     * 获取付款方式
     */
    public void getDepostMethods(String action) {
        handleRequest(action, apiService.getDepostMethods(LoginManager.get().getToken()));
    }


    /**
     * 是否授权数据
     */
    public void getHarvestGrant(String action) {
        handleRequest(action, apiService.getHarvestGrant(LoginManager.get().getToken()));
    }


    /**
     * 创建还款订单
     */
    public void getRepayOrder(String action, LatestLoanAppBean loanAppBean, final double payAmount, final String repayMethodorBank) {
        handleRequest(action,
                apiService.getRepayOrder(loanAppBean.getLoanAppId(),
                        "IDR",
                        repayMethodorBank,
                        payAmount,
                        LoginManager.get().getToken()
                ));
    }

}
