package com.androidbegin.buttonclicktutorial;


import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

//https://sites.google.com/site/androidcheckinfo/android-alarm-example/android-alarm-for-specific-date

public class datepicker extends Activity 
{
	
	 TextView date_infos; 
	 Button valide;
	
	 private int year;
	 private int month;
	 private int day;
	 private int hour;
	 private int min;
	 
	 private DbAdapter dbHelper;
	 private SimpleCursorAdapter dataAdapter;
		
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.datepicker_view);
	
	     
	     //   final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
	        date_infos = (TextView) findViewById(R.id.textView1);
	        
	        final TimePicker TimePicker = (TimePicker) findViewById(R.id.simpleTimePicker);
	        TimePicker.setIs24HourView(true); // to set 24 hours mode
	        
	        final DatePicker DatePicker = (DatePicker) findViewById(R.id.datePicker1);
	        

	        Button btn = (Button) findViewById(R.id.btn);
	        btn.setOnClickListener(new View.OnClickListener() 
	        {
	            @Override
	            public void onClick(View v)
	            {
	                // Initialize a new date picker dialog fragment
	                DialogFragment dFragment = new DatePickerFragment();
	                // Show the date picker dialog fragment
	                dFragment.show(getFragmentManager(), "Date Picker");
	            }
	        });
	        
	        valide = (Button) findViewById(R.id.button1);
	        valide.setOnClickListener(new View.OnClickListener()
	        {
	            @Override
	            public void onClick(View v)
	            {

	            	hour = TimePicker.getCurrentHour();
	            	min = TimePicker.getCurrentMinute();
	            	day = DatePicker.getDayOfMonth();
	            	month = DatePicker.getMonth();
	            	year = DatePicker.getYear();
	            	
	            	date_infos.setText(new StringBuilder().append(hour).append(":").append(min).append("-").append(day).append("/").append(month+1).append("/").append(year));
	            	
	            	Calendar calendar = Calendar.getInstance();
	                calendar.set(Calendar.HOUR_OF_DAY, hour);
	                calendar.set(Calendar.MINUTE, min);
	                calendar.set(Calendar.SECOND, 0);
	                calendar.set(Calendar.DAY_OF_MONTH, day);
	                calendar.set(Calendar.MONTH, month);
	                calendar.set(Calendar.YEAR, year);
	                
	                //int day = 25;
	    			//int month = 9;
	    			//month = month - 1;
	    			//int year = 2016;
	    			//calendar.set(Calendar.DAY_OF_MONTH, day);
	    			//calendar.set(Calendar.MONTH, month);
	    			//calendar.set(Calendar.YEAR, year);
	                
	            	long val = calendar.getTimeInMillis();
	            	
	            	Date aff = calendar.getTime();
	            	
	            	date_infos.setText(
	        				"\n\n***\n"
	        				+ "Alarm is set@ " +  calendar.getTime() + "\n"
	        				+ "***\n");
	            	
//	            	long now = System.currentTimeMillis();
	            	
//	            	long diff = val - now;
	            	
//	                 calendar.set(Calendar.HOUR_OF_DAY, hour );
//	                 calendar.set(Calendar.MINUTE, min);
//	                 calendar.set(Calendar.SECOND, 0);
//	                 val = calendar.getTimeInMillis();
	                 
//	                 val = new GregorianCalendar().getTimeInMillis() + 1 * 60 * 1000;      // 24*60*60*1000;
	                 
	                 
	                 //val = 120000;
	            	
	        		//Alarm alarm = new Alarm();
	        		//alarm.setName(msgEdit.getText().toString());
	        		//alarm.setSound(soundCb.isChecked());
	        		//AlarmTime alarmTime = new AlarmTime();
	        		//long alarmId = 0;
	        		
	           
	    			//alarm.setFromDate(DbHelper.getDateStr(datePicker.getYear(), datePicker.getMonth()+1, datePicker.getDayOfMonth()));
	    			//alarmId = alarm.persist(db);
	    			
	    			//alarmTime.setAt(DbHelper.getTimeStr(timePicker.getCurrentHour(), timePicker.getCurrentMinute()));
	    			//alarmTime.setAlarmId(alarmId);
	    			//alarmTime.persist(db);
	            	
//	            	Intent service = new Intent(MainActivity.this, AlarmService.class);
//	        		service.putExtra("msg", "Salut Patoche");
//	        		service.putExtra("temps", val);
	        		//service.setAction(AlarmService.POPULATE);
//	        		startService(service);
	        		//finish();

	            	
	            	
	            }
	        });
   }


    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState){
            final Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            /*
                Create a DatePickerDialog using Theme.

                    DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
                        int year, int monthOfYear, int dayOfMonth)
             */

            // DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
            DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                    AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,year,month,day);

            // DatePickerDialog THEME_DEVICE_DEFAULT_DARK
            //DatePickerDialog dpd2 = new DatePickerDialog(getActivity(),
            //        AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,year,month,day);

            // DatePickerDialog THEME_HOLO_LIGHT
           // DatePickerDialog dpd3 = new DatePickerDialog(getActivity(),
            //        AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);

            // DatePickerDialog THEME_HOLO_DARK
            //DatePickerDialog dpd4 = new DatePickerDialog(getActivity(),
            //        AlertDialog.THEME_HOLO_DARK,this,year,month,day);

            // DatePickerDialog THEME_TRADITIONAL
            //DatePickerDialog dpd5 = new DatePickerDialog(getActivity(),
            //        AlertDialog.THEME_TRADITIONAL,this,year,month,day);

            // Return the DatePickerDialog
            return  dpd;
        }

        public void onDateSet(DatePicker view, int year, int month, int day)
        {
            // Do something with the chosen date
        	//String dateString = year;
        	
        	String date_selected = String.valueOf(year)+" /"+String.valueOf(month+1)+" /"+String.valueOf(day);
        	date_infos.setText(date_selected);
        	
        	// set selected date into textview
        	//			tvDisplayDate.setText(new StringBuilder().append(month + 1)
        	//			   .append("-").append(day).append("-").append(year)
        	//			   .append(" "));
        	
        	
        	String pipo = "646464";
        }
        
        public void onCancel(DialogInterface dialog){
            // Send a message to confirm cancel button click
            Toast.makeText(getActivity(),"Date Picker Canceled.", Toast.LENGTH_SHORT).show();
        }
        
    }
}