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.ShumeiManager
import com.mints.goodmoney.manager.UserManager
import com.mints.goodmoney.mvp.model.*
import com.mints.goodmoney.mvp.views.MyView
import com.mints.goodmoney.utils.DeviceUuidFactory
import com.mints.library.net.neterror.BaseSubscriber
import com.mints.library.net.neterror.Throwable
import java.util.*

class MyPresenter : BasePresenter<MyView>() {

    /**
     * 获取签到内容
     */
    open fun getSignInHomePageMsg() {
//        view.showLoading("加载中...");
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.signInHomePageMsg,
                        object : BaseSubscriber<BaseResponse<SignCardBean>>() {
                            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<SignCardBean>) {
                                if (isLinkView) return
                                val code = baseResponse.status
                                val message = baseResponse.message

                                when (code) {
                                    200 -> view.getSignInHomePageMsgSuc(baseResponse.data)
                                    401 -> {
                                    }
                                    else -> view.showToast(message)
                                }
                            }
                        })
    }

    /**
     * 签到
     */
    fun signInHomePage() {
        view.showLoading("加载中...")
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.signInHomePage(),
                        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 = baseResponse.status
                                val message = baseResponse.message
                                when (code) {
                                    200 -> view.signInHomePageSuc()
                                    else -> {
                                        view.showToast(message)
                                    }
                                }
                            }
                        })
    }

    fun getAutoUserHallBaseMsg() {
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.autoUserHallBaseMsg, object : BaseSubscriber<BaseResponse<MyInfo>>() {

                    override fun onCompleted() {
                        if (isLinkView)
                            return

                        view.hideLoading()
                    }

                    override fun onNext(baseResponse: BaseResponse<MyInfo>) {
                        if (isLinkView) return

                        val code: Int = baseResponse.status
                        val message: String = baseResponse.message
                        when (code) {
                            200 -> {
                                view.getUserTaskMsgSuc(baseResponse.data)
                            }
                            401 -> view.getUserTaskMsgFail()
                            else -> {
                                view.getUserTaskMsgFail()
                                view.showToast(message)
                            }
                        }

                    }

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

                        view.hideLoading()
                        view.showToast(e!!.message)
                        view.getUserTaskMsgFail()
                    }

                })
    }

//    fun getUserHallBaseMsg() {
//        AppHttpManager.getInstance(loanApplication)
//                .call(loanService.userHallBaseMsg, object : BaseSubscriber<BaseResponse<MyInfo>>() {
//
//                    override fun onCompleted() {
//                        if (isLinkView)
//                            return
//
//                        view.hideLoading()
//                    }
//
//                    override fun onNext(baseResponse: BaseResponse<MyInfo>) {
//                        if (isLinkView) return
//
//                        val code: Int = baseResponse.status
//                        val message: String = baseResponse.message
//                        when (code) {
//                            200 -> {
//                                view.getUserTaskMsgSuc(baseResponse.data)
//                            }
//                            401 -> view.getUserTaskMsgFail()
//                            else -> {
//                                view.getUserTaskMsgFail()
//                                view.showToast(message)
//                            }
//                        }
//
//                    }
//
//                    override fun onError(e: Throwable?) {
//                        if (isLinkView) return
//
//                        view.hideLoading()
//                        view.showToast(e!!.message)
//                        view.getUserTaskMsgFail()
//                    }
//
//                })
//    }

    /**
     * 热门活动接口
     */
    fun myHotActivity() {
        val vo = HashMap<String, Any>()
        vo["os"] = "android"
        vo["isShowYwhzGame"] = true
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.myHotActivity(vo),
                        object : BaseSubscriber<BaseResponse<BannerBean?>>() {
                            override fun onCompleted() {
                                if (isLinkView) return
                            }

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

                            override fun onNext(baseResponse: BaseResponse<BannerBean?>) {
                                if (isLinkView) return
                                val data: BannerBean? = baseResponse.data
                                if (data != null) {
                                    view.getMyHotActivitySuc(data)
                                }
                            }
                        })
    }

    /**
     * 游客登录
     */
    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.status
                                val message = baseResponse.message
                                val data: UserBean? = baseResponse.data
                                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"] = "android"
        vo["model"] = deviceInfo.brand + " " + deviceInfo.mobileModel
        vo["uuid"] = DeviceUuidFactory().deviceUuid
        vo["osversion"] = deviceInfo.oSVersion
        vo["appversion"] = deviceInfo.versionName
        vo["shumeiId"] = ShumeiManager.getInstance().shumeiDeviceId
        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 -> view.userLoginSuc()
                                }
                            }
                        })
    }

    /**
     * 设置邀请成功
     *
     */
    fun cmtShareStatus() {
        val vo = HashMap<String, Any>()
        vo["carrierType"] = Constant.CHALLENGE_SHAREFRIEND
        vo["value"] = "1"
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.setStatusInOneDayByCarrierType(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
                        when (baseResponse.status) {
//                                    200 -> baseMsg()
                        }
                    }
                })
    }


    fun getHallBaseMsg() {
        AppHttpManager.getInstance(loanApplication)
                .call(loanService.getHallBaseMsg(),
                        object : BaseSubscriber<BaseResponse<UserTaskMsgBean>>() {
                            override fun onCompleted() {
                                if (isLinkView) return
                                view.hideLoading()
                            }

                            override fun onError(e: Throwable) {
                                if (isLinkView) return
                                view.hideLoading()
                            }

                            override fun onNext(baseResponse: BaseResponse<UserTaskMsgBean>) {
                                if (isLinkView) return
                                val code = baseResponse.getStatus()
                                when (code) {
                                    200 -> view.getHallBaseMsgSuc(baseResponse.getData())
                                }
                            }
                        })
    }

}