package com.example.db01;

import java.util.zip.Inflater;




import android.os.Bundle;  
import android.app.Activity;  
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  
import android.widget.Toast;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;

public class debug extends Activity 
{  
    
	TextView textView1, textView2;
    Button btn_retour, btn_update, btn_delete, btn_dialog ;  
    
    final String EXTRA_POSITION = "extra_position";
	final String EXTRA_ID = "extra_id";
    
	String id_alarm = "";
	
	DB_alarm db;
	private SQLiteDatabase dataBase;
	
	final Context context = this;
	
	Ringtone ringTone;

    Uri uriAlarm, uriNotification, uriRingtone;
	
    @Override  
    protected void onCreate(Bundle savedInstanceState)
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.debug_view);  
        
        db = new DB_alarm(getApplicationContext());
        
        textView1=(TextView)findViewById(R.id.textView1);  
        textView2=(TextView)findViewById(R.id.textView2);  
        
//        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
//        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
//        r.play();
        
        // http://android-er.blogspot.fr/2015/06/generate-alarm-notification-and.html
        //uriAlarm = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        //uriNotification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        //uriRingtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        
        //ringTone = RingtoneManager.getRingtone(getApplicationContext(), uriAlarm);
        //ringTone.play();
        
        Intent intent = getIntent();
      
        if (intent != null) 
        {
        	textView1.setText(intent.getStringExtra(EXTRA_POSITION) );
        	textView2.setText(intent.getStringExtra(EXTRA_ID) );
        	
        	id_alarm = intent.getStringExtra(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 val9 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_MODIFIEDTIME )); 
            String val10 = mCursor.getString(mCursor.getColumnIndex(DB_alarm.COL_CREATED_AT )); 
            
            
        	
        	
/***************************   POSSIBILITES  ***************************************/
/*			cas 1
 * 
 * db.execSQL("UPDATE DB_TABLE SET YOUR_COLUMN='newValue' WHERE id=6 ");
 */
/*        	cas 2
 * 
        	ContentValues newValues = new ContentValues();
        	newValues.put("YOUR_COLUMN", "newValue");
        	db.update("YOUR_TABLE", newValues, "id=6", null);
*/        	
        	
/*			cas 3
 * 
          	ContentValues newValues = new ContentValues();
			newValues.put("YOUR_COLUMN", "newValue");
			String[] args = new String[]{"user1", "user2"};
			db.update("YOUR_TABLE", newValues, "name=? OR name=?", args);
 */
        	
        }
        
        
        	btn_retour=(Button)findViewById(R.id.btn_retour);  
        	btn_retour.setOnClickListener(new OnClickListener() 
        	{  
        		@Override  
        		public void onClick(View arg0) 
        		{  
                    finish();  
        		}  
        	});  
       
        	
        	btn_update=(Button)findViewById(R.id.btn_update);  
        	btn_update.setOnClickListener(new OnClickListener() 
        	{  
	        		@Override  
	        		public void onClick(View arg0) 
	        		{  
	        			update_alarm();
	        		}  
        	});  
       
            
        	btn_delete=(Button)findViewById(R.id.btn_delete);  
            btn_delete.setOnClickListener(new View.OnClickListener()
            {
	                @Override
	                public void onClick(View v) 
	                {
		                	delete_alarm(v);
/*		                	
		                    AlertDialog.Builder builder = new AlertDialog.Builder( debug.this);
		                    builder.setTitle("Confirmation:");
		                    builder.setMessage("Delete alarm");
		                    //builder.setIcon(R.drawable.delete);
		                    
		                    
		                    builder.setNegativeButton("NO", new DialogInterface.OnClickListener() 
		                    {
		                                public void onClick(DialogInterface dialog, int which)
		                                {
		                                    Toast.makeText(getApplicationContext(),"No is clicked",Toast.LENGTH_LONG).show();
		                                }
		                    });
		                    builder.setPositiveButton("YES", new DialogInterface.OnClickListener() 
		                    {
		                                public void onClick(DialogInterface dialog, int which)
		                                {
		                                    Toast.makeText(getApplicationContext(),"Yes is clicked",Toast.LENGTH_LONG).show();
		                                }
		                    });
		                    
		                    builder.show();
*/		                    
		                    
	                }
            });
            
            
            btn_dialog=(Button)findViewById(R.id.btn_dialog);  
            btn_dialog.setOnClickListener(new OnClickListener() 
        	{  
	        		@Override  
	        		public void onClick(View arg0) 
	        		{  
	        			   //final AlertDialog.Builder b = new AlertDialog.Builder(debug.this);
	        			   //LayoutInflater inflater=getLayoutInflater();
	        			   //View view = LayoutInflater.from(debug.this).inflate(R.layout.dialog, null); 
	        	           //b.setView(view);
	        	           //b.setTitle("Content Management page");
	        	           
	        	           LayoutInflater inflater = getLayoutInflater();
	        	           View alertLayout = inflater.inflate(R.layout.dialog, null);
	        	           
	        	           AlertDialog.Builder alert = new AlertDialog.Builder(debug.this);
	        	           alert.setTitle("Toto");
	        	           alert.setView(alertLayout);
	        	           
	        			//final Dialog dialog = new Dialog(context);
	        			//dialog.setContentView(R.layout.dialog);
	        			//dialog.setTitle("Title...");
	        	           
	        	           
	        	           alert.setPositiveButton("OK", new DialogInterface.OnClickListener() 
	        	           {

	        	               @Override
	        	               public void onClick(DialogInterface dialog, int which)
	        	               {
	        	                  
	        	                   Toast.makeText(getBaseContext(), "plouf ", Toast.LENGTH_SHORT).show();
	        	               }
	        	           });
	        	           
	        	           
	        	           alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
	        	           {

	        	               @Override
	        	               public void onClick(DialogInterface dialog, int which)
	        	               {
	        	                 dialog.cancel();
	        	               }
	        	           });
	        			
	        	          AlertDialog dialog = alert.create();
	        	           dialog.show();
	        	            
	        	           
	        	            
	        				//dialog.show();	
	        	            //b.create().show();
	        		}  
        	});  
            
         
    }  
    
    
    
    private void update_alarm() 
    {
			    	///// mise à jour - update 
			    	String val1 = "zorro";
			    	String val2 = "pipo";
			    	
			    	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() + "'");
			    	requete = "UPDATE table_alarm SET name='" + val1 + "', status ='" + val2 + "' WHERE _id ='" + id_alarm + "'";
			    	
			    	db.update(requete);
    }	
    
    private void delete_alarm(View view) 
    {
    	
    		         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(debug.this);
    	
    		         alertDialogBuilder.setTitle("Delete alarm confirmation");
    		         alertDialogBuilder.setMessage("Are you sure ?");
    		         //alertDialogBuilder.setIcon(R.drawable.tick);
    		       
    		         alertDialogBuilder.setPositiveButton("Yes",new DialogInterface.OnClickListener() 
    		         {
    		                public void onClick(DialogInterface dialog,int item) 
    		                {
    		                    Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_LONG).show();
    		                    db.delete(id_alarm);
    		                }
    		         });
    	
    		         alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() 
    		         {
    		                public void onClick(DialogInterface dialog,int id) 
    		                {
    		                    dialog.cancel();
    		                }
    	
    		         });
    	
    		         AlertDialog alertDialog = alertDialogBuilder.create();
    		         alertDialog.show();
    }
    
    private void insert()
    {
    	//execSQL("INSERT INTO student VALUES('"+editstu_Rollno.getText()+"','"+stu_editName.getText()+ "','"+stu_editMarks.getText()+"');");
    }
    
}  