package com.ama.core.architecture.util

import com.ama.core.architecture.R
import com.ama.core.common.exception.RuleException
import com.ama.core.common.util.AppProvider
import net.lucode.hackware.magicindicator.BuildConfig
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import kotlin.toString

fun Throwable.toErrorMessage(): String {

    return when (this) {
        is RuleException -> message.toString()
        else -> {
            val resources = AppProvider.getApplicationContext().resources
            if (BuildConfig.DEBUG) {
                when (this) {
                    is SocketTimeoutException -> resources.getString(R.string.state_error_network_timeout)
                    is UnknownHostException -> resources.getString(R.string.state_error_network_unknown_host)
                    else -> resources.getString(
                        R.string.state_error_network_unknown_exception,
                        message.toString()
                    )
                }
            } else {
                resources.getString(R.string.state_error_hint)
            }
        }
    }
}


