package com.mints.goodmoney.mvp.presenters

import com.mints.goodmoney.MintsApplication
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.common.DeviceInfo
import com.mints.goodmoney.manager.AppHttpManager
import com.mints.goodmoney.manager.JpushManager
import com.mints.goodmoney.manager.ShumeiManager
import com.mints.goodmoney.manager.UserManager
import com.mints.goodmoney.mvp.model.BaseResponse
import com.mints.goodmoney.mvp.model.UserBean
import com.mints.goodmoney.mvp.views.LoginView
import com.mints.goodmoney.utils.DeviceUuidFactory
import com.mints.library.net.neterror.BaseSubscriber
import com.mints.library.net.neterror.Throwable
import net.grandcentrix.tray.AppPreferences
import java.util.*

class LoginPresenter : BasePresenter<LoginView>() {

    /**
     * 手机验证码
     *
     * @param mobile
     */
    fun sendMobileCode(mobile: String) {
        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
                                val code: Int = baseResponse.getStatus()
                                val message: String = baseResponse.getMessage()
                                when (code) {
                                    200 -> {
                                    }
                                    else -> {
                                        view.showToast(message)
                                    }
                                }
                            }
                        })
    }

    /**
     * 绑定
     */
    fun bindingMobile(mobile: String, smsCode: String) {
        view.showLoading("加载中...")
        val vo = HashMap<String, Any>()
        vo["mobile"] = mobile
        vo["smsCode"] = smsCode
        vo["device"] = DeviceUuidFactory().deviceUuid.toString()
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.bindingMobile(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
                                val code = baseResponse.getStatus()
                                val message = baseResponse.getMessage()
                                val data: UserBean? = baseResponse.getData()
                                when (code) {
                                    200 -> if (data != null) {
                                        UserManager.getInstance().saveUserInfo(data)
                                        view.loginSuc()
                                    }
                                    else -> 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()
        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
                                val code = baseResponse.getStatus()
                                val message = baseResponse.getMessage()
                                val data: UserBean? = baseResponse.getData()
                                when (code) {
                                    200 -> if (data != null) {
                                        UserManager.getInstance().saveUserInfo(data)
                                        saveTerminalInfo(data.consumer.pk_id)
                                        view.loginSuc()
                                    }
                                    else -> view.showToast(message)
                                }
                            }
                        })
    }

    /**
     * 微信登录
     */
    fun login(wxInfo: String) {
        view.showLoading("加载中...")
        val vo = HashMap<String, Any>()
        vo["shumeiId"] = ShumeiManager.getInstance().shumeiDeviceId
        vo["device"] = DeviceUuidFactory().deviceUuid.toString()
        vo["wxInfo"] = wxInfo
        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
                                val code = baseResponse.status
                                val message = baseResponse.message
                                val data = baseResponse.data
                                when (code) {
                                    200 -> if (data != null) {
                                        UserManager.getInstance().saveUserInfo(data)
                                        saveTerminalInfo(data.consumer.pk_id)
                                        view.loginSuc()
                                    }
                                    else -> view.showToast(message)
                                }
                            }
                        })
    }

    /**
     * 提交设备信息
     *
     * @param context
     */
    fun saveTerminalInfo(userId:Long) {
        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"] = "android"
        vo["model"] = deviceInfo.brand + " " + deviceInfo.mobileModel
        vo["uuid"] = DeviceUuidFactory().deviceUuid
        vo["osversion"] = deviceInfo.oSVersion
        vo["appversion"] = deviceInfo.versionName
        vo["shumeiId"] = ShumeiManager.getInstance().getShumeiDeviceId()

        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 -> {
                                        JpushManager.getInstance().setJpushService(userId.toString())
                                    }
                                }
                            }
                        })
    }
}