package jp.co.corecorp.s3pdf;

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


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

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

///// pane04
/*
*  "card":{
*  	"P4":{
*		"res":true, "memo":"memo"
*  	}
*  }
*/

public class CardPane04 extends CardPane {
	PaneRec paneRec;

	class PaneRec {
		public boolean	res = false;
		public String	memo = null;
	}

	public CardPane04(CardActivity v, SharePDF cdata, S3Application service,
			S3Resource resource) {
		super(v, cdata, service, resource);
		paneRec = new PaneRec();
	}

	private void notifyDataSetChanged() {
		CheckBox v_res = (CheckBox)activity.findViewById(R.id.pane04response);
		v_res.setChecked(paneRec.res);
		EditText v_memo = (EditText)activity.findViewById(R.id.pane04memo);
		String v;
		if( paneRec.memo == null ) {
			v = "";
		} else {
			v = paneRec.memo;
		}
		v_memo.setText(v);
	}
	
	@Override
	void loadPaneData(JSONObject paneData) {
		if( paneData != null ) {
			try {
				if( paneData.has("res"))
					paneRec.res	= paneData.getBoolean("res");
				else
					paneRec.res	= false;
				if( paneData.has("memo")) 
					paneRec.memo = paneData.getString("memo");
				else
					paneRec.memo = null;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	JSONObject savePaneData() {
		CheckBox v_res	= (CheckBox)activity.findViewById(R.id.pane04response);
		paneRec.res		= v_res.isChecked();
		EditText v_memo	= (EditText)activity.findViewById(R.id.pane04memo);
		paneRec.memo	= v_memo.getText().toString();
		
		if( !paneRec.res &&
			(paneRec.memo == null || paneRec.memo.isEmpty()) ){
			return null;
		}
		
		JSONObject paneData = null;
		try {
			paneData = new JSONObject();
			if( paneRec.res )
				paneData.put("res", paneRec.res);
			if( paneRec.memo != null && !paneRec.memo.isEmpty() )
				paneData.put("memo", paneRec.memo);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return paneData;
	}

	@Override
	void initPane() {
		notifyDataSetChanged();
	}

	@Override
	void startPane() {
	}

	@Override
	void endPane() {
	}

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

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