package com.utopiaindustries.service;

import com.utopiaindustries.dao.uind.LocationSiteDAO;
import com.utopiaindustries.model.uind.LocationSite;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LocationService {

    private final LocationSiteDAO locationSiteDAO;

    public LocationService(LocationSiteDAO locationSiteDAO) {
        this.locationSiteDAO = locationSiteDAO;
    }


    public List<LocationSite> findAll(){
        return locationSiteDAO.findAll();
    }

    /**
     * get site by title
     */
    public List<LocationSite> getSitesByTitle( String title ) {
        try {
            List<LocationSite> locationSites = locationSiteDAO.findAll();
            return locationSites.stream()
                    .filter( ent -> ent.getTitle().toLowerCase().matches( String.format( ".*%s.*", title.toLowerCase() ) ) )
                    .collect( Collectors.toList() );
        } catch ( Exception e ) {
            System.out.println( e );
        }
        return new ArrayList<>();
    }

}
