package com.mints.goodmoney.mvp.presenters

import com.mints.goodmoney.manager.AppHttpManager
import com.mints.goodmoney.manager.UserManager
import com.mints.goodmoney.mvp.model.BaseResponse
import com.mints.goodmoney.mvp.model.DrawcashBean
import com.mints.goodmoney.mvp.model.UserBean
import com.mints.goodmoney.mvp.views.SettingsView
import com.mints.library.net.neterror.BaseSubscriber
import com.mints.library.net.neterror.Throwable
import java.util.*

class SettingsPresenter : BasePresenter<SettingsView>() {
    /**
     * 修改用户信息
     */
    fun editUserMsg(wxInfo: String) {
        val vo = HashMap<String, Any>()
        vo["type"] = "OPENID"
        vo["value"] = wxInfo
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.bindingWechat(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
                                when (code) {
                                    200 -> {
                                        val data = baseResponse.data
                                        if (data != null) {
                                            UserManager.getInstance().saveUserInfo(data)
                                            view.editUserMsgSuc()
                                        }
                                    }
                                    else -> view.showToast(message)
                                }
                            }
                        })
    }

    /**
     * 获取用户配置信息
     */
    fun getUserTaskMsg() {
        view.showLoading("加载中...")
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.drawcashUserTaskMsg,
                        object : BaseSubscriber<BaseResponse<DrawcashBean>>() {
                            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<DrawcashBean>) {
                                if (isLinkView) return
                                val code = baseResponse.status
                                val message = baseResponse.message
                                when (code) {
                                    200 -> view.getUserTaskMsgSuc(baseResponse.data)
                                    else -> view.showToast(message)
                                }
                            }
                        })
    }
}