package com.tsi.navi.location.display;

import java.util.concurrent.TimeUnit;

public class DisplayUnits {
    public String formatSpeed(double mps)
    {
        return String.format("%.1f Km/h", toKMH(mps));
    }

    public String formatAltitude(double meters)
    {
        return String.format("%.1f meters", meters);
    }

    public String formatDistance(double meters)
    {
        return String.format("%.1f KM", toKilometers(meters));
    }

    public String formatAccuracy(double meters)
    {
        return String.format("%.1f meters", meters);
    }

    private double toKMH(double mps)
    {
        return mps * 3600.0 / 1000.0;
    }

    private double toKilometers(double meters)
    {
        return 0.001 * meters;
    }
    public String formatBearing(double bearing) {
        String ordinals[] = {
                "north", "northeast", "east", "southeast", "south", "southwest", "west", "northwest"
        };

        int index = (((int) (bearing + 22.5)) % 360) / 45;
        return ordinals[index];
    }

    public String formatElapsedTime(long milliseconds) {
        final long hr = TimeUnit.MILLISECONDS.toHours(milliseconds);
        final long min = TimeUnit.MILLISECONDS.toMinutes(milliseconds - TimeUnit.HOURS.toMillis(hr));
        final long sec = TimeUnit.MILLISECONDS.toSeconds(milliseconds - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
        return String.format("%02d:%02d:%02d", hr, min, sec);
    }
}
