package jp.co.corecorp.s3pdf;

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

import jp.co.corecorp.s3pdf.CardPane03.PaneRec;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

///// pane05
/*
*  "card":{
*  	"P5":{
*		"date":"2012/09/10", "back":false, "memo":"memo"
*  	}
*  }
*/

public class CardPane05 extends CardPane {
	PaneRec paneRec;

	class PaneRec {
		public Date		date = null;
		public int		back = 0;
		public String	memo = null;
	}
	public CardPane05(CardActivity v, SharePDF cdata, S3Application service,
			S3Resource resource) {
		super(v, cdata, service, resource);
		paneRec = new PaneRec();
	}

	private void notifyDataSetChanged() {
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		String v;
		if( paneRec.date != null ) {
			v = df.format(paneRec.date);
		} else {
			v = "";
		}
		Button v_date = (Button)activity.findViewById(R.id.pane05date);
		v_date.setText(v);
		Button v_back = (Button)activity.findViewById(R.id.pane05back);
		v = "";
		switch(paneRec.back) {
		case 1:	v = "要"; break;
		case 2:	v = "不要"; break;
		}
		v_back.setText(v);
		
		if( paneRec.memo == null || paneRec.memo.isEmpty() ) {
			v = "";
		} else {
			v = paneRec.memo;
		}
		EditText v_memo = (EditText)activity.findViewById(R.id.pane05memo);
		v_memo.setText(v);
	}
	
	@Override
	void loadPaneData(JSONObject paneData) {
		if( paneData != null ) {
			try {
				DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
				if( paneData.has("date"))
					paneRec.date	= df.parse(paneData.getString("date"));
				else
					paneRec.date	= null;
				if( paneData.has("back"))
					paneRec.back	= paneData.getInt("back");
				else
					paneRec.back	= 0;
				if( paneData.has("memo"))
					paneRec.memo	= paneData.getString("memo");
				else
					paneRec.memo	= null;
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	JSONObject savePaneData() {
		String text = ((EditText)activity.findViewById(R.id.pane05memo)).getText().toString();
		paneRec.memo	= text;
		if( paneRec.back == 0 &&
			paneRec.date == null &&
			(paneRec.memo == null || paneRec.memo.isEmpty()) ) {
			return null;	
		}
		
		JSONObject paneData = null;
		try {
			DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
			paneData = new JSONObject();
			if( paneRec.date != null )
				paneData.put("date", df.format(paneRec.date));
			if( paneRec.back != 0 )
				paneData.put("back", paneRec.back);
			if( paneRec.memo != null && !paneRec.memo.isEmpty() )
				paneData.put("memo", paneRec.memo);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return paneData;
	}
	
	@Override
	void onSelectDate(Date dv, int pos) {
		paneRec.date = dv;
		notifyDataSetChanged();
	}

 	private View.OnClickListener onDateClick = new View.OnClickListener() {
		public void onClick(View v) {
			Date base = null;
			if( paneRec.date == null ) {
				base = new Date();
			} else {
				base = paneRec.date;
			}
			activity.showDatePicker(v, 0, base);
		}
	}; 
	
	private View.OnLongClickListener onDateLongClick = new View.OnLongClickListener() {
		public boolean onLongClick(View v) {
			if( paneRec.date != null ) {
				paneRec.date = null;
				notifyDataSetChanged();
			}
			return true;
		}
	};
	private View.OnClickListener onBackClick = new View.OnClickListener() {
		public void onClick(View v) {
			switch(paneRec.back) {
			case 0:	paneRec.back = 1; break;
			case 1:	paneRec.back = 2; break;
			case 2:	paneRec.back = 0; break;
			}
			notifyDataSetChanged();
		}
	};
	
	private View.OnFocusChangeListener onMemoFocusChange = new View.OnFocusChangeListener() {
		public void onFocusChange(View v, boolean hasFocus) {
			if( !hasFocus ) {
				String text = ((EditText)v).getText().toString();
				if( text.isEmpty() ) {
					paneRec.memo = null;
				} else {
					paneRec.memo	= text;
				}
			}
		}
	};
	
	@Override
	void initPane() {
		Button v_date = (Button)activity.findViewById(R.id.pane05date);
		v_date.setOnClickListener(onDateClick);
		v_date.setOnLongClickListener(onDateLongClick);
		Button v_back = (Button)activity.findViewById(R.id.pane05back);
		v_back.setOnClickListener(onBackClick);
		notifyDataSetChanged();
	}

	@Override
	void startPane() {
	}

	@Override
	void endPane() {
	}

	@Override
	void showPane() {
		View v = activity.findViewById(R.id.card05pane);
		v.setVisibility(View.VISIBLE);
	}

	@Override
	void hidePane() {
		View v = activity.findViewById(R.id.card05pane);
		v.setVisibility(View.GONE);
	}
}
