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.AppBase
import com.adealink.frame.base.IError
import com.adealink.frame.base.NetworkHttpExecuteError
import com.adealink.frame.base.NetworkHttpResBodyNullError
import com.adealink.frame.base.NetworkHttpStatusNoSuccessError
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, Toast.LENGTH_SHORT)
}

fun showToast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT, style: ToastCompat.Style = ToastCompat.Style.TEXT) {
    runOnUiThread {
        ToastCompat.show(
            AppUtil.appContext,
            text = text.toString(),
            position = ToastCompat.Position.CENTER,
            duration = ToastCompat.Duration.map(duration),
            style = style
        )
    }
}

fun showToast(@StringRes resId: Int) {
    showToast(resId, Toast.LENGTH_SHORT)
}

fun showToast(@StringRes resId: Int, duration: Int, style: ToastCompat.Style = ToastCompat.Style.TEXT) {
    runOnUiThread {
        showToast(getCompatString(resId), duration = duration, style = style)
    }
}

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

        is Rlt.Failed -> {
            showFailedToast(rlt)
        }

        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 = getCompatString(R.string.common_failed)) {
    showToast(getFailedMsg(error, default.toString()))
}

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

    //网络错误
    if (error is NetworkHttpStatusNoSuccessError || error is NetworkHttpExecuteError || error is NetworkHttpResBodyNullError) {
        return if (AppBase.isRelease) {
            getCompatString(R.string.common_net_error)
        } else {
            "${getCompatString(R.string.common_net_error)} ${error.exception?.message}"
        }
    } else {
        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)
    }
}