package com.ydl.component.mvp

import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.ydl.ydlcommon.mvp.base.BasePresenter
import com.ydl.ydlcommon.utils.RxLifecycleUtils
import com.ydl.ydlnet.builder.interceptor.YDLTransformer
import com.ydl.ydlnet.client.observer.CommonObserver

/**
 * Created by haorui on 2019-09-01 .
 * Des:
 */
class DemoPresenter : BasePresenter<DemoContract.View,DemoContract.Model>(),
    DemoContract.Presenter {
    override fun loadHome() = mModel!!.getHome()
        .compose(YDLTransformer.switchSchedulers(mView))
        .compose(RxLifecycleUtils.bindToLifecycle(mView!!))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
        .subscribe(object : CommonObserver<JsonObject>() {
            override fun onError(errorMsg: String) {
            }

            override fun onSuccess(users: JsonObject) {
                var cateTitle = users.getAsJsonObject("data").getAsJsonArray("askCategoryData").get(0).asJsonObject.get("cateTitle").asString;
                mView?.setData(cateTitle)
            }
        })

    override fun loadUsers() = mModel!!.getUser()
        .compose(YDLTransformer.switchSchedulers(mView))
        .compose(RxLifecycleUtils.bindToLifecycle(mView!!))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
        .subscribe(object : CommonObserver<JsonArray>() {
            override fun onError(errorMsg: String) {
            }

            override fun onSuccess(users: JsonArray) {
                var userName = users.get(0).asJsonObject.get("login").asString
                mView?.setData(userName)
            }
        })

    override fun createModel(): DemoContract.Model {
        return DemoModel()
    }

}
