/*Copyright 2011 indiandragon

This file is part of j2me2AB.

J2meAB is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

j2me2AB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with j2me2AB.  If not, see <http://www.gnu.org/licenses/>.
 */

package javax.microedition.lcdui;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.ViewGroup.LayoutParams;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class DateField extends Item {
	public static final int DATE = 1;
	public static final int DATE_TIME = 3;
	public static final int TIME = 2;

	private Date date;
	private int mode;
	private String label;

	private LinearLayout linearLayout;
	private TextView textView;
	private DatePicker datePicker;
	private TimePicker timePicker;

	private Calendar calendar;
	private int Year;
	private int Month;
	private int Day;
	private int Hour;
	private int Minute;

	public DateField(String label, int mode) {

		field(label, mode);
	}

	public DateField(String label, int mode, TimeZone timeZone) {

		field(label, mode);
	}

	public void field(String label, int mode) {
		this.label = label;
		this.mode = mode;

		calendar = Calendar.getInstance();
		Year = calendar.get(Calendar.YEAR);
		Month = calendar.get(Calendar.MONTH);
		Day = calendar.get(Calendar.DAY_OF_MONTH);
		Hour = calendar.get(Calendar.HOUR);
		Minute = calendar.get(Calendar.MINUTE);
		date = calendar.getTime();

		linearLayout = new LinearLayout(UIGlobal.APP_CONTEXT);
		linearLayout.setBackgroundColor(UIGlobal.BACKGROUND);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		textView = new TextView(UIGlobal.APP_CONTEXT);
		textView.setText(label);

		textView.setTypeface(UIGlobal.APP_TYPEFACE, Typeface.BOLD);
		textView.setTextSize(Font.getDefaultFont().getSize());
		textView.setTextColor(UIGlobal.TEXT_NORMAL);
		linearLayout.addView(textView, new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		datePicker = new DatePicker(UIGlobal.APP_CONTEXT);
		datePicker.setCalendarViewShown(false);
		timePicker = new TimePicker(UIGlobal.APP_CONTEXT);
		timePicker.setOnTimeChangedListener(timec);
		datePicker.init(Year, Month, Day, datec);
		switch (mode) {
		case DATE:
			linearLayout.addView(datePicker, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			break;
		case TIME:
			linearLayout.addView(timePicker, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			break;
		case DATE_TIME:
			linearLayout.addView(datePicker, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			linearLayout.addView(timePicker, new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			break;
		default:
			break;
		}
	}

	OnDateChangedListener datec = new OnDateChangedListener() {

		public void onDateChanged(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			Year = year;
			Month = monthOfYear;
			Day = dayOfMonth;
			textView.setText(label +" "+ Year + "/" + (Month + 1) + "/" + Day);
		}
	};

	OnTimeChangedListener timec = new OnTimeChangedListener() {

		public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
			Hour = hourOfDay;
			Minute = minute;
			textView.setText(label +" "+ Hour + ":" + Minute);
		}
	};

	public LinearLayout getView() {

		return linearLayout;
	}

	public Date getDate() {

		calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, Year);
		calendar.set(Calendar.MONTH, Month);
		calendar.set(Calendar.DAY_OF_MONTH, Day);
		calendar.set(Calendar.HOUR, Hour);
		calendar.set(Calendar.MINUTE, Minute);
		date = calendar.getTime();

		return date;
	}

	public void setDate(Date date) {
		this.date = date;

		calendar = Calendar.getInstance();
		calendar.setTimeInMillis(this.date.getTime());
		Year = calendar.get(Calendar.YEAR);
		Month = calendar.get(Calendar.MONTH);
		Day = calendar.get(Calendar.DAY_OF_MONTH);
		Hour = calendar.get(Calendar.HOUR);
		Minute = calendar.get(Calendar.MINUTE);
		// date = calendar.getTime();

	}

	public int getInputMode() {
		return mode;
	}

	public void setInputMode(int mode) {
		this.mode = mode;
	}
}