package com.mints.goodmoney.mvp.presenters

import android.content.Context
import android.text.TextUtils
import com.mints.goodmoney.MintsApplication
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.common.DeviceInfo
import com.mints.library.net.neterror.BaseSubscriber
import com.mints.library.net.neterror.Throwable
import com.mints.goodmoney.manager.AppHttpManager
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.PanView
import com.mints.goodmoney.utils.DeviceUuidFactory
import net.grandcentrix.tray.AppPreferences
import java.util.*

class PanPresenter : BasePresenter<PanView>() {

    /**
     * 游客登录
     */
    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() {
                            }

                            override fun onError(e: Throwable) {
                            }

                            override fun onNext(baseResponse: BaseResponse<UserBean>) {
                                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)
                                }
                            }
                        })
    }

    /**
     * 提交设备信息
     *
     * @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"] = "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) {

                                }
                            }
                        })
    }
}