package com.progetto.VEBS.mapper;

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.springframework.stereotype.Component;

import com.progetto.VEBS.model.dto.AreaVerdeDTO;
import com.progetto.VEBS.model.entity.AreaVerde;

@Component
public class AreaVerdeMapperImpl implements AreaVerdeMapper {


	@Override
	public AreaVerdeDTO toDTO(AreaVerde entity) {
		if ( entity == null ) {
            return null;
        }
		AreaVerdeDTO areaVerdeDTO = new AreaVerdeDTO();
		areaVerdeDTO.setId(entity.getId());
		areaVerdeDTO.setNome(entity.getNome());
		areaVerdeDTO.setWKTGeometry(entity.getGeometry().toText());
		areaVerdeDTO.setArea(entity.getArea());
		areaVerdeDTO.setDensita(entity.getDensita());
		areaVerdeDTO.setPoligonoAreaVerde(entity.getPoligonoAreaVerde());
        return areaVerdeDTO;
	}

	@Override
	public AreaVerde toEntity(AreaVerdeDTO dto)  {
		if ( dto == null ) {
            return null;
        }
		
		AreaVerde areaVerde = new AreaVerde();
		
		WKTReader reader = new WKTReader();
		Geometry geom;
		try {
			geom = reader.read(dto.getWKTGeometry());
			areaVerde.setGeometry(geom);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		areaVerde.setId(dto.getId());
		areaVerde.setNome(dto.getNome());
		areaVerde.setArea(dto.getArea());
		areaVerde.setDensita(dto.getDensita());
		areaVerde.setPoligonoAreaVerde(dto.getPoligonoAreaVerde());
        return areaVerde;
	}

}
