package es.verdnatura.presentation.common

import android.content.Context
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import es.verdnatura.R
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

class FunctionUtils{
    fun getMessageFromJSON(json: String): String {
        val gson = Gson()
        var message = ""
        val tutorialMap: Map<String, Any> =
            gson.fromJson(json, object : TypeToken<Map<String, Any>>() {}.type)
        tutorialMap.forEach {
            if (it.key == "Message") {
                message = it.value.toString()
            }
        }
        return message
    }

    fun getListFromJSON(json: String): List<String> {
        val gson = Gson()
        val list = mutableListOf<String>()
        //  val mapType = object : TypeToken<Map<String, Any>>() {}.type
        val tutorialMap: Map<String, Any> =
            gson.fromJson(json, object : TypeToken<Map<String, Any>>() {}.type)
        tutorialMap.forEach {

            list.add(it.key + ":" + it.value)

        }
        return list
    }

    private fun getCurdate(date: String, context: Context): String {
        val c = Calendar.getInstance()
        val df = SimpleDateFormat(context.getString(R.string.dateCompleteFormat), Locale.getDefault())
        val df2 = SimpleDateFormat(context.getString(R.string.timeFormat), Locale.getDefault())
        c.time = df.parse(date)!!
        return df2.format(c.time)
    }
}