package com.ym.userinfo.module.presenter

import com.ym.library.module.WithdrawEntity
import com.ym.library.net.BaseObserver
import com.ym.library.net.RxSchedulers
import com.ym.modulecommon.module.UserInfoEntity
import com.ym.userinfo.module.contract.UserContract
import com.ym.userinfo.module.entity.LotteryEntity
import com.ym.userinfo.module.net.UserApiClient

/**
 *Craeted by ${junqi.li}
 *Craeted by 2020/8/26
 */
class UserPresenter : UserContract.Presenter {

    private val mView: UserContract.View

    constructor(mView: UserContract.View) {
        this.mView = mView
    }


    //获取提现配置
    override fun requestWithdrawList() {
        UserApiClient.userApi.getWithdrawList()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<WithdrawEntity>>() {
                override fun onSuccess(result: List<WithdrawEntity>) {
                    //常规提现
                    mView.setWithdrawDataSuccess(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    mView.setWithdrawDataFailure()
                }
            })
    }

    //我的抽奖/提现列表
    override fun requestGuessMusicLottery() {
        UserApiClient.userApi.getGuessMusicLottery()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<LotteryEntity>>() {
                override fun onSuccess(result: List<LotteryEntity>) {
                    mView.setGuessMusicLotterySuccess(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    mView.setGuessMusicLotteryFailure()
                }
            })
    }

    //抽奖配置
    override fun requestLotteryConfig(sessionId: String) {
        UserApiClient.userApi.getLotteryConfig(sessionId)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<List<WithdrawEntity>>() {
                override fun onSuccess(result: List<WithdrawEntity>) {
                    mView.setLotteryConfigSuccess(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    mView.setLotteryConfigFailure()
                }
            })
    }

    //用户金币
    override fun requestUserGold() {
        UserApiClient.userApi.getUserGold()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<UserInfoEntity>() {
                override fun onSuccess(result: UserInfoEntity) {
                    mView.setUserGold(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    mView.setUserGoldFailure()
                }
            })
    }

    //微信是否绑定
    override fun requestIsWxBind() {
        val token =com.ym.modulecommon.utils.SettingPreference.getToken()
        UserApiClient.userApi.getIsBindWithdrawWx(token)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<UserInfoEntity>() {
                override fun onSuccess(result: UserInfoEntity) {
                    mView.wxBindSuccess(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    if (null != errorMsg) {
                        mView.wxBindFailure(errorMsg?:"")
                    }
                }
            })
    }

    //微信提现
    override fun requestWxWithdraw(wdId: Int) {
        UserApiClient.userApi.getUserWxWithdraw(wdId)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<WithdrawEntity>() {
                override fun onSuccess(result: WithdrawEntity) {
                    mView.wxWithdrawSuccess(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    mView.wxWithdrawFailure(errorMsg?:"")
                }
            })
    }

    //金币兑换
    override fun requestCoin2cash(sId:String) {
        UserApiClient.userApi.getUserCoin2cash(sId)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<WithdrawEntity>() {
                override fun onSuccess(result: WithdrawEntity) {
                    mView.coin2cashSuccess(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    mView.coin2cashFailure(errorMsg?:"")
                }
            })
    }

    //注意事项
    override fun requestUserMind() {
        UserApiClient.userApi.getUserMind()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<UserInfoEntity>() {
                override fun onSuccess(result: UserInfoEntity) {
                    mView.userMindSuccess(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    mView.userMindFailure()
                }
            })
    }

    override fun requestCoin2cashCheck() {
        UserApiClient.userApi.getUserCoin2cash_check()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<WithdrawEntity>() {
                override fun onSuccess(result: WithdrawEntity) {
                    mView.coin2cashCheckSuccess(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    mView.coin2cashCheckFailure(errorMsg?:"")
                }
            })
    }

    override fun requestUserWxWithdrawLottery(sessionId: String) {
        UserApiClient.userApi.getUserWxWithdrawLottery(sessionId)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<WithdrawEntity>() {
                override fun onSuccess(result: WithdrawEntity) {
                    mView.userWxWithdrawLotterySuccess(result)
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String?) {
                    mView.userWxWithdrawLotteryFailure(errorMsg?:"")
                }
            })
    }


}