package com.zxhl.uc.page.fragment

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.chat.PictureEntity
import okhttp3.MultipartBody

class UserPresenter : UserContract.Presenter {
    private val mView: UserContract.View

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

    override fun upload(file: MultipartBody.Part?) {
        ApiClient.homeApi.upload(file)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<PictureEntity>() {
                override fun onSuccess(result: PictureEntity?) {
                    if (result != null) {
                        mView.uploadSuc(result.url.toString())
                    }
                }

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

    override fun pictures(list:String?) {
        ApiClient.homeApi.updatePictures(list)
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    if (result != null) {
                        mView.updatePicSuc()
                    }
                }

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