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.BaseResponse
import com.duben.microtribe.mvp.model.IndexList
import com.duben.microtribe.mvp.model.NineShowBean
import com.duben.microtribe.mvp.model.VipBean
import com.duben.microtribe.mvp.views.VideoView
import java.util.HashMap

class VideoPresenter : BasePresenter<VideoView>() {

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

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

                    override fun onNext(baseResponse: BaseResponse<VipBean>) {
                        if (isLinkView) return
                        val code: Int = baseResponse.getStatus()
                        when (code) {
                            200 -> {
                                view.getVipProductSuc(baseResponse.data)
                            }
                            else -> {
                                view.showToast(baseResponse.getMessage())
                            }
                        }
                    }
                })
    }

    fun getIndexList(thirdId: String, csj: Boolean = false) {
        val vo = HashMap<String, Any>()
        if (csj) {
            vo["thirdId"] = thirdId
        } else {
            vo["vedioId"] = thirdId
        }
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.getIndexList(vo),
                object : BaseSubscriber<BaseResponse<IndexList>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                        view.hideLoading()
                    }

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

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

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

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

                        view.getIndexListFail()
                    }

                })
    }

    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()
                    }

                })

    }

    fun unlock(vo: HashMap<String, Any>) {
        AppHttpManager.getInstance(loanApplication)
            .call(
                loanService.unlock(vo),
                object : BaseSubscriber<BaseResponse<IndexList>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                        view.hideLoading()
                    }

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

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

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

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

                        view.unlockFail()
                    }

                })

    }

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

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

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

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

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

    }

}