package com.yidianling.tests.search

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.hannesdorfmann.mosby3.mvp.MvpNullObjectBasePresenter
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.yidianling.common.tools.ToastUtil
import com.yidianling.tests.TestRetrofitApi
import com.yidianling.tests.home.http.TestHomeDataManager
import com.yidianling.tests.home.param.TestHomeParam
import com.yidianling.tests.list.model.TestListCommand
import com.yidianling.tests.list.model.bean.Test
import com.yidianling.ydlcommon.app.YdlCommonApp
import com.yidianling.ydlcommon.http.RxUtils
import com.yidianling.ydlcommon.http.ThrowableConsumer
import com.yidianling.ydlcommon.http.YdlRetrofitUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 * TestSearchPresenter
 * Created by zqk on 17-12-5.
 */
class TestSearchPresenter : MvpNullObjectBasePresenter<TestSearchView>() {

    fun fetchHotTests() {
        view.showLoadingView()
        val cmd = TestListCommand()
        cmd.tab = "hot_test"
        cmd.page = 1
        TestRetrofitApi.getTestRetrofitApi()
                .fetchTopSearch()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    if (resp.code == "200") {
                        view.hideNoResultView()
                        view.onRecommendSearchListResponse(resp.data)
                    } else {
                        ToastUtil.toastShort(resp.msg)
                        view.hideLoadingView()
                    }

                }, { throwble ->
                    YdlRetrofitUtils.handleError(YdlCommonApp.getApp(), throwble)
                })
    }


    /**
     * 测评热门推荐
     */
    fun hotRecommendRequest() {
        view.showLoadingView()
        TestHomeDataManager
                .getHttp()
                .newHomeRequest(TestHomeParam( 0))
                .compose(RxUtils.resultData())
                .map { it }
                .filter { it != null }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Consumer {
                    if (it[3].body!=null){
                        var jsonStr = Gson().toJson(it[3].body)
                        val list = Gson().fromJson<MutableList<Test>>(jsonStr, object : TypeToken<MutableList<Test>>() {
                        }.type)
                        view.hideNoResultView()
                        view.onHotRecommendResponse(list)
                    }else{
                        view.hideLoadingView()
                    }
                }, object : ThrowableConsumer() {
                    override fun accept(msg: String) {
                        view.showError(msg)
                    }
                })
    }

    fun searchTests(keyword: String?, page: Int) {
        if (page == 1) {
            view.showLoadingView()
        }
        val cmd = TestListCommand()
        cmd.tab = "search"
        cmd.page = page
        cmd.keyword = keyword
        TestRetrofitApi.getTestRetrofitApi()
                .fetchTestList(YdlRetrofitUtils.getMaps(cmd))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    if (resp.code == 0) {
                        view.hideLoadingView()
                        if (page == 1 && (resp.data.testList == null || resp.data.testList?.size == 0)) {
                            view.showNoResultView()
                            view.showSearchResultView(resp.data.testList, page)
                        } else {
                            view.hideNoResultView()
                            view.showSearchResultView(resp.data.testList, page)
                        }
                    } else {
                        view.hideLoadingView()
                        ToastUtil.toastShort(resp.msg)
                    }
                }, { throwble ->
                    YdlRetrofitUtils.handleError(YdlCommonApp.getApp(), throwble)
                })
    }
}