package com.zxhl.cms.utils

import android.text.TextUtils
import com.zxhl.cms.ad.upload.model.Response
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.ApiClient.userInfoAPi
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.SettingPreference
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.AppInEntity
import com.zxhl.cms.net.model.uc.UserInfo

/**
 * Created by gaoleichao on 2021/8/25
 */
object UserDataUtils {
    private var isLoadAppConfig = false;
    fun updateUserInfo(callback: AdCallback<String>?) {
        userInfoAPi.userInfo.compose<Response<UserInfo>>(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<UserInfo>() {
                override fun onSuccess(result: UserInfo) {
                    SettingPreference.saveUserInfo(result)
                    SettingPreference.setFlowTag(result.flowTag)
                    SettingPreference.setUserName(result.nickname)
                    SettingPreference.setUserHeadImg(result.headImage)
                    SettingPreference.setUserCoinNum(result.ava)
                    SettingPreference.setIsNewUser(result.isNewUser)
                    callback?.onResult(0, "成功")
                    if (!isLoadAppConfig) {
                        isLoadAppConfig = true
                        getAppInfo()
                    }
                }

                override fun onFailure(e: Throwable, code: String, errorMsg: String) {
                    callback?.onResult(-1, "成功")
                }
            })
    }

    fun getAppInfo() {
        ApiClient.homeApi.getAppInfo()
            .compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<AppInEntity>() {
                override fun onSuccess(result: AppInEntity?) {
                    Constant.Switch.isOpenLogin = result?.login.equals("1")
                }

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

                }
            })
    }
}