package com.duben.microtribe.mvp.presenters

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.NineListBean
import com.duben.microtribe.mvp.model.NineShowBean
import com.duben.microtribe.mvp.views.NineView
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import java.util.HashMap

class NinePresenter : BasePresenter<NineView>() {

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

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

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

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

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

                })

    }

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

    }

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

                    override fun onNext(baseResponse: BaseResponse<NineListBean.NineBean>) {
                        if (isLinkView) return

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

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

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

    }


    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()
                            else -> {
                                view.showToast(message)
                            }
                        }
                    }

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

                })

    }

}