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.WxPayParamBean
import com.duben.microtribe.mvp.views.NinePayView
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import java.util.HashMap

class NinePayPresenter : BasePresenter<NinePayView>() {

    fun getVipPayParams(pid: String) {
        val vo = HashMap<String, Any>()
        vo["payChannel"] = "ALIPAY"
        vo["pid"] = pid
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.getVipPayParams(vo),
                object : BaseSubscriber<BaseResponse<WxPayParamBean>>() {
                    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<WxPayParamBean>) {
                        if (isLinkView) return
                        val code: Int = baseResponse.getStatus()
                        when (code) {
                            200 -> {
                                view.getVipPayParamsSuc("ALIPAY",baseResponse.data)
                            }
                            else -> {
                                view.showToast(baseResponse.getMessage())
                            }
                        }
                    }
                })
    }

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

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

                    override fun onNext(baseResponse: BaseResponse<Any>) {
                        if (isLinkView) return
                        val code: Int = baseResponse.getStatus()
                    }
                })
    }

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

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

                    override fun onNext(baseResponse: BaseResponse<Any>) {
                        if (isLinkView) return
                        val code: Int = baseResponse.getStatus()
                        when (code) {
                            200 -> {
                                view.queryVipOrderSuc(isPay)
                            }
                        }
                    }
                })
    }


}