package com.swago.baseswago

import android.app.Application
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.huantansheng.easyphotos.ui.dialog.LoadingDialog
import com.swago.baseswago.inter.ApiManager
import com.swago.baseswago.model.*
import com.swago.baseswago.model.mine.LevelModel
import com.swago.baseswago.model.mine.RoomBlack
import com.swago.baseswago.model.mine.RoomBlackItem
import com.swago.baseswago.util.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 *@date 2021/8/28 10:41
 *description:
 */
class UserVm(application: Application) : BaseViewModel(application) {

    val momentListLiveData by lazy {
        MutableLiveData<List<MomentModel>>()
    }

    fun getMomentList() {
        requestData {
            val data = ApiManager.userApi.getMomentList()
            momentListLiveData.value = data
        }
    }

    val userInfoLiveData by lazy {
        MutableLiveData<UserInfoModel>()
    }

    fun getUserInfo() {
        requestData {
            val data = ApiManager.userApi.getUserInfo()
            userInfoLiveData.value = data
        }
    }

    val otherUserInfoLiveData by lazy {
        MutableLiveData<OtherUserInfoModel>()
    }

    fun getOtherUserInfo(uid: String) {
        requestData {
            val data = ApiManager.userApi.getOtherUserInfo(uid)
            otherUserInfoLiveData.value = data
        }
    }

    val modifyInfoLiveData by lazy {
        MutableLiveData<Any>()
    }

    fun modifyInfo(uploadInfo: UploadInfo) {
        requestData {
            val data = ApiManager.userApi.modifyUserInfo(uploadInfo)
            modifyInfoLiveData.value = data
        }
    }


    val followStateLiveData by lazy {
        MutableLiveData<Any>()
    }

    fun followUser(userId: String, type: Int) {
        requestData2 {
            requestData {
                SwagoLoading.showLoadingDialog(ActivityManagerUtil.get().currentActivity())
                val data = ApiManager.userApi.follow(userId, type)
                followStateLiveData.value = data
                SwagoLoading.cancelLoadingDialog()
            }

            requestError {
                SwagoLoading.cancelLoadingDialog()
            }
        }
    }

    val followFansListLiveData by lazy {
        MutableLiveData<BlackListModel>()
    }

    fun getFollowList(page: Int, type: Int) {
        requestData {
            val data = ApiManager.userApi.getList(page, type)
            followFansListLiveData.value = data
        }
    }

    val hasBeenBlackLiveData by lazy {
        MutableLiveData<Any>()
    }

    fun addBlack(userAccount: String, type: Int) {
        requestData {
            val data = ApiManager.userApi.addToBlackList(userAccount, type)
            hasBeenBlackLiveData.value = data
        }
    }


    val blackListLiveData by lazy {
        MutableLiveData<BlackListModel?>()
    }

    fun getBlackList(page: Int) {
        requestData2 {
            requestData {
                val data = ApiManager.userApi.getBackList(page)
                blackListLiveData.value = data
            }

            requestError {
                blackListLiveData.value = null
            }
        }
    }

    /**
     * 举报
     */
    val reportSuccessLiveData by lazy {
        MutableLiveData<Any>()
    }

    fun reportUser(userAccount: String, content: String) {
        requestData2 {
            requestData {
                SwagoLoading.showLoadingDialog(ActivityManagerUtil.get().currentActivity())
                ApiManager.userApi.reportUser(userAccount, content)
                reportSuccessLiveData.value = "success"
                Toast.makeText(
                    AppContext.getContext(),
                    AppContext.getContext().resources.getString(R.string.report_success),
                    Toast.LENGTH_SHORT
                ).show()
                SwagoLoading.cancelLoadingDialog()
            }

            requestError {
                SwagoLoading.cancelLoadingDialog()
            }
        }
    }

    val uploadPicUrlLiveData by lazy {
        MutableLiveData<PicModel>()
    }

    fun uploadFile(file: String) {
        requestData {
            val file = File(file)
            val fileRQ = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
            val requestBody = MultipartBody.Part
                .createFormData("file", file.name, fileRQ)
            val data = ApiManager.userApi.uploadPic(requestBody)
            uploadPicUrlLiveData.value = data
        }
    }

    val logoutLiveData by lazy {
        MutableLiveData<Any>()
    }

    fun logout() {
        requestData2 {
            requestData {
                SwagoLoading.showLoadingDialog(ActivityManagerUtil.get().currentActivity())
                ApiManager.userApi.logout()
                UserInfo.clearUserInfo()
                SpUtil.clearSp()
                logoutLiveData.value = ""
                SwagoLoading.cancelLoadingDialog()
            }
            requestError {
                SwagoLoading.cancelLoadingDialog()
            }
        }
    }

    val levelLiveData by lazy {
        MutableLiveData<ArrayList<LevelModel>>()
    }

    fun getNextLevelExp(levelType: Int) {
        requestData {
            val data = ApiManager.userApi.getLevelList(levelType)
            levelLiveData.value = data
        }
    }


    val roomAnchorBlackListLiveData by lazy {
        MutableLiveData<List<RoomBlack>?>()
    }

    /**
     * 获取房间被拉黑的主播
     */
    fun getAnchorBlackList(page: Int) {
        requestData2 {
            requestData {
                val data = ApiManager.userApi.getAnchorBlackList(page)
                data.list?.let {
                    roomAnchorBlackListLiveData.value = it
                }
            }

            requestError {
                roomAnchorBlackListLiveData.value = null
            }
        }

    }

    val roomBlackListLiveData by lazy {
        MutableLiveData<List<RoomBlackItem>?>()
    }

    /**
     * 直播间黑名单
     */
    fun getRoomBlackList(page: Int) {
        requestData2 {
            requestData {
                val data = ApiManager.userApi.getRoomBlackList(page)
                data.list?.let {
                    roomBlackListLiveData.value = it
                }
            }


            requestError {
                roomBlackListLiveData.value = null
            }
        }
    }


}