/*
 */
package com.teltonikaServer.protocol;

import com.teltonikaServer.lib.Utils;

/**
 *
 * @author Americo
 */
public class GpsElementSection {

    protected double lng;
    protected double lat;
    protected int altitude;
    protected int angle;
    protected int satellites;
    protected int speed;
    protected long utcTimestamp; 

    public boolean parse(byte dm[], long utcTimestamp) {
        boolean parsed = true;
        this.setUtcTimestamp(utcTimestamp);
        try {
            this.setLng(extractLatLng(Utils.getSubArray(dm, 0, 4)));
            this.setLat(extractLatLng(Utils.getSubArray(dm, 4, 4)));
            this.setAltitude((int) Utils.getLongFromMessageArray(Utils.getSubArray(dm, 8, 2)));
            this.setAngle((int) Utils.getLongFromMessageArray(Utils.getSubArray(dm, 10, 2)));
            this.setSatellites((int) Utils.getLongFromMessageArray(Utils.getSubArray(dm, 12, 1)));
            this.setSpeed((int) Utils.getLongFromMessageArray(Utils.getSubArray(dm,13, 2)));
        } catch (Exception anyException) {
            parsed = false;
        }
        return parsed;
    }

    @Override
    public String toString() {
        String toString = "Lng: " + this.getLng() + "\n";
        toString += "Lat: " + this.getLat() + "\n";
        toString += "Alt: " + this.getAltitude() + "\n";
        toString += "Angle: " + this.getAngle() + "\n";
        toString += "Sats: " + this.getSatellites() + "\n";
        toString += "Speed: " + this.getSpeed()+"\n";
        return toString;
    }

    protected double extractLatLng(byte coordinate[]) {

        String hexVal = "";
        for (byte b : coordinate) {
            String hexByte = Utils.getHexByte(Utils.getUnsignedbyte(b));
            //System.out.println("H: "+hexByte);
            hexVal += hexByte;
        }
        
        double coord = Utils.calculateTwosComplement(hexVal);
        return coord;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public int getAltitude() {
        return altitude;
    }

    public void setAltitude(int altitude) {
        this.altitude = altitude;
    }

    public int getAngle() {
        return angle;
    }

    public void setAngle(int angle) {
        this.angle = angle;
    }

    public int getSatellites() {
        return satellites;
    }

    public void setSatellites(int satellites) {
        this.satellites = satellites;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public long getUtcTimestamp() {
        return utcTimestamp;
    }

    public void setUtcTimestamp(long utcTimestamp) {
        this.utcTimestamp = utcTimestamp;
    }
}
