package jp.co.corecorp.s3pdf;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;

abstract class ResourceAdapter extends BaseAdapter {
	public LayoutInflater inflater;
	public SharePDF cdata;
	public String tag;
	public ResourceAdapter(SharePDF cdata, String tag) {
		super();
		this.tag		= tag;
		this.cdata		= cdata;
		this.inflater	=
			(LayoutInflater)cdata.getSystemService(CardActivity.LAYOUT_INFLATER_SERVICE);  
	}
	public long getItemId(int position) {
		return position;
	}
	public int getCount() {
		S3Application svc = getService();
		if( svc == null )
			return 0;
		return svc.getResourceCount(tag);
	}

	public Object getItem(int position) {
		S3Application svc = getService();
		if( svc != null ) {
			int rn = svc.getResourceCount(tag);
			if( 0 <= position && position < rn ) {
				return svc.getResource(tag, position);
			}
		}
		return null;
	}

	S3Application getService() {
		S3ServiceManager sm = cdata.getS3Con().getServiceManager();
		return sm.findService(SharePDF.SharePdfService);
	}
}

abstract class GadgetAdapter extends BaseAdapter {
	protected LayoutInflater inflater;
	protected SharePDF cdata;
	protected S3Resource rsc = null;
	protected String tag = null;
	protected ArrayList<String> gadgetIndex = null;
	GadgetAdapter(SharePDF cdata, String tag) {
		super();
		this.cdata = cdata;
		this.tag = tag;
		this.inflater =
				(LayoutInflater)cdata.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	}
	public void setResource(S3Resource r) {
		rsc	= r;
		notifyDataSetChanged();
	}
	public S3Resource getResource() {
		return rsc;
	}
	
	@Override
	public void notifyDataSetChanged() {
		loadIndex();
		super.notifyDataSetChanged();
	}

	protected void loadIndex() {
		gadgetIndex = null;
		if( rsc != null ) {
			int n = rsc.getGadgetCount(tag);
			ArrayList<String> index = new ArrayList<String>(n);
			int i;
			for( i = 0; i < n; i++ ) {
				String id = rsc.getGadgetId(tag, i);
				index.add(id);
			}
			gadgetIndex = index;
		}
	}
	public long getItemId(int position) {
		return position;
	}
	public int getCount() {
		if( gadgetIndex == null )
			return 0;
		return gadgetIndex.size();
	}

	public Object getItem(int position) {
		if( gadgetIndex == null || position < 0 || gadgetIndex.size() <=  position ) {
			return null;
		}
		return gadgetIndex.get(position);
	}
}

public class SharePDF extends Application {
	public static final String SharePdfService = "SharePDF@core-corp.co.jp"; 
	public static final String SharePdfContentTag = "$$SharePDF";
	public static final String RESOURCE_ID = "rid";
	public static final String RESOURCE_TITLE = "title";
	public static final String RESOURCE_DESC = "description";
	public static final String RESOURCE_SYNCTIME = "lastSyncTime";
	public static final String RESOURCE_MODIFYTIME = "modifyTime";
	public static final String RESOURCE_REMOVETIME = "removeTime";
	public static final String RESOURCE_EXPIRETIME = "expierTime";
	public static final String RESOURCE_TAGS = "tags";
	public static final String RESOURCE_TIMEFMT = "yyyy/MM/dd HH:mm:ss";
	public static final String RESOURCE_IMAGE_ATTACHTAG = "$$Image";

	public static final int	GADGET_EMPTY = 0;
	public static final int	GADGET_INT = 1;
	public static final int	GADGET_FLOAT = 2;
	public static final int	GADGET_DATE = 3;
	public static final int	GADGET_STRING = 4;
	public static final int	GADGET_JSON = 5;
	public static final int	GADGET_FILE = 6;

	public static final String GADGET_ID = "id";
	public static final String GADGET_IDPREXIF = "g";
	public static final String GADGET_SYNCTIME = "sTime";
	public static final String GADGET_MODIFYTIME = "mTime";
	public static final String GADGET_TAGS = "tags";
	public static final String GADGET_VALUETYPE = "vt";
	public static final String GADGET_VALUE = "v";

	private S3Context con;
	
	public SharePDF() {
		super();
	}
	public void setS3Con(S3Context c) {
		con = c;
	}
	public S3Context getS3Con() {
		return con;
	}
}
