package com.aplusinformatique.aplusmanager.db;

import androidx.annotation.NonNull;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;

import org.json.JSONException;
import org.json.JSONObject;

@Entity(tableName = "geofence_table", indices = {@Index(value = {"uid"}, unique = true)})
public class Geofence {

    public final static String JSON_PARAM_GEOFENING_ID = "id";
    public final static String JSON_PARAM_GEOFENING_LATITUDE = "latitude";
    public final static String JSON_PARAM_GEOFENING_LONGITUDE = "longitude";
    public final static String JSON_PARAM_GEOFENING_RADIUS = "radius";
    public final static String JSON_PARAM_GEOFENING_WARNING_DELAY = "warning_delay";
    public final static String JSON_PARAM_GEOFENING_CHECK_DELAY = "check_delay";
    public final static String JSON_PARAM_GEOFENING_ROUTING_API = "routing_api";
    /**
     * Used to set an expiration time for a geofence. After this amount of time Location Services
     * stops tracking the geofence.
     */
    private static final long GEOFENCE_EXPIRATION_IN_HOURS = 12;
    /**
     * For this sample, geofences expire after twelve hours.
     */
    static final long GEOFENCE_EXPIRATION_IN_MILLISECONDS =
            GEOFENCE_EXPIRATION_IN_HOURS * 60 * 60 * 1000;
    @PrimaryKey(autoGenerate = true)
    @NonNull
    @ColumnInfo(name = "id")
    public long mId;
    @ColumnInfo()
    public String uid;
    public double latitude;
    public double longitude;
    public float radius;

    @ColumnInfo(name = "warning_delay")
    public long warningDelay;
    @ColumnInfo(name = "check_delay")
    public long checkDelay;
    @ColumnInfo(name = "expiration_delay")
    public long expirationDelay; // in millisecond
    @ColumnInfo(name = "routing_api")
    public String routingApi;
    public boolean hasBeenTriggered;
    public boolean hasToCalculateRouting = true;


    // Avoid empty constructor : parameters are mandatory
    @Ignore
    private Geofence() {
    }

    @Ignore
    public Geofence(String uid, double latitude, double longitude, float radius, long warningDelay, long checkDelay, String routingApi) {
        this(uid, latitude, longitude, radius, warningDelay, checkDelay, GEOFENCE_EXPIRATION_IN_MILLISECONDS, routingApi);
    }

    public Geofence(String uid, double latitude, double longitude, float radius, long warningDelay, long checkDelay, long expirationDelay, String routingApi) {
        this.uid = uid;
        this.latitude = latitude;
        this.longitude = longitude;
        this.radius = radius;
        this.warningDelay = warningDelay;
        this.checkDelay = checkDelay;
        this.expirationDelay = GEOFENCE_EXPIRATION_IN_MILLISECONDS;
        this.routingApi = routingApi;
    }

    public JSONObject getJSONObject() {
        JSONObject geofence = new JSONObject();
        try {
            geofence.put(JSON_PARAM_GEOFENING_ID, uid);
            geofence.put(JSON_PARAM_GEOFENING_LATITUDE, latitude);
            geofence.put(JSON_PARAM_GEOFENING_LONGITUDE, longitude);
            geofence.put(JSON_PARAM_GEOFENING_RADIUS, radius);
            geofence.put(JSON_PARAM_GEOFENING_WARNING_DELAY, warningDelay);
            geofence.put(JSON_PARAM_GEOFENING_CHECK_DELAY, checkDelay);
            geofence.put(JSON_PARAM_GEOFENING_ROUTING_API, routingApi);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return geofence;
    }

    @Override
    public String toString() {
        return "Geofence{" +
                "mId=" + mId +
                ", uid='" + uid + '\'' +
                ", latitude=" + latitude +
                ", longitude=" + longitude +
                ", radius=" + radius +
                ", warningDelay=" + warningDelay +
                ", checkDelay=" + checkDelay +
                ", expirationDelay=" + expirationDelay +
                ", routingApi=" + routingApi +
                '}';
    }
}
