package com.duben.microtribe.mvp.presenters

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

class WatchRecordPresenter : BasePresenter<WatchRecordView>() {

    fun getHistory() {
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.history(), 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.getHistorySuc(baseResponse.data)
                        else -> {
                            view.getHistoryFail()
                            view.showToast(message)
                        }
                    }
                }

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

                    view.getHistoryFail()
                }

            })

    }

    fun collect(videoId: String) {
        val vo = HashMap<String, Any>()
        vo["vedioId"] = videoId
        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(videoId: String) {
        val vo = HashMap<String, Any>()
        vo["vedioId"] = videoId
        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()
                    }

                })

    }


}