package com.adealink.weparty.commonui.toast.util

import android.widget.Toast
import androidx.annotation.StringRes
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.IError
import com.adealink.frame.base.Rlt
import com.adealink.frame.util.AppUtil
import com.adealink.frame.util.runOnUiThread
import com.adealink.weparty.BuildConfig
import com.adealink.weparty.R
import com.adealink.weparty.commonui.toast.ToastCompat

fun showToast(text: CharSequence) {
    showToast(text, 0)
}

fun showToast(text: CharSequence, duration: Int) {
    runOnUiThread {
        if (ToastCompat.isToastEnabled()) {
            ToastCompat.makeText(AppUtil.appContext, text, duration).show()
        } else {
            ToastCompat.show(text, duration)
        }
    }
}

fun showToast(@StringRes resId: Int) {
    showToast(resId, 0)
}

fun showToast(@StringRes resId: Int, duration: Int) {
    runOnUiThread {
        if (ToastCompat.isToastEnabled()) {
            ToastCompat.makeText(AppUtil.appContext, resId, duration).show()
        } else {
            ToastCompat.show(getCompatString(resId), duration)
        }
    }
}

fun showToast(rlt: Rlt<*>?) {
    when (rlt) {
        is Rlt.Success -> {
            showToast(getCompatString(R.string.common_success))
        }

        is Rlt.Failed -> {
            showToast(
                rlt.error,
                getCompatString(
                    R.string.commonui_error_msg,
                    getCompatString(R.string.common_failed),
                    rlt.error.serverCode
                )
            )
        }

        else -> {
            showToast(getCompatString(R.string.common_failed))
        }
    }
}

fun showFailedToast(rlt: Rlt<*>) {
    if (rlt is Rlt.Failed) {
        showToast(rlt.error, getCompatString(R.string.common_failed))
    }
}

fun showToast(error: IError, default: CharSequence? = null) {
    if (error.msg.isNotEmpty()) {
        if (error.serverCode != 0) {
            showToast(getCompatString(R.string.commonui_error_msg, error.msg, error.serverCode))
        } else {
            showToast(error.msg)
        }
        return
    }

    if (error.exception?.message?.isNotBlank() == true) {
        showToast(error.exception!!.message!!)
        return
    }

    if (default.isNullOrEmpty()) {
        return
    }

    showToast(default)
}

fun getFailedMsg(error: IError, default: String? = getCompatString(R.string.common_failed)): String {
    if (error.msg.isNotEmpty()) {
        return if (error.serverCode != 0) {
            getCompatString(R.string.commonui_error_msg, error.msg, error.serverCode)
        } else {
            error.msg
        }
    }

    val exceptionMsg = error.exception?.message
    if (exceptionMsg?.isNotBlank() == true) {
        return exceptionMsg
    }

    if (default.isNullOrEmpty()) {
        return ""
    }

    return default
}

fun debugToast(msg: String) {
    if (BuildConfig.DEBUG) {
        showToast("DEBUG: $msg", Toast.LENGTH_LONG)
    }
}