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.main.page.contract.UserDetailsContract


class UserDetailsPresenter : UserDetailsContract.Presenter {

    private val mView: UserDetailsContract.View

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

    override fun getUserDetails(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.setUserDetails(result)
                    }
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {

                }
            })
    }

    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.likeCallBack(true)
                }

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

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

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

    override fun reportUser(uid: String?,content:String) {
        ApiClient.homeApi.report(content, uid)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    if (result != null) {
                        mView.reportCallback(true)
                    }
                }

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

}