package com.ejada.hmg.geofence

import com.google.android.gms.location.Geofence
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class GeoZoneModel {
    var GEOF_ID:Int = 0
    var Radius:Int = 0
    var Type:Int = 0
    var ProjectID:Int = 0

    var Description:String? = null
    var DescriptionN:String? = null
    var Latitude:String? = null
    var Longitude:String? = null
    var ImageURL:String? = null
    var IsCity:String? = null

    fun identifier():String{
        return "$GEOF_ID" + "_hmg"
    }

    fun message():String{
        return Description ?: "nil"
    }

    fun listFrom(jsonString: String) : List<GeoZoneModel>{
        val type =  object : TypeToken<List<GeoZoneModel?>?>() {}.getType()
        return Gson().fromJson(jsonString, type)
    }

    fun toGeofence() : Geofence?{
        if (!Latitude.isNullOrEmpty() && !Longitude.isNullOrEmpty() && Radius > 50) {
            val lat = Latitude!!.trim().toDoubleOrNull()
            val long = Longitude!!.trim().toDoubleOrNull()
            val rad = Radius.toFloat()
            if(lat != null && long != null){

                val loiteringDelayMinutes:Int = 2 // in Minutes
                return Geofence.Builder()
                        .setRequestId(identifier())
                        .setCircularRegion(
                                lat,
                                long,
                                rad
                        )
                        .setTransitionTypes(GeofenceTransition.ENTER_EXIT.value)
                        .setNotificationResponsiveness(0)
                        .setLoiteringDelay(loiteringDelayMinutes * 60 * 1000)
                        .setExpirationDuration(Geofence.NEVER_EXPIRE)
                        .build()
            }
        }
        return null
    }

}
