/**
 * 
 * 
 * @author Charles Thiebaut (238935)
 * @author Arnaud Delamare (234841)
 * 
 */

package ch.epfl.isochrone.tiledmap;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import ch.epfl.isochrone.geo.PointOSM;
import ch.epfl.isochrone.geo.PointWGS84;
import ch.epfl.isochrone.timetable.Stop;

public class PathTileProvider implements TileProvider{
    
    private final List<Stop> path;
    private final PointWGS84 arrival;
    private final int time;
    
    public PathTileProvider(List<Stop> path, PointWGS84 arrival, int time)
    {
        this.path = new ArrayList<Stop>(path);
        this.arrival = arrival;
        this.time=time;
    }
    
    @Override
    public Tile tileAt(int zoom, int x, int y) {
        BufferedImage image =
                new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
        
        for (int i=0; i<image.getWidth(); i++)
        {
            for (int j=0; j<image.getWidth(); j++)
            {
                image.setRGB(i, j, 0);  // on rend l'image transparente
            }
        }
        Graphics2D g = image.createGraphics();
        
        Stroke s1 = new BasicStroke((float)1.0);
        Stroke s2 = new BasicStroke((float)4.0);
        
        g.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        int cornerX = new PointOSM(zoom, x * 256, y * 256).roundedX();
        int cornerY = new PointOSM(zoom, x * 256, y * 256).roundedY();
        
        
        for (int i=0; i<path.size()-1; i++)  // tous sauf le dernier
        {
            g.setStroke(s2);
            g.setColor(Color.BLUE);
            g.drawLine(path.get(i).position().toOSM(zoom).roundedX()-cornerX, path.get(i).position().toOSM(zoom).roundedY()-cornerY, path.get(i+1).position().toOSM(zoom).roundedX()-cornerX, path.get(i+1).position().toOSM(zoom).roundedY()-cornerY);
            g.setStroke(s1);
            g.setColor(Color.WHITE);
            g.drawLine(path.get(i).position().toOSM(zoom).roundedX()-cornerX, path.get(i).position().toOSM(zoom).roundedY()-cornerY, path.get(i+1).position().toOSM(zoom).roundedX()-cornerX, path.get(i+1).position().toOSM(zoom).roundedY()-cornerY);
        }
        
        g.setStroke(s2);
        g.setColor(Color.WHITE);
        g.drawLine(path.get(path.size()-1).position().toOSM(zoom).roundedX()-cornerX, path.get(path.size()-1).position().toOSM(zoom).roundedY()-cornerY, arrival.toOSM(zoom).roundedX()-cornerX, arrival.toOSM(zoom).roundedY()-cornerY);
        g.setStroke(s1);
        g.setColor(Color.BLACK);
        g.drawLine(path.get(path.size()-1).position().toOSM(zoom).roundedX()-cornerX, path.get(path.size()-1).position().toOSM(zoom).roundedY()-cornerY, arrival.toOSM(zoom).roundedX()-cornerX, arrival.toOSM(zoom).roundedY()-cornerY);
            
        g.setFont(new Font("Helvetica", Font.BOLD, 16));
        g.setColor(Color.BLACK);
        g.drawString(path.get(path.size()-1).name(), path.get(path.size()-1).position().toOSM(zoom).roundedX()-cornerX, path.get(path.size()-1).position().toOSM(zoom).roundedY()-cornerY);
        
        
        return new Tile(x, y, zoom, image);
    }

    
}
