package de.avatic.lcc.service.transformer.generic;

import de.avatic.lcc.dto.generic.NodeDTO;
import de.avatic.lcc.dto.generic.NodeType;
import de.avatic.lcc.model.nodes.Node;
import de.avatic.lcc.model.premises.route.RouteNode;
import de.avatic.lcc.repositories.country.CountryRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class NodeTransformer {

    private final CountryTransformer countryTransformerService;
    private final CountryRepository countryRepository;
    private final LocationTransformer locationTransformer;

    public NodeTransformer(CountryTransformer countryTransformerService, CountryRepository countryRepository, LocationTransformer locationTransformer) {
        this.countryTransformerService = countryTransformerService;
        this.countryRepository = countryRepository;
        this.locationTransformer = locationTransformer;
    }

    public NodeDTO toNodeDTO(Node entity) {
        NodeDTO dto = new NodeDTO();

        ArrayList<NodeType> types = new ArrayList<>();
        if (entity.getDestination()) types.add(NodeType.DESTINATION);
        if (entity.getSource()) types.add(NodeType.SOURCE);
        if (entity.getIntermediate()) types.add(NodeType.INTERMEDIATE);

        dto.setId(entity.getId());
        dto.setName(entity.getName());
        dto.setAddress(entity.getAddress());
        dto.setCountry(countryTransformerService.toCountryDTO(countryRepository.getById(entity.getCountryId()).orElseThrow()));
        dto.setTypes(types);
        dto.setDeprecated(entity.getDeprecated());
        dto.setLocation(locationTransformer.toLocationDTO(entity));
        dto.setUserNode(false);
        dto.setExternalMappingId(entity.getExternalMappingId());

        return dto;
    }

    public NodeDTO toNodeDTO(RouteNode entity) {
        NodeDTO dto = new NodeDTO();

        ArrayList<NodeType> types = new ArrayList<>();
        if (entity.getDestination()) types.add(NodeType.DESTINATION);
        if (entity.getSource()) types.add(NodeType.SOURCE);
        if (entity.getIntermediate()) types.add(NodeType.INTERMEDIATE);

        dto.setId(entity.getId());
        dto.setName(entity.getName());
        dto.setAddress(entity.getAddress());
        dto.setCountry(countryTransformerService.toCountryDTO(countryRepository.getById(entity.getCountryId()).orElseThrow()));
        dto.setTypes(types);
        dto.setDeprecated(entity.getOutdated());
        dto.setLocation(locationTransformer.toLocationDTO(entity));
        dto.setUserNode(false);
        dto.setExternalMappingId(entity.getExternalMappingId());

        return dto;
    }

}
