package com.zxhl.main.page.presenter

import android.util.Log
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.HomeListEntity
import com.zxhl.cms.net.model.chat.DistanceEntity
import com.zxhl.main.page.contract.UserDetailContract


class UserDetailPresenter : UserDetailContract.Presenter {

    private val mView: UserDetailContract.View

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

    override fun getUserInfo(id: String?,free:String) {
        ApiClient.homeApi.getUserInfo(id,free).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<HomeListEntity>() {
                override fun onSuccess(result: HomeListEntity?) {
                    if (result != null) {
                        mView.setUserInfo(result)
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL", "ERROR" + code + errorMsg)
                }
            })

    }

    override fun followUser(uid: String?) {
        ApiClient.homeApi.followUser(uid).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView.followSuc()
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL", "ERROR" + code + errorMsg)
                }
            })
    }

    override fun likeUser(uid: String?,free:String) {
        ApiClient.homeApi.likeUser(uid,free).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {

                    mView.likeSuc()

                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL", "ERROR" + code + errorMsg)
                }
            })
    }

    override fun getDistance(free: Int, uid: String?) {
        ApiClient.homeApi.getDistance(free, uid).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<DistanceEntity>() {
                override fun onSuccess(result: DistanceEntity?) {
                    if (result != null) {
                        mView.distanceSuc(result)
                    }

                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Log.e("MXL", "ERROR" + code + errorMsg)
                    mView.distanceFail()
                }
            })
    }
}