package ch.epfl.isochrone.tiledmap;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

public class OSMTileProvider implements TileProvider {
    private String adresseBase;
    private Tile erreurTile;
    
    public OSMTileProvider(String adresseBase) throws IOException
    {
        this.adresseBase=adresseBase;
        this.erreurTile=new Tile(0,0,0,ImageIO.read(getClass().getResource("/images/error-tile.png")));
    }
    
    public OSMTileProvider(URL adresseBase) throws IOException
    {
        this(adresseBase.toString());
    }
    
    @Override
    public Tile tileAt(int zoom, int x, int y) {
        try {
            URL adresse =  new URL(adresseBase + "/" + zoom + "/" + x + "/" + y + ".png");
            BufferedImage image = ImageIO.read(adresse);
            
            return new Tile(x,y,zoom,image);
            
        } catch (MalformedURLException e) {
          
            return erreurTile;
        } catch (IOException e) {
            return erreurTile;
        }  
         
    }

}
