package com.duben.happyplaylet.mvp.presenters

import com.duben.happyplaylet.MintsApplication
import com.duben.happyplaylet.manager.AppHttpManager
import com.duben.happyplaylet.mvp.model.BaseResponse
import com.duben.happyplaylet.mvp.model.VipBean
import com.duben.happyplaylet.mvp.model.WxPayParamBean
import com.duben.happyplaylet.mvp.views.VipView
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import com.duben.happyplaylet.common.DeviceInfo
import com.duben.happyplaylet.manager.UserManager
import com.duben.happyplaylet.mvp.model.UserBean
import com.duben.happyplaylet.utils.DeviceUuidFactory
import java.util.*

class VipPresenter : BasePresenter<VipView>() {

    /**
     * 游客登录
     */
    fun userLogin(isInstallAlipay:Boolean) {
        val vo = HashMap<String, Any>()
        vo["device"] = DeviceUuidFactory().deviceUuid.toString()
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.visitorlogin(vo),
                object : BaseSubscriber<BaseResponse<UserBean>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                    }

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

                    override fun onNext(baseResponse: BaseResponse<UserBean>) {
                        if (isLinkView) return
                        view.hideLoading()
                        val code = baseResponse.getStatus()
                        val message = baseResponse.getMessage()
                        val data: UserBean? = baseResponse.getData()
                        when (code) {
                            200 -> if (data != null) {
                                UserManager.getInstance().saveUserInfo(data)
                                saveTerminalInfo(isInstallAlipay)
                            }
                            else -> view.showToast(message)
                        }
                    }
                })
    }

    /**
     * 提交设备信息
     *
     * @param context
     */
    fun saveTerminalInfo(isInstallAlipay:Boolean) {
        val vo = HashMap<String, Any>()
        val deviceInfo: DeviceInfo = DeviceInfo.instance
        val macAddress: String = deviceInfo.getMacAddress()
        val mac = macAddress.replace(":", "")
        vo["mac"] = mac
        vo["mac1"] = macAddress
        vo["androidid"] = deviceInfo.getAndroidId(null)
        vo["imei"] = deviceInfo.iMEI
        vo["oaid"] = MintsApplication.OAID
        vo["os"] = if (deviceInfo.isHarmonyOS()) "android-HarmonyOS" else "android"
        vo["model"] = deviceInfo.newModel
        vo["uuid"] = DeviceUuidFactory().deviceUuid
        vo["osversion"] = deviceInfo.oSVersion
        vo["appversion"] = deviceInfo.versionName
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.saveTerminalInfo(vo),
                object : BaseSubscriber<BaseResponse<Any>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                    }

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

                    override fun onNext(baseResponse: BaseResponse<Any>) {
                        if (isLinkView) return
                        val code = baseResponse.status
                        when (code) {
                            200 -> getVipProducts(isInstallAlipay)
                        }
                    }
                })
    }

    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 getVipProducts(isInstallAlipay:Boolean) {
        val vo = HashMap<String, Any>()
        vo["isInstallAlipay"] = isInstallAlipay

        view.showLoading("加载中...")
        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 getVipPayParams(payChannel: String, pid: String,isInstallAlipay:Boolean) {
        val vo = HashMap<String, Any>()
        vo["payChannel"] = payChannel
        vo["pid"] = pid
        vo["isInstallAlipay"] = isInstallAlipay
        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(payChannel, baseResponse.data)
                            }
                            else -> {
                                view.showToast(baseResponse.getMessage())
                            }
                        }
                    }
                })
    }

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