package com.chaoran.entiry;

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

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.chaoran.imp.InputInterface;
import com.activity.chaoran.R;

public class SelfDateField extends RelativeLayout implements InputInterface {
	private SelfEditText et;
	private ImageButton but;
	private Dialog dialog;
	private Calendar calendar;
	private SimpleDateFormat dateFormat;
	private SimpleDateFormat format;
	public String formatString = "yyyy-MM-dd";

	public SelfDateField(final Context context) {
		super(context);
		et = new SelfEditText(context);
		LayoutParams params = new LayoutParams(
				LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		params.leftMargin = 0;
		params.rightMargin = 48;
		params.bottomMargin = 0;
		et.setPadding(0, 0, 0, 0);
		et.setSingleLine(true);
		// et.setLayoutParams(params);
		et.setId(1);
		addView(et, params);
		but = new ImageButton(context);
		// but.setScaleType(ScaleType.FIT_XY);
		LayoutParams butParams = new LayoutParams(
				48, 48);
		but.setPadding(0, 0, 0, 0);
		but.setBackgroundResource(R.drawable.calendar);
		butParams.addRule(RelativeLayout.CENTER_VERTICAL);
		butParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		addView(but, butParams);
		format = new SimpleDateFormat(formatString);
		but.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (dialog == null) {
					calendar = Calendar.getInstance();
					dialog = new DatePickerDialog(context,
							new DatePickerDialog.OnDateSetListener() {
								public void onDateSet(DatePicker view,
										int year, int monthOfYear,
										int dayOfMonth) {
									// TODO Auto-generated method stub
									if (dateFormat == null) {
										dateFormat = new SimpleDateFormat(
												formatString);
									}
									Date date = null;
									try {
										date = format.parse(year + "-"
												+ (monthOfYear + 1) + "-"
												+ dayOfMonth);
										et.setText(dateFormat.format(date));
									} catch (ParseException e) {
										et.setText("error");
									}
								}
							}, calendar.get(Calendar.YEAR), calendar
									.get(Calendar.MONTH), calendar
									.get(Calendar.DAY_OF_MONTH));
				}
				dialog.show();

			}
		});
	}

	@Override
	public void setOnFocusChangeListener(OnFocusChangeListener l) {
		et.setOnFocusChangeListener(l);
	}

	public void setText(CharSequence text) {
		if (dateFormat == null) {
			dateFormat = new SimpleDateFormat(formatString);
		}
		Date date = null;
		try {
			date = format.parse(text.toString());
			et.setText(dateFormat.format(date));
		} catch (ParseException e) {
			et.setText(text);
		}
	}

	public Editable getText() {
		return et.getText();
	}

	public void setTextSize(float size) {
		et.setTextSize(size);
	}

	public void setEnabled(boolean b) {
		et.setEnabled(b);
		but.setEnabled(b);
	}

	public void setTag(Object tag) {
		et.setTag(tag);
	}

	public Object getTag() {
		return et.getTag();
	}

	@Override
	public void setBackgroundDrawable(Drawable gd) {
		et.setBackgroundDrawable(gd);
	}

	/* 接口方法 */
	public String getDefaultValue() {
		return et.defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		et.defaultValue = defaultValue;
	}

	public void setNextFocus(String focus) {
		et.nextFocus = focus;
	}

	public void setFocusInFun(String fun) {
		et.focusInFun = fun;
	}

	public void setFocusOutFun(String fun) {
		et.focusOutFun = fun;
	}

	public void setInputType(int type) {
		et.setInputType(type);
	}

	public boolean getCloseKeyBoard() {
		return et.getCloseKeyBoard();
	}

	public void setCloseKeyBoard(boolean closeKeyBoard) {
		et.setCloseKeyBoard(closeKeyBoard);
	}

	@Override
	public String getNextFocus() {
		return et.getNextFocus();
	}

	@Override
	public String getFocusInFun() {
		return et.getFocusInFun();
	}

	@Override
	public String getFocusOutFun() {
		return et.getFocusOutFun();
	}

	@Override
	public boolean getIsNull() {
		return et.getIsNull();
	}

	@Override
	public void setIsNull(boolean isNull) {
		et.setIsNull(isNull);
	}

	@Override
	public String getLabel() {
		return et.getLabel();
	}

	@Override
	public void setLabel(String label) {
		et.setLabel(label);
	}

	@Override
	public boolean getIsTextChange() {
		return et.getIsTextChange();
	}

	@Override
	public void setIsTextChange(boolean isTextChange) {
		et.setIsTextChange(isTextChange);
	}

	@Override
	public void setTextColor(int color) {
		et.setTextColor(color);
	}

}
