package com.duben.microtribe.mvp.presenters

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

class MyPresenter : BasePresenter<MyView>() {

    /**
     * 游客登录
     */
    fun userLogin() {
        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()
                            }
                            else -> view.showToast(message)
                        }
                    }
                })
    }

    /**
     * 提交设备信息
     *
     * @param context
     */
    fun saveTerminalInfo() {
        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 -> {
//                                showTurn()
                                getMyInfo()
                            }
                        }
                    }
                })
    }

    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

                        view.hideLoading()
                    }

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

                        view.hideLoading()
                    }

                    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 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 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 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: java.util.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)
                    }

                })

    }
}