package com.zxhl.main.page.presenter

import android.util.Log
import com.zxhl.cms.ad.upload.model.Response
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 com.zxhl.cms.net.model.uc.UserInfo
import com.zxhl.main.page.contract.EditUserInfoContract
import okhttp3.MultipartBody

class EditUserInfoPresenter : EditUserInfoContract.Presenter {
    private val mView: EditUserInfoContract.View


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

    override fun getUserInfo() {
        ApiClient.userInfoAPi.userInfo.compose<Response<UserInfo>>(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<UserInfo>() {
                override fun onSuccess(result: UserInfo) {
                    mView.setData(result)
                }

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

                }
            })
    }

    override fun updateUserInfo(
        birthday: String,
        description: String,
        headImage: MultipartBody.Part?,
        height: String,
        income: String,
        nickname: String,
        occupation: String,
        province: String,
        weight: String
    ) {
        ApiClient.homeApi.updateUserInfo(
            birthday,
            description,
            headImage,
            height,
            income,
            nickname,
            occupation,
            province,
            weight
        )
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView.updateSuc()
                }

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

    override fun updateUserInfoNoHeadImg(
        birthday: String,
        description: String,
        height: String,
        income: String,
        nickname: String,
        occupation: String,
        province: String,
        weight: String
    ) {
        ApiClient.homeApi.updateUserInfoNoHeadImg(
            birthday,
            description,
            height,
            income,
            nickname,
            occupation,
            province,
            weight
        )
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView.updateSuc()
                }

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

    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()
                }
            })
    }

    override fun updateUserHeadImg(headImage: MultipartBody.Part?) {
        ApiClient.homeApi.updateUserHeadImg(
            headImage
        )
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView.updateSuc()
                }

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

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

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

    override fun updateUserSex(sex: String) {
        ApiClient.homeApi.updateUserGender(
            sex
        )
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView.updateSuc()
                }

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

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

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

    override fun updateIntroduction(indroduction: String) {
        ApiClient.homeApi.updateUserDescrption(
            indroduction
        )
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    mView.updateSuc()
                }

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


}