package com.duben.microtribe.mvp.presenters

import com.duben.microtribe.MintsApplication
import com.duben.microtribe.common.DeviceInfo
import com.duben.microtribe.manager.*
import com.duben.microtribe.mvp.model.BaseResponse
import com.duben.microtribe.mvp.model.UserBean
import com.duben.microtribe.mvp.views.LoginView
import com.duben.microtribe.utils.DeviceUuidFactory
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import java.util.*

class LoginPresenter : BasePresenter<LoginView>() {

    /**
     * 手机验证码
     *
     * @param mobile
     */
    fun sendMobileCode(mobile: String) {
        view.showLoading("")
        val vo = HashMap<String, Any>()
        vo["mobile"] = mobile
        vo["type"] = 1
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.sendMobileCode(vo),
                object : BaseSubscriber<BaseResponse<Any>>() {
                    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<Any>) {
                        if (isLinkView) return
                        view.hideLoading()
                        val code: Int = baseResponse.getStatus()
                        val message: String = baseResponse.getMessage()
                        when (code) {
                            200 -> {
//                                        view.sendCodeSuc()
                            }
                            else -> {
//                                        view.sendCodeSuc()
                                view.showToast(message)
                            }
                        }
                    }
                })
    }

    /**
     * 登录
     */
    fun login(mobile: String, smsCode: String) {
        view.showLoading("加载中...")
        val vo = HashMap<String, Any>()
        vo["mobile"] = mobile
        vo["smsCode"] = smsCode
        vo["device"] = DeviceUuidFactory().deviceUuid.toString()
        val macAddress: String = DeviceInfo.instance.getMacAddress()
        vo["mac"] = macAddress.replace(":", "")
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.login(vo),
                object : BaseSubscriber<BaseResponse<UserBean>>() {
                    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<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()

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

    /**
     * 微信登录
     */
    fun login(wxInfo: String) {
        view.showLoading("加载中...")
        val vo = HashMap<String, Any>()
        vo["device"] = DeviceUuidFactory().deviceUuid.toString()
        vo["wxInfo"] = wxInfo
        val macAddress: String = DeviceInfo.instance.getMacAddress()
        vo["mac"] = macAddress.replace(":", "")
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.wechatlogin(vo),
                object : BaseSubscriber<BaseResponse<UserBean>>() {
                    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<UserBean>) {
                        if (isLinkView) return
                        view.hideLoading()
                        val code = baseResponse.status
                        val message = baseResponse.message
                        val data = baseResponse.data
                        when (code) {
                            200 -> if (data != null) {
                                UserManager.getInstance().saveUserInfo(data)
                                saveTerminalInfo()

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

    /**
     * 提交设备信息
     *
     * @param context
     */
    fun saveTerminalInfo() {
        val deviceInfo: DeviceInfo = DeviceInfo.instance
        val vo = HashMap<String, Any>()
        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
                    }
                })
    }

}