package eu.reply.lea.services.yms.task.tracking.observer;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import java.time.Instant;

import eu.reply.lea.services.masterdata.reference.LocationReference;
import eu.reply.lea.services.yms.task.tracking.AreaType;
import eu.reply.lea.services.yms.task.tracking.QueueingStatusDTO;
import eu.reply.lea.services.yms.task.tracking.LogisticUnitLocationTrackerService;
import eu.reply.lea.services.yms.util.ObjectMapperUtility;
import eu.reply.sce.backend.core.entity.EntityReference;
import eu.reply.sce.backend.core.event.ModuleEvent;
import eu.reply.sce.backend.core.event.Observer;
import eu.reply.sce.backend.core.jee.log.Logged;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;


@Observer
@ApplicationScoped
@Logged
public class LogisticUnitLocationTrackerObserver {


    @Inject
    private LogisticUnitLocationTrackerService logisticUnitLocationTrackerService;

    @Inject
    private Logger log;

    public void handleUpdateLocationTrackingInfo(
        @Observes @ModuleEvent("elementChangedAreaEvent") final JsonNode event)
        throws JsonProcessingException {
        EntityReference
            logisticUnitRef = ObjectMapperUtility.getMapper().treeToValue(event.path("externalEntityReference"), EntityReference.class);
        if ( logisticUnitRef.getEntityName().equals( "Container" ) || logisticUnitRef.getEntityName().equals( "Trailer" )) {
            log.info("Returning from elementChangedAreaEvent because entity name: {}", logisticUnitRef.getEntityName());
            return;
        }
        Instant
            eventInstant = Instant.parse(ObjectMapperUtility.getMapper().treeToValue(event.path("eventInstant"), String.class));
        String
            areaType = ObjectMapperUtility.getMapper().treeToValue(event.path("areaType"), String.class);
        LocationReference
            currentLocationReference = ObjectMapperUtility.getMapper().treeToValue(event.path("currentLocationReference"), LocationReference.class);
        LocationReference
            executionLocationReference = ObjectMapperUtility.getMapper().treeToValue(event.path("executionLocationReference"), LocationReference.class);
        QueueingStatusDTO queueingStatus =
            new QueueingStatusDTO(executionLocationReference, currentLocationReference, areaTypeConverter(areaType), eventInstant);
        logisticUnitLocationTrackerService.updateLocation(logisticUnitRef, queueingStatus);

    }

    public void updatePositionAfterDequeuedEvent(@Observes @ModuleEvent("updatePositionAfterDequeuedEvent") final JsonNode event)
        throws JsonProcessingException {
        EntityReference
            logisticUnitRef = ObjectMapperUtility.getMapper().treeToValue(event.path("queuedExternalEntityRef"), EntityReference.class);
        if ( logisticUnitRef.getEntityName().equals( "Container" ) || logisticUnitRef.getEntityName().equals( "Trailer" )) {
            log.info("Returning from elementChangedAreaEvent because entity name: {}", logisticUnitRef.getEntityName());
            return;
        }
        logisticUnitLocationTrackerService.commandToUpdateLoadingUnitPosition(logisticUnitRef);
    }

    private AreaType areaTypeConverter(String areaType){
        return AreaType.valueOf(areaType);
    }


}

what do you think about :
        if ( logisticUnitRef.getEntityName().equals( "Container" ) || logisticUnitRef.getEntityName().equals( "Trailer" )) {
            log.info("Returning from elementChangedAreaEvent because entity name: {}", logisticUnitRef.getEntityName());
            return;
        }
with DDD how could i do this differently