package be.iminds.ilabt.jfed.experimenter_gui.timeline.util;

/**
 *
 * @author Cedric
 * 
 *  > Time is calculated in milliseconds, width is calculated in pixels
 *  > Initially every pixel of width will represent a set value of time 
 *  > Zooming in or out will increase or decrease the amount of ms per pixel 
 */
public class TimeCoordinateTranslator {
    private static final TimeCoordinateTranslator instance = new TimeCoordinateTranslator();
    private final double MIN_ZOOM = 4.0;
    private final double MAX_ZOOM = 1024;
    private final double INIT_ZOOM = 16.0;
    
    private double zoom = INIT_ZOOM;
    private final long msPerPixel = 50;
    
    public static TimeCoordinateTranslator getInstance(){
        return instance;
    }

    public double getZoom() {
        return zoom;
    }

    /**
    * Bigger zoom = more zoomed out
    */
    public void setZoom(double zoom) {
        this.zoom = zoom;
    }
    
    public void zoomIn(){
        zoom = (zoom / 2.0 < MIN_ZOOM? MIN_ZOOM : zoom / 2.0);
    }
    
    public void zoomOut(){
        zoom = (zoom * 2.0 > MAX_ZOOM? MAX_ZOOM : zoom * 2.0);
    }
    
    public void resetZoom(){
        zoom = INIT_ZOOM;
    }
    
    public boolean isMinZoom(){
        return zoom == MIN_ZOOM;
    }
    
    public boolean isMaxZoom(){
        return zoom == MAX_ZOOM;
    }
    
    public long translateWidthToTime(double width){
        return (int) (width * (msPerPixel * zoom));
    }
    
    public double translateTimeToWidth(double t){
        return t * 1.0 / (msPerPixel * zoom);
    }
}
