/*
 */
package com.teltonikaServer.persistence;

import com.teltonikaServer.network.TCPServer;
import com.teltonikaServer.protocol.CodecIoSection;
import com.teltonikaServer.protocol.GpsElementSection;
import com.teltonikaServer.protocol.Teltonika;
import java.sql.SQLException;
import java.util.logging.Level;

/**
 *
 * @author Americo
 */
public class Asset {

    protected long assetId;
    protected String assetName;
    protected long orgId;
    protected long iotDeviceId;
    protected int gatewayApiMask;
    protected GpsElementSection currentGpsElement;
    protected CodecIoSection currentIO;
    protected String matricula;
    protected String orgName;
   // protected Long currentDriverId = null;
    private int lastMessageType;
    protected long lastIgnitionCounter;
    protected long ignitionCounterOffset;
    protected long lastPositionTimestamp;
    protected Long driverId;
    protected int maxAllowedSpeed;
    protected double mileageOffset;

    public double getMileageOffset() {
        return mileageOffset;
    }

    public double getMileageOffset(int unit) {
        try{
            return getMileageOffset()*(1000/unit);
        } catch(Exception e){}
        return 0;
    }

    public void setMileageOffset(double mileageOffset) {
        this.mileageOffset = mileageOffset;
    }

    public long getAssetId() {
        return assetId;
    }

    public String getAssetName() {
        return assetName;
    }

    public long getOrgId() {
        return orgId;
    }

    public long getIotDeviceId() {
        return iotDeviceId;
    }

    public int getGatewayApiMask() {
        return gatewayApiMask;
    }

    public void setAssetId(long assetId) {
        this.assetId = assetId;
    }

    public void setAssetName(String assetName) {
        this.assetName = assetName;
    }

    public void setOrgId(int orgId) {
        this.orgId = orgId;
    }

    public void setIotDeviceId(long iotDeviceId) {
        this.iotDeviceId = iotDeviceId;
    }

    public void setGatewayApiMask(int gatewayApiMask) {
        this.gatewayApiMask = gatewayApiMask;
    }

    public void setGpsElement(GpsElementSection gpsElement) {
        this.currentGpsElement = gpsElement;
    }

    public void setIoElement(CodecIoSection ioElement) {
        this.currentIO = ioElement;
    }

    public boolean mustReportImm() {
        return (this.gatewayApiMask & TCPServer.IMM_GATEWAY) != 0;
    }

    public void persist() {
        try {

            long ignCount = currentIO.getIgnitionCounter();
            double mileage = currentIO.getOdometer();
            int externalPower = currentIO.getExternalPower();
            int inputs = ((currentIO.getIn1()) ? 1 : 0) + ((currentIO.getIn2()) ? 2 : 0) + ((currentIO.getIn3()) ? 4 : 0);
            int outputs = ((currentIO.getOut1()) ? 1 : 0) + ((currentIO.getOut2()) ? 2 : 0);
            int ad1 = currentIO.getAd1();
            long driverId;
            
            /*
            switch (currentIO.getLastImmobilizerEventId()) {
                case 0:
                    driverId = 0;
                    break;
                case 2:
                    driverId = currentIO.getDriverId();
                    break;
                default:
                    driverId = -1;
                    break;
            }
            */
            
            TCPServer.persistor.updateAssetPosition(currentGpsElement, assetId, currentIO.getIgnitionStatus(), currentIO.getMovementStatus(), ignCount, mileage, externalPower, this.driverId, inputs, outputs, ad1, (this.lastMessageType == Teltonika.IMMOBILIZER_EVENT),this.ignitionCounterOffset);
            if (this.mustReportImm()) {
                TCPServer.persistor.appendIMMPositions(currentGpsElement, this.assetId, this.matricula, this.orgName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TCPServer.serverLog.logEvent(Level.SEVERE, "Error al persistir el Asset");
        }
    }

    String getMatricula() {
        return this.matricula;
    }

    String getOrgName() {
        return this.orgName;
    }

    void setMatricula(String unaMatricula) {
        this.matricula = unaMatricula;
    }

    void setOrgName(String unNombre) {
        this.orgName = unNombre;
    }

    /*
    public long getCurrentDriver() {
        if (this.currentDriverId > 0) {
            return this.currentDriverId;
        } else {
            return this.driverId;
        }
    }
    */
    
    public void setLastMessageType(int messageType) {
        this.lastMessageType = messageType;
    }

    public long getIgnitionCounter() {
        return lastIgnitionCounter;
    }

    public long getIgnitionCounterOffset() {
        return ignitionCounterOffset;
    }

    public void setLastIgnitionCounter(long aLong) {
        this.lastIgnitionCounter = aLong;
    }

    public void setIgnitionCounterOffset(long hourCountOffset) {
        this.ignitionCounterOffset = hourCountOffset;
    }

    public long fitIgnCounterOffsetTo(long ignitionCounter) {
        this.ignitionCounterOffset = this.ignitionCounterOffset + (this.lastIgnitionCounter - ignitionCounter);
        return this.ignitionCounterOffset;
    }

    public boolean updateIgnitionCounterOffset() throws SQLException {
        return TCPServer.persistor.persistIgnitionCounterOffset(this.assetId, this.ignitionCounterOffset);
    }

    public void setDriverId(Long aDriverId) {
        this.driverId = aDriverId;
    }

    public Long getDriverId(){
        return this.driverId;
    }
    
    public long getOffsettedIgnitionCounter() {
        return this.getIgnitionCounterOffset() + this.getIgnitionCounter();
    }

    public long getLastTimestamp() {
        return this.lastPositionTimestamp;
    }

    public void setLastTimestamp(long aTimestamp) {
        this.lastPositionTimestamp = aTimestamp;
    }

    public int getMaxAllowedSpeed() {
        return this.maxAllowedSpeed;
    }

    public void setMaxAllowedSpeed(int anInt){
        this.maxAllowedSpeed = anInt;
    }
}
