/**
 * @author Twenty Codes
 * @author ricky barrette
 */
package com.TwentyCodes.android.SweetDreamsLib;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

/**
 * creates a time picker preference that saves the hour and minutes preferred as getKey()+"_hour" and getKey()+"_minute"
 * NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
 * @author ricky barrette
 */
public class TimePickerPreference extends Preference implements OnTimeChangedListener{

//	private TimePicker timePicker;
	private final String TAG = "TimePickerPreference";
	private SharedPreferences settings;

	/**
	 * creates a time picker preference that saves the hour and minutes preferred as getKey()+"_hour" and getKey()+"_minute"
	 * NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
	 * @param context
	 */
	public TimePickerPreference(Context context) {
		super(context);
//		timePicker = new TimePicker(getContext());
	}

	/**
	 * creates a time picker preference that saves the hour and minutes preferred as getKey()+"_hour" and getKey()+"_minute"
	 * NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
	 * @param context
	 * @param attrs
	 */
	public TimePickerPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
//		timePicker = new TimePicker(getContext());
	}

	/**
	 * creates a time picker preference that saves the hour and minutes preferred as getKey()+"_hour" and getKey()+"_minute"
	 * NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public TimePickerPreference(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
//		timePicker = new TimePicker(getContext());
	}

	/**
	 * saves the current time selected by the user into the shared_prefs keys of getKey()+"_hour" and getKey()+"_minute"
	 * NOTE: it might be better to save the combined hour and minutes saves as minutes (1 hour + 30 minutes would be saved as 90 minutes)
	 * (non-Javadoc)
	 * @see android.widget.TimePicker.OnTimeChangedListener#onTimeChanged(android.widget.TimePicker, int, int)
	 * @param view
	 * @param hourOfDay
	 * @param minute
	 * @author ricky barrette
	 */
	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
		Log.i(TAG,"onTimeChanged");
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(getKey()+"_hour", hourOfDay);
		editor.putInt(getKey()+"_minute", minute);
		editor.commit();
	}
	
	/**
	 * creates a linear layout the contains only a textview (for title) and a time picker dialog for user input.
	 * (non-Javadoc)
	 * @see android.preference.Preference#onCreateView(android.view.ViewGroup)
	 * @param parent
	 * @return
	 * @author ricky barrette
	 */
	@Override
	 protected View onCreateView(ViewGroup parent){
		Log.i(TAG, "onCreateView");
		
		/*
		 * create a vertical linear layout that width and height that wraps content
		 */
		LinearLayout layout = new LinearLayout(getContext());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.CENTER;
		layout.setPadding(15, 5, 10, 5);
		layout.setOrientation(LinearLayout.VERTICAL);

		layout.removeAllViews();
		
		/*
		 * create a textview that will be used to display the title provided in xml
		 * and add it to the lay out
		 */
		TextView title = new TextView(getContext());
		title.setText(getTitle());
		title.setTextSize(18);
		title.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
		title.setGravity(Gravity.LEFT);
		title.setLayoutParams(params);

		/*
		 * add the time picker to the layout
		 * set the time picker to be 24 hour style
		 * load saved time preference from shared_prefs, then set
		 * onTimeChangedListener
		 */
		TimePicker timePicker = new TimePicker(getContext());
		timePicker.setLayoutParams(params);
		timePicker.setIs24HourView(true);
		settings = getSharedPreferences();
		timePicker.setCurrentHour(settings.getInt(getKey() + "_hour", 0));
		timePicker.setCurrentMinute(settings.getInt(getKey() + "_minute", 5));
		timePicker.setOnTimeChangedListener(this);

		/*
		 * add the title and the time picker views to the layout
		 */
		layout.addView(title);
		layout.addView(timePicker);
		layout.setId(android.R.id.widget_frame);

		return layout;
	}
	 
}
