package com.androidbegin.buttonclicktutorial;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;



import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.Toast;

public class Tools 
{
	
	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 final String DEFAULT_DATE_FORMAT = "yyyy-M-d";
	public static final StringBuilder sb = new StringBuilder();
	public static final SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
	

	public static final String TIME_OPTION = "time_option";
	public static final String DATE_RANGE = "date_range";
	public static final String DATE_FORMAT = "date_format";
	public static final String TIME_FORMAT = "time_format";
	public static final String VIBRATE_PREF = "vibrate_pref";
	public static final String RINGTONE_PREF = "ringtone_pref";
	
	//PreferenceManager.setDefaultValues(this, R.xml.settings, false);
	//sp = PreferenceManager.getDefaultSharedPreferences(this);

	public static long convertir_date_timestamp(String dateInString)
	
    {
    	  SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy HH:mm"); // HH format 24H (0:23) alors que hh sur 12H
   	   	  Date date = null;
   	   	  try
	      {
   	   		  date = sdf.parse(dateInString);
		  } 
	      catch (ParseException e)
	      {
				e.printStackTrace();
		  }
	      
   	   	  return date.getTime();
	}
	
	 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 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;
	}
	
	
	public static String concat(Object... objects)
	{
		sb.setLength(0);
		for (Object obj : objects) 
		{
			sb.append(obj);
		}
		return sb.toString();
	}
	
	public static final String getDateStr(int year, int month, int date) 
	{
		 return concat(year, "-", month, "-", date);
	 }
	 
	 public static final String getTimeStr(int hour, int minute)
	 {
		 return concat(hour, ":", minute>9 ? "":"0", minute);
	 }	 

	 
	 public static boolean showRemainingTime() 
	 {
		  return "1".equals(MainActivity.sp.getString(TIME_OPTION, "0"));
		 //return(true);
	 }
		
	 public static int getDateRange() 
	 {
		return Integer.parseInt(MainActivity.sp.getString(Tools.DATE_RANGE, "0"));
		// return(123);

	 }
		
	 public static String getDateFormat() 
	 {
		//return sp.getString(DATE_FORMAT, DEFAULT_DATE_FORMAT);
		 return("format");
	 }	
		
	 public static boolean is24Hours() 
	 {
		//return sp.getBoolean(TIME_FORMAT, true);
		 return(true);

	 }
		
	 public static boolean isVibrate()
	 {
		//return sp.getBoolean(VIBRATE_PREF, true);
		 return(true);
	 }
		
	 public static String getRingtone() 
	 {
		//return sp.getString(RINGTONE_PREF, android.provider.Settings.System.DEFAULT_NOTIFICATION_URI.toString());
		 return("Allo");
	 }

	 
	 
	 
	 
}
