package com.example.db01;



import android.net.Uri;
import android.os.Bundle;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class view_alarm extends Activity 
{
	
	private EditText txt_id, txt_name, txt_from_date, txt_to_date, txt_datetime, txt_status, txt_created_time, txt_modified_time, txt_rule, txt_interval, txt_sound, txt_created_at;
	private EditText txt_info;
	
	private ImageButton btncall, btnemail;
	
	String id_alarm = "";
	
	DB_alarm db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
			super.onCreate(savedInstanceState);
			//setContentView(R.layout.view_alarm);
			setContentView(R.layout.essai03);
			
			db = new DB_alarm(getApplicationContext());
			
			txt_id = (EditText)findViewById(R.id.txt_id);
			txt_name = (EditText)findViewById(R.id.txt_name);
			txt_from_date = (EditText)findViewById(R.id.txt_from_date);
			txt_to_date = (EditText)findViewById(R.id.txt_to_date);
			txt_datetime = (EditText)findViewById(R.id.txt_datetime);
			txt_status = (EditText)findViewById(R.id.txt_status);
			txt_created_time = (EditText)findViewById(R.id.txt_created_time);
			txt_modified_time = (EditText)findViewById(R.id.txt_modified_time);
			txt_rule = (EditText)findViewById(R.id.txt_rule);
			txt_interval = (EditText)findViewById(R.id.txt_interval);
			txt_sound = (EditText)findViewById(R.id.txt_sound);
			txt_info = (EditText)findViewById(R.id.txt_info);
			//txt_created_at = (EditText)findViewById(R.id.txt_created_at);
			
		    Intent intent = getIntent();
		      
	        if (intent != null) 
	        {
		        	
		        	id_alarm = intent.getStringExtra(MainActivity.EXTRA_ID);
		        	
		        	// selection d'un élément par son id
		        	String requete = "SELECT * FROM " + DB_alarm.TABLE + " WHERE " + DB_alarm.KEY_ID + "='" + id_alarm +"'";
		            Cursor mCursor = db.DB_query(requete);
		            int val = mCursor.getCount();
		            
		            String val1 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.KEY_ID ));
		            String val2 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_NAME ));
		            String val3 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_FROMDATE )); 
		            String val4 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_TODATE )); 
		            String val5 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_DATETIME ));
		            String val6 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_STATUS )); 
		            String val7 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_CREATEDTIME )); 
		            String val8 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_MODIFIEDTIME )); 
		            String val9 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_RULE )); 
		            String val10 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_INTERVAL )); 
		            String val11 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_SOUND )); 
		            String val12 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_INFO ));
		            String val13 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_CREATED_AT )); // on utilisée
		            
		           
		            Calendar cal_tmp = Calendar.getInstance();
				 	Date dt = new Date();
				 	Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		            
				 	txt_id.setText(val1);
				 	txt_name.setText(val2);
				 	txt_from_date.setText(val3);
				 	txt_to_date.setText(val4);
				 	
					long lg = Long.parseLong(val5);
					dt.setTime(lg);
					String s = formatter.format(dt);
					txt_datetime.setText(s);

					txt_status.setText(val6);
					
					lg = Long.parseLong(val7);
					dt.setTime(lg);
					s = formatter.format(dt);
					txt_created_time.setText(s); 
					
					lg = Long.parseLong(val8);
					dt.setTime(lg);
					s = formatter.format(dt);
					txt_modified_time.setText(s);
					
					txt_rule.setText(val9);
					txt_interval.setText(val10); 
					txt_sound.setText(val11); 
					txt_info.setText(val12); 
					
		          
					//txt_info = (EditText)findViewById(R.id.txt_id);
					//txt_created_at = (EditText)findViewById(R.id.txt_id);
		            
		            
	        }
	       
		
			//handling the call
			//be sure to add <uses-permission android:name="android.permission.CALL_PHONE" /> in manifest
/*			
			btncall=(ImageButton)findViewById(R.id.btncall);
			btncall.setOnClickListener( new OnClickListener()
			{
				public void onClick(View v)
				{
					Intent intent = new Intent(Intent.ACTION_CALL);
					intent.setData(Uri.parse("tel:"+telno ));//provide the phone number
					startActivity(intent);//call
				}
			});
			
			btnemail=(ImageButton)findViewById(R.id.btnemail);
			btnemail.setOnClickListener( new OnClickListener()
			{
				public void onClick(View v)
				{
					Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",email, null));
					emailIntent.putExtra(Intent.EXTRA_SUBJECT, "A query about FSTM");//title
					emailIntent.putExtra(Intent.EXTRA_TEXT, "Salaam, "+name);//message body
					startActivity(Intent.createChooser(emailIntent, "Send email..."));//invoke email client chooser
				}
			});
*/			
	}
	
	/*
	@Override
	protected void onResume() 
	{
		//refresh data for screen is invoked/displayed
		//displayData();
		//super.onResume();
	}*/
	
	//display single record of data from stafid
/*	
	private void displayData() 
	{
		mHelper=new DBHelper(this);
		dataBase = mHelper.getWritableDatabase();
		//the SQL command to fetched all records from the table
		String sql="SELECT * FROM "	+ DBHelper.TABLE_NAME +" WHERE stafid='"+id+"';";
		Cursor mCursor = dataBase.rawQuery(sql, null);
		
		//fetch the record
		if (mCursor.moveToFirst()) {
			//fetch each field and transfer to textbox
			telno=mCursor.getString(mCursor.getColumnIndex(DBHelper.TELEFON));
			email=mCursor.getString(mCursor.getColumnIndex(DBHelper.EMEL));
			name=mCursor.getString(mCursor.getColumnIndex(DBHelper.NAMA));
			//get data from field and transfer to EditText
			txtnama.setText(name);
			txtjbt.setText(mCursor.getString(mCursor.getColumnIndex(DBHelper.JBT)));
			txttelefon.setText(telno);
			txtemel.setText(email);
				
		}
		else
		{
			//do something here if no record fetched from database
			txtnama.setText(sql);
		}
	}//end displayData
*/
}
