package com.duben.microtribe.mvp.presenters

import com.duben.microtribe.MintsApplication
import com.duben.microtribe.common.Constant
import com.duben.microtribe.common.DeviceInfo
import com.duben.microtribe.manager.AppHttpManager
import com.duben.microtribe.manager.UserManager
import com.duben.microtribe.mvp.model.*
import com.duben.microtribe.mvp.views.HomeView
import com.duben.microtribe.utils.AppPreferencesManager
import com.duben.microtribe.utils.DeviceUuidFactory
import com.duben.library.net.neterror.BaseSubscriber
import com.duben.library.net.neterror.Throwable
import java.util.HashMap

class HomePresenter : BasePresenter<HomeView>() {

    fun getHomeV1Types() {
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.homeV1Types,
                object : BaseSubscriber<BaseResponse<HotStyleTypesList>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                    }

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

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

                        val code = baseResponse.status
                        val message = baseResponse.message

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

    fun topTabs() {
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.topTabs(),
                object : BaseSubscriber<BaseResponse<BannerList>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                    }

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

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

                        val code = baseResponse.status
                        val message = baseResponse.message

                        when (code) {
                            200 -> {
                                view.topTabsSuc(baseResponse.data)
//                                showTurn()
                            }
                            else -> {
                                view.showToast(message)
                            }
                        }
                    }
                })
    }

    private fun showTurn() {
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.showTurn(),
                object : BaseSubscriber<BaseResponse<NineShowBean>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                    }

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

                        val code = baseResponse.status
                        val message = baseResponse.message
                        when (code) {
                            200 -> {
                                val data = baseResponse.data
                                if (data != null) {
                                    view.showTurnSuc(data)

//                                    AppPreferencesManager.get()
//                                        .put(Constant.LUCKY_FLAG, data.isShow)
//                                    AppPreferencesManager.get()
//                                        .put(Constant.LUCKY_COMPLETE, data.complete)
//                                    AppPreferencesManager.get()
//                                        .put(Constant.LUCKY_NEED, data.need)
                                }
                            }
                        }
                    }

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

    }

    fun orders() {
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.orders(),
                object : BaseSubscriber<BaseResponse<BannerList>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                    }

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

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

                        val code = baseResponse.status
                        val message = baseResponse.message

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

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

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

                    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()
                            }
                            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"] = 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
                        val code = baseResponse.status
                        when (code) {
                            200 -> {
                                topTabs()
                                orders()
                                getHomeV1Types()
                            }
                        }
                    }
                })
    }

    fun getSoltVedio() {
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.getSoltVedio(),
                object : BaseSubscriber<BaseResponse<IndexList>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                    }

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

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

                        val code = baseResponse.status
                        val message = baseResponse.message

                        when (code) {
                            200 -> {
                                view.getSoltVedioSuc(baseResponse.data)
                            }
                        }
                    }
                })
    }

    fun getRecommendVedio() {
        AppHttpManager.getInstance(loanApplication)
            .call(loanService.getRecommendVedio(),
                object : BaseSubscriber<BaseResponse<RecoBean>>() {
                    override fun onCompleted() {
                        if (isLinkView) return
                    }

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

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

                        val code = baseResponse.status

                        when (code) {
                            200 -> {
                                view.getRecommendVedioSuc(baseResponse.data)
                            }
                        }
                    }
                })
    }
}