package com.example.preferences02;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

//http://abhiandroid.com/ui/baseadapter-tutorial-example.html

public class Listing_alarm_adapter extends BaseAdapter
{
	private Context mContext;
	//list fields to be displayed
	private ArrayList<String> stafid;
	private ArrayList<String> nama;
	private ArrayList<String> jbt;
	

	public Listing_alarm_adapter(Context c, ArrayList<String> stafid, ArrayList<String> nama, ArrayList<String> jbt)
	{
		this.mContext = c;
		//transfer content from database to temporary memory
		this.stafid = stafid;
		this.nama = nama;
		this.jbt = jbt;
	}

	public int getCount() 
	{
		// TODO Auto-generated method stub
		return stafid.size();
	}

	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return null;
	}

	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(int pos, View child, ViewGroup parent)
	{
		Holder mHolder;
		LayoutInflater layoutInflater;
		if (child == null)
		{
			layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			child = layoutInflater.inflate(R.layout.row, null); // listcell
			mHolder = new Holder();
			
			//link to TextView
			//mHolder.txtstafid = (TextView) child.findViewById(R.id.txtstafid);
			//mHolder.txtnama = (TextView) child.findViewById(R.id.txtnama);
			//mHolder.txtjbt = (TextView) child.findViewById(R.id.txtjbt);
			
			mHolder.month_tv = (TextView) child.findViewById(R.id.month_tv);
			mHolder.year_tv = (TextView) child.findViewById(R.id.year_tv);
			mHolder.date_tv = (TextView) child.findViewById(R.id.date_tv);
			mHolder.msg_tv = (TextView) child.findViewById(R.id.msg_tv);
			mHolder.time_tv = (TextView) child.findViewById(R.id.time_tv);
			
			
			child.setTag(mHolder);
			
			// format 
			// nom					jour  mois
			// temps 					  annee	
			
		} 
		else
		{
			mHolder = (Holder) child.getTag();
		}
		
		//transfer to TextView in screen
		//mHolder.txtstafid.setText(stafid.get(pos));
		//mHolder.txtnama.setText(nama.get(pos));
		//mHolder.txtjbt.setText(jbt.get(pos));
		
		String toto = jbt.get(pos);

		long timestamp = Long.parseLong(toto);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(timestamp);
		Date dt = cal.getTime();
		//SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		// OK mais deprecated
		//dt.setTime(number);
		//int aaa = dt.getYear();
		//int bbb = dt.getMonth();
		//int ccc = dt.getDate();
		
		// inutile cal.setTime(dt);
		int jour = cal.get(Calendar.DATE);
		String mois = MainActivity.monthArr[cal.get(Calendar.MONTH) +1 ];
		int annee = cal.get(Calendar.YEAR);

		String option = "";
		
		Boolean option_alarme = Tools.showRemainingTime();
		
		// delai avant l'alarme
		if(option_alarme)
		{
			long now = System.currentTimeMillis();
			String delai = Tools.getRemainingTime(timestamp, now); 
			option = delai;
		}
		
		// heure de l'alarme
		if(!option_alarme)
		{
			//String heure_alarme = Tools.getActualTime(cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
			
			int heure_format24h = cal.get(Calendar.HOUR_OF_DAY);
			int heure_format12h = cal.get(Calendar.HOUR);
			int minute = cal.get(Calendar.MINUTE);
			
			/*  format pour afficher les minutes */
			String str_minute = "";
			if(minute == 0) 
			{
			    str_minute = "00";
			}
			if(minute <= 9)
			{
			    str_minute = "0"  + Integer.toString(minute);
			}
			else
			{
				str_minute =  Integer.toString(minute);
			}
			
			option = heure_format24h +":" + str_minute;
			
		}
/*		
		long now = System.currentTimeMillis();
		String delai = Tools.getRemainingTime(timestamp, now); 
		//String heure_alarme = Tools.getActualTime(dt.getHours(), dt.getMinutes());
		String heure_alarme = Tools.getActualTime(cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE));
		
		int heure_format24h = cal.get(Calendar.HOUR_OF_DAY);
		int heure_format12h = cal.get(Calendar.HOUR);
		int minute = cal.get(Calendar.MINUTE);
		
		
		/*  format pour afficher les minutes */
/*		
		String str_minute = "";
		
		if(minute == 0) 
		{
		    str_minute = "00";
		}
		if(minute <= 9)
		{
		    str_minute = "0"  + Integer.toString(minute);
		}
		else
		{
			str_minute =  Integer.toString(minute);
		}
		
		
		String concatenation = heure_format24h +":" + str_minute + "  -  " + delai;
*/		
		//tv.setText(String.valueOf(dt.getYear()+1900));
		//tv.setText(monthArr[dt.getMonth()+1]);
		//tv.setText(String.valueOf(dt.getDate()));
		
		
		mHolder.month_tv.setText(Integer.toString(jour));
		mHolder.year_tv.setText(mois);
		mHolder.date_tv.setText(Integer.toString(annee));
		
		//mHolder.month_tv.setText(stafid.get(pos));
		//mHolder.year_tv.setText(nama.get(pos));
		//mHolder.date_tv.setText(jbt.get(pos));
		mHolder.msg_tv.setText("message");
		mHolder.time_tv.setText(option);
		
		

		
		return child;
	}

	public class Holder 
	{
		//TextView txtstafid;
		//TextView txtnama;
		//TextView txtjbt;
		
		TextView month_tv;
		TextView year_tv;
		TextView date_tv;
		TextView msg_tv;
		TextView time_tv;
		
	}

}

