package es.verdnatura.presentation.base

import android.content.Context
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.ViewModel
import es.verdnatura.MobileApplication
import es.verdnatura.domain.SalixService
import org.json.JSONObject

abstract class BaseViewModel : ViewModel, LifecycleObserver {
    lateinit var app: MobileApplication
    lateinit var salix: SalixService

    constructor() : super()
    constructor(context: Context) : super() {
        app = context as MobileApplication
        salix = app.salix
    }
}

fun nameofFunction(function: Any): String {
    return try {
        function.javaClass.enclosingMethod!!.name + "->"
    } catch (e: Exception) {
        "ActivityMain->"
    }
}

fun getMessageFromAllResponse(callFunction: String, responseMessage: String): String {

    var messageFromError: String = try {
        val answerError = JSONObject(responseMessage)
        answerError.get("Message").toString()
    } catch (e: Exception) {
        responseMessage
    }

    return "$messageFromError.\r${"Callback: $callFunction."}"

}
