package es.verdnatura.presentation.common

import android.os.Build
import androidx.annotation.RequiresApi
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONObject
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

data class itemScanned(var scanned: JSONObject) {
    var table: String = scanned.get("table").toString()
    var id: Int = scanned.get("id").toString().toInt()
    var company: String = scanned.get("company").toString()
    var more: String = scanned.get("more").toString()
    var user: Int = scanned.get("user").toString().toInt()

    @RequiresApi(Build.VERSION_CODES.O)
    var created = DateTimeFormatter.ISO_INSTANT.parse(scanned.get("created").toString())

    /* var created: LocalDateTime = LocalDateTime.parse(
         scanned.get("created").toString(),
         DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
     )*/
    /*  var created: LocalDateTime =
          (scanned.get("created").toString()).toDate().formatTo("yyyy-MM-dd HH:mm")

      )*/
}

//Tarea#5109
@RequiresApi(Build.VERSION_CODES.O)
data class itemScannedAll(var scanned: JSONObject) {
    var company: String = scanned.get("company").toString()
    var user: Int = scanned.get("user").toString().toInt()
    var created: LocalDateTime = LocalDateTime.parse(
        scanned.get("created").toString(),
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    )
    var table: String = scanned.get("table").toString()
    var id: String = scanned.get("id").toString()
    var more: String = scanned.get("more").toString()
}

//Tarea #5109
fun itemScanIsQr(value: String): Boolean {

    return try {
        itemScanned(JSONObject(value))
        return true

    } catch (Ex: Exception) {
        return false
    }

}

fun itemScanValue(value: String, table: String, field: String): Any {
    var item: itemScanned
    return try {

        try {
            item = itemScanned(JSONObject(value))
        } catch (Ex: Exception) {
            return value
        }

        if (item.company == "vnl" && item.table == table) {

            when (field) {
                "id" -> return item.id
                "more" -> return item.more

            }

        }
        throw Exception("QR no válido")


    } catch (Ex: Exception) {
        throw Ex
    }

}

fun itemScanValueOld(value: String, table: String, field: String): Any {
    val gson = Gson()
    var item = itemScanned(JSONObject(value))

    try {
        if (item.company == "vnl" && item.table == table) {

            var valueToReturn: Map<String, Any> =
                gson.fromJson(value, object : TypeToken<Map<String, Any>>() {}.type)
            valueToReturn.forEach {

                if (it.key == field) {
                    // return it.value.toString() /*si no li pose toString torna error double to string*/
                    /*return (it.value as Double).toInt()*/
                    item.more
                }

            }
        }

        throw Exception("QR no válido")

    } catch (Ex: Exception) {

        if (Ex.message!!.contains("QR")) {
            throw Ex
        } else {
            return value
        }
    }

}