package com.xty.network.model

import android.app.Dialog
import android.view.Gravity
import androidx.lifecycle.MutableLiveData
import com.hjq.toast.ToastUtils
import com.hjq.toast.style.BlackToastStyle

data class RespBody<T>(
    val code: Int,
    var `data`: T,
    val msg: String,
    var paramsData: String? = null
){
    val isSuccess: Boolean
        get() = code == 0

    /**
     * 判断当前状态
     * status  穿 nowData
     */
    fun getCodeStatus(
        liveData: MutableLiveData<RespBody<T>>,
        status: MutableLiveData<String>,
        loading: Dialog? = null,
        paramsData: String?=null
    ) {
        loading?.dismiss()
        when (code) {
            0 -> liveData.postValue(this.apply { this.paramsData=paramsData })
            102 -> liveData.postValue(this)
            403 -> status.postValue("")
            else -> showToast(msg)
        }
    }

    fun getAwaitDate(status: MutableLiveData<String>): T? {
        when (code) {
            0 -> {
                return data
            }
            403 -> {
                status.postValue("")
                return null
            }
            else -> showToast(msg)
        }

        return null
    }


    fun getLoginStatus(status: MutableLiveData<String>, loading: Dialog? = null): T? {
        loading?.dismiss()
        if (code == 0) {
            return data
        } else if (code == 403) {
            status.postValue("")
            return null
        } else {
            showToast(msg)
            return null
        }
    }

    /**
     * 自定义失败吐司
     */
    private fun showToast(toastStr: String) {
        ToastUtils.setStyle(BlackToastStyle())
        ToastUtils.setGravity(Gravity.CENTER)
        ToastUtils.show(toastStr)
    }

    /**
     * 自定义失败吐司
     */
    private fun showToast(toastStr: Int) {
        ToastUtils.setStyle(BlackToastStyle())
        ToastUtils.setGravity(Gravity.CENTER)
        ToastUtils.show(toastStr)
    }
}
