package com.duben.happyplaylet.mvp.presenters

import com.google.gson.JsonObject
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import com.duben.happyplaylet.manager.AppHttpManager
import com.duben.happyplaylet.mvp.model.BannerList
import com.duben.happyplaylet.mvp.model.BaseResponse
import com.duben.happyplaylet.mvp.model.NineShowBean
import com.duben.happyplaylet.mvp.views.RecommendView
import java.util.HashMap

class RecommendPresenter : BasePresenter<RecommendView>() {

    fun autoList() {
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.autoList(), object : BaseSubscriber<BaseResponse<BannerList>>() {
                override fun onCompleted() {
                    if (isLinkView) return
                    view.hideLoading()
                }

                override fun onNext(baseResponse: BaseResponse<BannerList>) {
                    if (isLinkView) return

                    view.hideLoading()
                    val code = baseResponse.status
                    val message = baseResponse.message

                    when (code) {
                        200 -> view.autoListSuc(baseResponse.data)
                        else -> {
                            view.autoListFail()
                            view.showToast(message)
                        }
                    }
                }

                override fun onError(e: Throwable?) {
                    if (isLinkView) return
                    view.hideLoading()
                    view.showToast(e?.message)

                    view.autoListFail()
                }

            })

    }


    fun collect(vedioId: String) {
        val vo = HashMap<String, Any>()
        vo["vedioId"] = vedioId
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.collect(vo), object : BaseSubscriber<BaseResponse<JsonObject>>() {
                override fun onCompleted() {
                    if (isLinkView) return
                    view.hideLoading()
                }

                override fun onNext(baseResponse: BaseResponse<JsonObject>) {
                    if (isLinkView) return

                    view.hideLoading()
                    val code = baseResponse.status
                    val message = baseResponse.message

                    when (code) {
                        200 -> view.collectSuc()
                        else -> {
                            view.collectFail()
                            view.showToast(message)
                        }
                    }
                }

                override fun onError(e: Throwable?) {
                    if (isLinkView) return
                    view.hideLoading()
                    view.showToast(e?.message)

                    view.collectFail()
                }

            })

    }

    fun cancelCollect(vedioId: String) {
        val vo = HashMap<String, Any>()
        vo["vedioId"] = vedioId
        AppHttpManager.getInstance(loanApplication)
            .call(
                loanService.cancelCollect(vo),
                object : BaseSubscriber<BaseResponse<JsonObject>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                        view.hideLoading()
                    }

                    override fun onNext(baseResponse: BaseResponse<JsonObject>) {
                        if (isLinkView) return

                        view.hideLoading()
                        val code = baseResponse.status
                        val message = baseResponse.message

                        when (code) {
                            200 -> view.cancelCollectSuc()
                            else -> {
                                view.cancelCollectFail()
                                view.showToast(message)
                            }
                        }
                    }

                    override fun onError(e: Throwable?) {
                        if (isLinkView) return
                        view.hideLoading()
                        view.showToast(e?.message)

                        view.cancelCollectFail()
                    }

                })

    }


}