package com.yidianling.tests.list.presenter

import com.hannesdorfmann.mosby3.mvp.MvpNullObjectBasePresenter
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.yidianling.tests.TestRetrofitApi
import com.yidianling.tests.list.view.TestListActivityView
import com.yidianling.ydlcommon.data.YdlDataManager
import com.yidianling.ydlcommon.http.RxUtils
import com.yidianling.ydlcommon.http.ThrowableConsumer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 * TestListActivityPresenter
 * Created by zqk on 17-11-22.
 */
class TestListActivityPresenter : MvpNullObjectBasePresenter<TestListActivityView>() {
    fun fetchTestTypeList() {
        TestRetrofitApi.getTestRetrofitApi().fetchTestCategory()
                .compose(RxUtils.resultJavaData())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                        view.onTestCategoryListFetched(resp)
                }, { t ->
                    view.onFailed(t.message)
                })
    }

    /**
     * 请求红包列表
     */
    fun requestCoupon() {
        YdlDataManager.getHttp().getCoupon()
                .subscribeOn(Schedulers.io())
                .compose(RxUtils.resultData())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Consumer {
                    view.couponResponse(it)
                }, object : ThrowableConsumer() {
                    override fun accept(msg: String) {
                        view.onFailed(msg)
                    }
                })
    }
}