package com.puzzletimer.util;

import java.util.ArrayList;

import com.puzzletimer.models.Solution;

public class SolutionUtils {
    public static String formatSeconds(long time) {
        if (time == Long.MAX_VALUE) {
            return "DNF";
        }

        return String.format(
            "%d.%02d",
            time / 1000,
            (time % 1000) / 10);
    }

    public static String formatMinutes(long time) {
        if (time == Long.MAX_VALUE) {
            return "DNF";
        }

        return String.format(
            "%02d:%02d.%02d",
            time / 60000,
            (time % 60000) / 1000,
            (time % 1000) / 10);
    }

    public static long realTime(Solution solution) {
        if (solution.penalty.equals("DNF")) {
            return Long.MAX_VALUE;
        }

        if (solution.penalty.equals("+2")) {
            return solution.timing.getElapsedTime() + 2000;
        }

        return solution.timing.getElapsedTime();
    }

    public static long[] realTimes(Solution[] solutions, boolean filterDNF) {
        ArrayList<Long> realTimes = new ArrayList<Long>();
        for (int i = 0; i < solutions.length; i++) {
            long actualTime = realTime(solutions[i]);
            if (!filterDNF || actualTime != Long.MAX_VALUE) {
                realTimes.add(actualTime);
            }
        }

        long[] realTimesArray = new long[realTimes.size()];
        for (int i = 0; i < realTimesArray.length; i++) {
            realTimesArray[i] = realTimes.get(i);
        }

        return realTimesArray;
    }
}

@@ -262,38 +262,162 @@
    %7D%0D%0A%0D%0A        
-return
+String sign = %22%22;%0D%0A        if (time %3C 0) %7B%0D%0A            sign = %22-%22;%0D%0A            time = -time;%0D%0A        %7D%0D%0A%0D%0A        return sign +
  String.format(%0D
@@ -634,32 +634,147 @@
 ;%0D%0A        %7D%0D%0A%0D%0A
+        String sign = %22%22;%0D%0A        if (time %3C 0) %7B%0D%0A            sign = %22-%22;%0D%0A            time = -time;%0D%0A        %7D%0D%0A
         return S
@@ -771,16 +771,23 @@
   return
+ sign +
  String.

Support for negative times in format methods