package com.androidbegin.buttonclicktutorial;

import java.text.ParseException;
import java.text.SimpleDateFormat;


import android.content.SharedPreferences;
import android.preference.PreferenceManager;


public class Utils 
{
	
	public static final StringBuilder sb = new StringBuilder();
	
	public static final double MIN = 60 * 1000.0;
	public static final double HOUR = 60 * MIN;
	public static final double DAY = 24 * HOUR;
	public static final double MONTH = 30 * DAY;
	public static final double YEAR = 365 * DAY;
	
	
	 public static String concat(Object... objects)
	 {
		StringBuilder sb = new StringBuilder();
		sb.setLength(0);
		for (Object obj : objects) 
		{
			sb.append(obj);
		}
		return sb.toString();
	}

	public static String getTimeStr(int hour, int minute) 
	{
		 return concat(hour, ":", minute>9 ? "":"0", minute);
	}	 

	 public static String getDateStr(int year, int month, int date) 
	 {
		 return concat(year, "-", month, "-", date);
	 }
	
	 public static String getRemainingTime(long time, long now)
	 {
		 long diff = time - now;
		 if (diff < 0) return null;
		 
		 String txt = "";
		 double div = diff / DAY;
		 if (div >= 1) {
			 txt += ((int)div) + "d ";
			 diff = diff - ((int)div * (long)DAY);
		 }
		 div = diff / HOUR;
		 if (div >= 1) {
			 txt += ((int)div) + "h ";
			 diff = diff - ((int)div * (long)HOUR);
		 }
		 div = diff / MIN;
		 if (div >= 1) {
			 txt += ((int)div) + "m ";
			 diff = diff - ((int)div * (long)MIN);
		 }		 
		 
		 return txt;
	 }
	 
	 public static String getActualTime(int hour, int minute)
	 {
		 if (is24Hours()) return getTimeStr(hour, minute);
		 
		 if (hour < 12) return getTimeStr(hour, minute) + " AM";
		 else if (hour == 12) return getTimeStr(12, minute) + " PM";
		 else return getTimeStr(hour-12, minute) + " PM";
	 }
	
	 public static boolean is24Hours() 
	 {
			return true;
	 }
	 
	 
	 public static String toPersistentTime(String txt) 
	 {
		 if (txt.contains(" AM")) 
		 {	 
			 return txt.replace(" AM", "");
		 }
		 else if (txt.contains(" PM")) 
		 {
			 String[] tokens = txt.replace(" PM", "").split(":");
			 if ("12".equals(tokens[0])) return tokens[0] + ":" + tokens[1];
			 else return (Integer.parseInt(tokens[0])+12) + ":" + tokens[1];
		 } 
		 else return txt;
	 }
	 
	public static String toPersistentDate(String txt, SimpleDateFormat sdf) 
	{
		try 
		{
				return sdf.format(sdf.parse(txt));
		} 
		catch (ParseException e) {}
		return txt;
	}
	
/*****/

	 
}
