package com.example.reminder_proj01;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

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

import android.app.Application;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.preference.PreferenceManager;

public class AddAlarm extends Activity
{
	
	private EditText msgEdit;
	private CheckBox soundCb;
	private DatePicker datePicker;
	private TimePicker timePicker;
	private TextView fromdateText;
	private TextView todateText;
	private TextView attimeText;
	TextView textAlarmPrompt; 
	
	private ViewSwitcher vs;
	private RadioGroup rg;
	private RelativeLayout rl3;
	private RelativeLayout rl4;
	
	private Spinner spinner1;
	private Spinner spinner2;
	private Spinner spinner3;
	
	private EditText minsEdit;
	private EditText hoursEdit;
	private EditText daysEdit;
	private EditText monthsEdit;
	private EditText yearsEdit;
	
	private static final int DIALOG_FROMDATE = 1;
	private static final int DIALOG_TODATE = 2;
	private static final int DIALOG_ATTIME = 3;
	
	private SQLiteDatabase db;
	
	Button btntimepicker, btndatepicker;
	
	int hour, min;
    int year, month, day;
	TextView txtdate, txttime;
	SimpleDateFormat format;
	java.sql.Time timeValue;
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setTitle("New Reminder");
        setContentView(R.layout.add);
        findViews();
        db = RemindMe.db;
      
		//db = dbpointer.g  .getWritableDatabase();	
        
       // handler = new DbAdapter(getApplicationContext());
      //  handler.open();
        

        /**********************/
        // ecouteur
        
       // btndatepicker = (Button) findViewById(R.id.btndatepicker);
       // btntimepicker = (Button) findViewById(R.id.btntimepicker);
        
        /**********************/
        /*
        btntimepicker.setOnClickListener(new View.OnClickListener()
        {
	            @Override
	            public void onClick(View v) 
	            {
		                TimePickerDialog td = new TimePickerDialog(AddAlarm.this, new TimePickerDialog.OnTimeSetListener() 
		                {
		                            @Override
		                            public void onTimeSet(TimePicker view, int hourOfDay, int minute)
		                            {
		                                try 
		                                {
		                                    //String dtStart = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);
		                                    //format = new SimpleDateFormat("HH:mm");
		
		                                    //timeValue = new java.sql.Time(format.parse(dtStart).getTime());
		                                    //txttime.setText(String.valueOf(timeValue));
		                                    //String amPm = hourOfDay % 12 + ":" + minute + " " + ((hourOfDay >= 12) ? "PM" : "AM");
		                                    //txttime.setText(amPm + "\n" + String.valueOf(timeValue));
		                                	txttime.setText("plouf");
		                                } 
		                                
		                                catch (Exception ex)
		                                {
		                                    txttime.setText(ex.getMessage().toString());
		                                }
		                            }
		                            
		                },  hour, min,
		                DateFormat.is24HourFormat(AddAlarm.this)
		                );
		                
		                td.show();
	            }
        });*/
        /**********************/
        /*
        btndatepicker.setOnClickListener(new View.OnClickListener()
        {
	            @Override
	            public void onClick(View v)
	            {
		                DatePickerDialog dd = new DatePickerDialog(AddAlarm.this, new DatePickerDialog.OnDateSetListener() 
		                {
		
		                            @Override
		                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
		                            {
		
			                                try 
			                                {
			                                    //SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			                                    //String dateInString = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
			                                    //Date date = formatter.parse(dateInString);
			                                	txtdate.setText("bilou");
			                                	int y = datePicker.getYear();
			                                	int m = datePicker.getMonth(); 
			                                	int d = datePicker.getDayOfMonth(); 
			            						//pickerTime.getCurrentHour(), 
			            						//pickerTime.getCurrentMinute(),
			                                	
			                                	
			                                	Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();
			                                    //txtdate.setText(formatter.format(date).toString());
			                                    	
			                                    //formatter = new SimpleDateFormat("dd/MMM/yyyy");
			                                    //txtdate.setText(txtdate.getText().toString()+"\n"+formatter.format(date).toString());
			                                    //formatter = new SimpleDateFormat("dd-MM-yyyy");
			                                    //txtdate.setText(txtdate.getText().toString()+"\n"+formatter.format(date).toString());
			                                    //formatter = new SimpleDateFormat("dd.MMM.yyyy");
			                                    //txtdate.setText(txtdate.getText().toString()+"\n"+formatter.format(date).toString());
			                                } 
			                                
			                                catch (Exception ex) 
			                                {
			
			                                }
		
		
		                            }
		                }, year, month, day);
		                dd.show();
	            }
        });
        */
        
        
}
        
    public void onClick(View v) 
    {
		switch (v.getId()) 
		{
			case R.id.toggleButton1:
				vs.showNext();
				break;
				
			//case R.id.fromdate_tv:
			//case R.id.fromdate_lb:
			//	showDialog(DIALOG_FROMDATE);
			//	break;
				
			//case R.id.todate_tv:
			//case R.id.todate_lb:
			//	showDialog(DIALOG_TODATE);
			//	break;
				
			//case R.id.attime_tv:
			//case R.id.attime_lb:
			//	showDialog(DIALOG_ATTIME);
			//	break;			
		}
	}
        
    

	private void findViews()
	{
		msgEdit = (EditText) findViewById(R.id.msg_et);
		soundCb = (CheckBox) findViewById(R.id.sound_cb);
		datePicker = (DatePicker) findViewById(R.id.datePicker);
		timePicker = (TimePicker) findViewById(R.id.timePicker);
	    vs = (ViewSwitcher) findViewById(R.id.view_switcher);
	    textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt); 
/*		fromdateText = (TextView) findViewById(R.id.fromdate_tv);
		todateText = (TextView) findViewById(R.id.todate_tv);
		attimeText = (TextView) findViewById(R.id.attime_tv);
        vs = (ViewSwitcher) findViewById(R.id.view_switcher);
        rg = (RadioGroup) findViewById(R.id.radioGroup);
        rl3 = (RelativeLayout) findViewById(R.id.relativeLayout3);
        rl4 = (RelativeLayout) findViewById(R.id.relativeLayout4);
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        spinner3 = (Spinner) findViewById(R.id.spinner3);
        
        minsEdit = (EditText) findViewById(R.id.mins_et);
        hoursEdit = (EditText) findViewById(R.id.hours_et);
        daysEdit = (EditText) findViewById(R.id.days_et);
        monthsEdit = (EditText) findViewById(R.id.months_et);
        yearsEdit = (EditText) findViewById(R.id.years_et);
*/        
	}
	
	public void create(View v) 
	{
		Alarm alarm = new Alarm();
		alarm.setName(msgEdit.getText().toString());
		alarm.setSound(soundCb.isChecked());
		AlarmTime alarmTime = new AlarmTime();
		long alarmId = 0;
		
		switch(vs.getDisplayedChild())
		{
			case 0: //one time
				
				// 2016 9 26
				alarm.setFromDate(getDateStr(datePicker.getYear(), datePicker.getMonth()+1, datePicker.getDayOfMonth()));
				alarmId = alarm.save(db, "name", "fromDate", "toDate", "rule", "interval", false);
				
				
				
				//alarmTime.setAt(getTimeStr(timePicker.getCurrentHour(), timePicker.getCurrentMinute()));
				//alarmTime.setAlarmId(alarmId);
				//alarmTime.save(db);
				break;
				
			case 1: //repeating
				String nada ="";
				break;				
		}
		
		Toast.makeText(getApplicationContext(), "Validation", Toast.LENGTH_LONG).show();
	}
	
	
	/*******************************************/
	
	public String getTimeStr(int hour, int minute) 
	{
		 return concat(hour, ":", minute>9 ? "":"0", minute);
	}	 

	 public String getDateStr(int year, int month, int date) 
	 {
		 return concat(year, "-", month, "-", date);
	 }
	
	 public String concat(Object... objects)
	 {
		StringBuilder sb = new StringBuilder();
		sb.setLength(0);
		for (Object obj : objects) {
			sb.append(obj);
	 }
		return sb.toString();
	}
	

}
