package es.verdnatura.domain

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.text.Html
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale


/*fun Any.toast(context: Context?, duration: Int = Toast.LENGTH_SHORT): Toast {
    return Toast.makeText(context, this.toString(), duration).apply { show() }
}*/
@RequiresApi(Build.VERSION_CODES.N)
fun Any.toast(
    context: Context?,
    duration: Int = Toast.LENGTH_SHORT,
    color: Int = Color.WHITE
): Toast {
    val toast = Toast.makeText(
        context,
        Html.fromHtml("<font color='$color' ><b>$this</b></font>", 0),
        duration
    )
    //toast2.setGravity(Gravity.TOP, 0, 0)
    toast.show()
    return toast.apply { show() }

}

fun <T : Any> T?.notNull(f: (it: T) -> Unit) {
    if (this != null) f(this)
}

fun EditText.toInt(): Int {
    return this.text.toString().toInt()
}
fun EditText.toLong(): Long {
    return this.text.toString().toLong()
}
fun toDateString(date: Date):String{
        val format = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault())
        return format.format(date)
}