package jp.co.corecorp.s3pdf;

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

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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

///// pane01
/*
 *  "card":{
 *  	"P1":{
 *  		"R0":["2012/5/10","","","","","",""],
 *  		"R1":[0,1,0,1,0,0,0],
 *  		"R2":[0,1,0,1,0,0,0],
 *  		"R3":[0,1,0,1,0,0,0],
 *  		"R4":[0,1,0,1,0,0,0],
 *  		"R5":[0,1,0,1,0,0,0],
 *  		"R6":[0,1,0,1,0,0,0],
 *  	}
 *  }
 */

public class CardPane01 extends CardPane {
	private static final int PANE0_ROWTYPE_DATE=0; 
	private static final int PANE0_ROWTYPE_NEED=1; 
	private static final int PANE0_ROWTYPE_CHECK=2; 

	Date[] pane01r0 = null;
	int[] pane01r1 = null;
	int[] pane01r2 = null;
	int[] pane01r3 = null;
	int[] pane01r4 = null;
	int[] pane01r5 = null;
	int[] pane01r6 = null;
	ListAdapter	listAdapter = null;
	
	private class ListAdapter extends BaseAdapter {
		protected LayoutInflater inflater;
		private SharePDF cdata;
		ListAdapter() {
			super();
			this.cdata		= CardPane01.this.cdata;
			this.inflater	=
				(LayoutInflater)cdata.getSystemService(CardActivity.LAYOUT_INFLATER_SERVICE);  
		}
		public long getItemId(int position) {
			return position;
		}
		public int getCount() {
			if( resource == null ) {
				return 0;
			}
			return 7;
		}

		public Object getItem(int position) {
			switch(position) {
			case 0:	return pane01r0;
			case 1:	return pane01r1;
			case 2:	return pane01r2;
			case 3:	return pane01r3;
			case 4:	return pane01r4;
			case 5:	return pane01r5;
			case 6:	return pane01r6;
			}
			return null;
		}

		public View getView(int position, View view, ViewGroup parent) {
			if( view == null ) {
				view = inflater.inflate(R.layout.card_pane01_row, null);  
			}
			int rowType=0;
			Date[] ds = null;
			int[] is = null;
			int id = 0;
			switch(position) {
			case 0:
				rowType = PANE0_ROWTYPE_DATE;
				ds		= pane01r0;
				id		= R.string.card01row0;
				break;
			case 1:
				rowType = PANE0_ROWTYPE_NEED;
				is		= pane01r1;
				id		= R.string.card01row1;
				break;
			case 2:
				rowType = PANE0_ROWTYPE_CHECK;
				is		= pane01r2;
				id		= R.string.card01row2;
				break;
			case 3:
				rowType = PANE0_ROWTYPE_CHECK;
				is		= pane01r3;
				id		= R.string.card01row3;
				break;
			case 4:
				rowType = PANE0_ROWTYPE_CHECK;
				is		= pane01r4;
				id		= R.string.card01row4;
				break;
			case 5:
				rowType = PANE0_ROWTYPE_NEED;
				is		= pane01r5;
				id		= R.string.card01row5;
				break;
			case 6:
				rowType = PANE0_ROWTYPE_CHECK;
				is		= pane01r6;
				id		= R.string.card01row6;
				break;
			}
			
			TextView tv = null;
			tv = (TextView)view.findViewById(R.id.pane01r_item);
			tv.setText(activity.getString(id));
			Button c[] = new Button[7];
			c[0] = (Button)view.findViewById(R.id.pane01r_col1);
			c[1] = (Button)view.findViewById(R.id.pane01r_col2);
			c[2] = (Button)view.findViewById(R.id.pane01r_col3);
			c[3] = (Button)view.findViewById(R.id.pane01r_col4);
			c[4] = (Button)view.findViewById(R.id.pane01r_col5);
			c[5] = (Button)view.findViewById(R.id.pane01r_col6);
			c[6] = (Button)view.findViewById(R.id.pane01r_col7);
			int i;
			if( rowType == PANE0_ROWTYPE_DATE ) {
				DateFormat df = new SimpleDateFormat("MM/dd");
				for(i = 0; i < 7; i++) {
					String v="09/12";
					if( ds != null && ds[i] != null ) {
						v = df.format(ds[i]);
					}
					c[i].setText(v);
					c[i].setOnClickListener(CardPane01.this.onDateItemClick);
				}
			} else {
				String v1;
				String v2;
				View.OnClickListener lsn;
				if( rowType == PANE0_ROWTYPE_NEED ) {
					v1 = activity.getString(R.string.cardSelNeed);
					v2 = activity.getString(R.string.cardSelUnneed);
					lsn = CardPane01.this.onNeedItemClick;
				} else {
					v1 = activity.getString(R.string.cardSelYes);
					v2 = activity.getString(R.string.cardSelNo);
					lsn = CardPane01.this.onCheckItemClick;
				}
				for(i = 0; i < 7; i++) {
					String v=v1;
					if( is != null ) {
						if( is[i] == 1 )
							v = v1;
						else if( is[i] == 2 )
							v = v2;
					}
					c[i].setText(v);
					c[i].setOnClickListener(lsn);
				}
			}
			return view;
		}
	}	
	CardPane01(CardActivity v, SharePDF cdata, S3Application service, S3Resource resource) {
		super(v, cdata, service, resource);
		pane01r0 = null;
		pane01r1 = null;
		pane01r2 = null;
		pane01r3 = null;
		pane01r4 = null;
		pane01r5 = null;
		pane01r6 = null;
		listAdapter = new ListAdapter();
	}

    private View.OnClickListener onDateItemClick = new View.OnClickListener() {
		public void onClick(View v) {
		}
	};
    private View.OnClickListener onNeedItemClick = new View.OnClickListener() {
		public void onClick(View v) {
		}
   	};
    private View.OnClickListener onCheckItemClick = new View.OnClickListener() {
		public void onClick(View v) {
			
		}
   	};
	
	
	int[] loadIntData(JSONArray arr) {
		int[] d = new int[7];
		int n = arr.length();
		int i;
		for( i = 0; i < n && i < 7; i++ ) {
			try {
				d[i] = arr.getInt(i);
			} catch (JSONException e) {
				d[i] = 0;
			}
		}
		for( ; i < 7; i++ ) {
			d[i] = 0;
		}
		return d;
	}
	
	Date[] loadDateData(JSONArray arr) {
		Date[] d = new Date[7];
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		int n = arr.length();
		int i;
		for( i = 0; i < n && i < 7; i++ ) {
			String t;
			try {
				t = arr.getString(i);
				d[i] = df.parse(t);
			} catch (JSONException e) {
				d[i] = null;
			} catch (ParseException e) {
				d[i] = null;
			}
		}
		for( ; i < 7; i++ ) {
			d[i] = null;
		}
		return d;
	}	
	
	@Override
	void loadPaneData(JSONObject paneData) {
		pane01r0 = null;
		pane01r1 = null;
		pane01r2 = null;
		pane01r3 = null;
		pane01r4 = null;
		pane01r5 = null;
		pane01r6 = null;
		if( paneData != null ) {
			JSONArray r = null;
			if( paneData != null ) {
				try {
					r = paneData.getJSONArray("R0");
					pane01r0 = loadDateData(r);
				} catch (JSONException e) {
				}
				try {
					r = paneData.getJSONArray("R1");
					pane01r1 = loadIntData(r);
				} catch (JSONException e) {
				}
				try {
					r = paneData.getJSONArray("R2");
					pane01r2 = loadIntData(r);
				} catch (JSONException e) {
				}
				try {
					r = paneData.getJSONArray("R3");
					pane01r3 = loadIntData(r);
				} catch (JSONException e) {
				}
				try {
					r = paneData.getJSONArray("R4");
					pane01r4 = loadIntData(r);
				} catch (JSONException e) {
				}
				try {
					r = paneData.getJSONArray("R5");
					pane01r5 = loadIntData(r);
				} catch (JSONException e) {
				}
			}
		}
	}
	
	@Override
	JSONObject savePaneData() {
		// TODO 保存コード
		return null;
	}
	
	@Override
	void initPane() {
		ListView v = (ListView)activity.findViewById(R.id.card01list);
		v.setAdapter(listAdapter);
	}

	@Override
	void startPane() {
	}

	@Override
	void endPane() {
	}

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

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