package com.futurice.android.reservator.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.util.Log;

public class Helpers {
	/**
	 * Read all from InputStream
	 * @param is Stream to read from
	 * @param size Guess the size of InputStream contents, give negative for the automatic
	 * @return
	 */
	public static String readFromInputStream(InputStream is, int size) {
		try {
			ByteArrayOutputStream os;
			if (size <= 0) {
				os = new ByteArrayOutputStream();
			} else {
				os = new ByteArrayOutputStream(size);
			}
			byte buffer[] = new byte[4096];
			int len;
			while ((len = is.read(buffer)) != -1) {
				os.write(buffer, 0, len);
			}
			return os.toString("UTF-8");
		} catch (IOException e) {
			Log.e("SOAP", "readFromInputStream", e);
			return "";
		}
	}

	// TODO: an hour and a half - and other common expressions
	public static String humanizeTimeSpan(int minutes) {
		if (minutes < 30) {
			return Integer.toString(minutes) + " minutes";
		} else if (minutes < 45) {
			return "half an hour";
		} else if (minutes < 60) {
			return "45 minutes";
		} else if (minutes < 85) {
			return "an hour";
		} else if (minutes < 110) {
			return "an hour and a half";
		} else if (minutes < 24*60) {
			return Integer.toString((minutes + 10)/60) + " hours";
		} else {
			return Integer.toString(minutes/(60*24)) + " days";
		}
	}
	
	// For use in traffic lights
	public static String humanizeTimeSpan2(int minutes) {
		int hours = minutes / 60;
		
		if (minutes < 30) {
			return Integer.toString(roundTo(minutes, 5)) + " minutes";
		} else if (minutes < 60) {
				return Integer.toString(roundTo(minutes, 15)) + " minutes";
		} else if (minutes <= 60*4) {
			return String.format("%dh:%02dmin", hours, roundTo(minutes - hours*60, 15));
		} else if (minutes < 24*60) {
			return String.format("%d hours", hours);
		} else {
			return String.format("%d days", hours/24);
		}
	}
	
	private static int roundTo(int in, int precision) {
		return precision * ((int)Math.round(in / (1.0*precision)));
	}
}

@@ -1554,32 +1554,24 @@
 %09return 
-Integer.toString
+getUnits
 (roundTo
@@ -1582,30 +1582,37 @@
 utes, 5)
-) +
+, %22minute%22,
  %22
- 
 minutes%22
 ;%0A%09%09%7D el
@@ -1595,32 +1595,33 @@
 nute%22, %22minutes%22
+)
 ;%0A%09%09%7D else if (m
@@ -1629,23 +1629,22 @@
 nutes %3C 
-60
+53
 ) %7B%0A
-%09
 %09%09%09retur
@@ -1645,32 +1645,24 @@
 %09return 
-Integer.toString
+getUnits
 (roundTo
@@ -1674,30 +1674,37 @@
 tes, 15)
-) + %22 
+, %22minute%22, %22
 minutes%22
 ;%0A%09%09%7D el
@@ -1687,32 +1687,33 @@
 nute%22, %22minutes%22
+)
 ;%0A%09%09%7D else if (m
@@ -1726,24 +1726,222 @@
  %3C= 60*4) %7B%0A
+%09%09%09int hourMins = roundTo(minutes - hours*60, 15);%0A%09%09%09if (hourMins == 60) %7B%0A%09%09%09%09hours++;%0A%09%09%09%09hourMins = 0;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09if (hourMins == 0) %7B%0A%09%09%09%09return getUnits(hours, %22hour%22, %22hours%22);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09return St
@@ -1978,41 +1978,23 @@
 rs, 
-roundTo(minutes - hours*60, 15));
+hourMins);%0A%09%09%09%7D
 %0A%09%09%7D
@@ -2036,39 +2036,39 @@
 urn 
-String.format(%22%25d hours
+getUnits(hours, %22hour
 %22, 
+%22
 hours
+%22
 );%0A%09
@@ -2091,47 +2091,253 @@
 urn 
-String.format(%22%25d days%22, hours/24);%0A%09%09%7D
+getUnits(hours/24, %22day%22, %22days%22);%0A%09%09%7D%0A%09%7D%0A%09%0A%09private static String getUnits(int amount, String unitName, String unitNamePlural) %7B%0A%09%09if (amount == 1) return String.format(%221 %25s%22, unitName);%0A%09%09return String.format(%22%25d %25s%22, amount, unitNamePlural);
 %0A%09%7D%0A

fix time display