package com.example.db01;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class edit_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 CheckBox chk1,chk2;
	private Button btn_update_alarm;  
	
	DB_alarm db;
	
	final String EXTRA_POSITION = "extra_position";
	final String EXTRA_ID = "extra_id";
	
	String id_alarm = "";
		
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{		
			super.onCreate(savedInstanceState);
			setContentView(R.layout.edit_alarm_view);
		
			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);
			
			chk1 = (CheckBox)findViewById(R.id.chk_status);
			chk2 = (CheckBox)findViewById(R.id.chk_sound);
			
			Intent intent = getIntent();
	      
	        if (intent != null) 
	        {
		        	id_alarm = intent.getStringExtra(EXTRA_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 )); 
		          
		            // affichage
		            txt_name.setText(val2);
		            
		            if( val6.equals("active") )
		            {
		            	chk1.setChecked(true);
		            }
		            
		            if( val11.equals("1") )
		            {
		            	chk2.setChecked(true);
		            }
		            
		            txt_info.setText(val12); 
	        }	
	        
	        
	        
	        btn_update_alarm =(Button)findViewById(R.id.btn_update_alarm);  
        	btn_update_alarm.setOnClickListener(new OnClickListener() 
        	{  
	        		@Override  
	        		public void onClick(View arg0) 
	        		{  
	        			update_alarm();
	        		}  
        	});  
		
	}
	
	  private void update_alarm() 
	  {
		  			
				    	String name = txt_name.getText().toString();
				    	
				    	String status = "";
				    	String sound = "";
				    	
				    	if(chk1.isChecked())
				    	{
				    			status = "active";
				    	}
				    	else
				    	{
				    			status = "unactive";
				    	}
				    	
				    	if(chk2.isChecked())
				    	{
				    			sound = "1";
				    	}
				    	else
				    	{
				    		    sound = "0";
				    	}
				    	
				    	String info = txt_info.getText().toString(); 
				    	long now = System.currentTimeMillis();
				    	ContentValues contentValues = new ContentValues();
				    	contentValues.put(DB_alarm.COL_NAME, name);
				    	contentValues.put(DB_alarm.COL_STATUS, status);
				    	contentValues.put(DB_alarm.COL_MODIFIEDTIME, now);
				    	contentValues.put(DB_alarm.COL_SOUND, sound);
				    	contentValues.put(DB_alarm.COL_INFO, info);
				    	db.update_alarm(id_alarm, contentValues);
				    	Toast.makeText(getBaseContext(), "Alarm parameters updated ", Toast.LENGTH_SHORT).show();
				    	finish();
				    	
				    	//String requete = "update table_alarm set name = '" + val1 + "'  where _id= '" + id_alarm + "' " ;
				    	//db.update(requete);
				    	
				    	//ContentValues values = new ContentValues();
				        //values.put("title", book.getTitle()); // get title
				        //values.put("author", book.getAuthor()); // get author
				        //int i = db.update(table_alarm, values, DB_alarm.KEY_ID +" = ?", new String[] { String.valueOf(book.getId()) }); //selection args
				    	
				    	//requete = execSQL("UPDATE student SET name='" + stu_editName.getText() + "', status ='" + stu_editMarks.getText() + "' WHERE _id ='" + editstu_Rollno.getText() + "'");
				    	
				    	
				    	//String val1 = "zorro";
				    	//String val2 = "pipo";
				    	//String requete = "UPDATE table_alarm SET name='" + val1 + "', status ='" + val2 + "' WHERE _id ='" + id_alarm + "'";
				    	//
				    	//String requete = "UPDATE table_alarm SET name='" + name + "', status ='" + status + "', sound ='" + sound  +   "', info ='" + info +  "' WHERE _id ='" + id_alarm + "'";
				    	
	    }	
		
}
