package com.duben.microtribe.mvp.presenters

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

class VipPresenter : BasePresenter<VipView>() {

    fun getMyInfo() {
        val vo = HashMap<String, Any>()
        vo["os"] = "android"
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.getMyInfo(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

                        val code: Int = baseResponse.status
                        val message: String = baseResponse.message
                        when (code) {
                            200 -> {
                                UserManager.getInstance().saveUserInfo(baseResponse.data)
                                view.getUserSuc(baseResponse.data)

                            }
                            else -> {
                                view.showToast(message)
                            }
                        }
                    }
                })
    }

    /**
     * 游客登录
     */
    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["payChannel"] = "ALIPAY"
        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("ALIPAY", 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)
                            }
                        }
                    }
                })
    }

    fun orders() {
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.orders(),
                object : BaseSubscriber<BaseResponse<BannerList>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                    }

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

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

                        val code = baseResponse.status
                        val message = baseResponse.message

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