package jp.co.corecorp.s3pdf;

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

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class S3Application {
	private S3Context s3ctx;
	private String serviceName;
	private List<S3Resource> resources;
	private List<TagMap> tagMaps;
	private class TagMap {
		public String tag;
		public List<S3Resource> resources;
		public TagMap(String tag) {
			this.tag = tag;
			this.resources = new ArrayList<S3Resource>();
		}
		public void addResource(S3Resource rsc) {
			resources.add(rsc);
		}
		public int getCount() {
			return resources.size();
		}
		public S3Resource getResource(int index) {
			return resources.get(index);
		}
	}
	public S3Application(S3Context ctx, String svn) {
		s3ctx = ctx;
		serviceName = svn;
		resources = new ArrayList<S3Resource>();
		tagMaps = new ArrayList<S3Application.TagMap>();
	}
	public S3Context getContext() {
		return s3ctx;
	}
	
	public String getServiceName() {
		return serviceName;
	}
	
	public void doSync(Handler handler) throws S3SyncException {
		Bundle startNotifyData = new Bundle();
		startNotifyData.putInt("C", S3Context.START_SERVICE_SYNC);
		Message startNotifyMsg = Message.obtain();
		startNotifyMsg.setData(startNotifyData);
		handler.sendMessage(startNotifyMsg);
		
		int i;
		int n = resources.size();
		for( i = 0; i < n; i++ ) {
			S3Resource r = resources.get(i);
			r.doSync(handler);
		}
		
		Bundle endNotifyData = new Bundle();
		endNotifyData.putInt("C", S3Context.END_SERVICE_SYNC);
		Message endNotifyMsg = Message.obtain();
		endNotifyMsg.setData(endNotifyData);
		handler.sendMessage(endNotifyMsg);
	}
	public void addResource(S3Resource rsc) {
		resources.add(rsc);
		List<String> tagList = rsc.getTags();
		if( tagList != null) {
			int n = tagList.size();
			int i;
			for( i = 0; i < n; i++ ) {
				String tag = tagList.get(i);
				TagMap tmap = findTagMap(tag);
				if( tmap == null ) {
					tmap = new TagMap(tag);
					tagMaps.add(tmap);
				}
				tmap.addResource(rsc);
			}
		}
	}
	private TagMap findTagMap(String tag) {
		if( tagMaps != null ) {
			int n = tagMaps.size();
			int i;
			for( i = 0; i < n; i++ ) {
				TagMap tmap = tagMaps.get(i); 
				if( tmap.tag.equals(tag) ) return tmap;
			}
		}
		return null;
	}
	int getResourceCount() {
		return resources.size();
	}
	int getResourceCount(String tag) {
		TagMap tmap = findTagMap(tag);
		if( tmap == null )
			return 0;
		return tmap.getCount();
	}
	S3Resource getResource(int index) {
		return resources.get(index);
	}
	S3Resource getResource(String tag, int index) {
		TagMap tmap = findTagMap(tag);
		if( tmap == null )
			return null;
		return tmap.resources.get(index);
	}
	S3Resource findResource(String rsn) {
		int i;
		int n = resources.size();
		for( i = 0; i < n; i++ ) {
			S3Resource r = resources.get(i);
			if( r.getResourceName().equals(rsn)) 
				return r;
		}
		return null;
	}
	public List<S3Resource> getResource() {
		return resources;
	}
}
