package com.jxml.quick.examples.timesheet;
import java.util.*;

public class Timesheet
{
	public String name="";
	public ArrayList days=new ArrayList();

	protected static String[] projects=Projects.list;
	protected static int projectsLength=projects.length;
	protected static int maxProjectLength;
	protected static String[] paddedProjects=new String[projectsLength];
	protected static String blankProject;
	protected float[] totals;

	static
	{
		int i;
		maxProjectLength=0;
		for (i=0;i<projectsLength;++i)
			if (projects[i].length()>maxProjectLength)
				maxProjectLength=projects[i].length();
		int w;
		int j;
		StringBuffer sb;
		for (i=0;i<projectsLength;++i)
		{
			sb=new StringBuffer();
			sb.append(projects[i]);
			w=maxProjectLength-projects[i].length();
			for (j=0;j<w;++j)
				sb.append(" ");
			paddedProjects[i]=sb.toString();
		}
		sb=new StringBuffer();
		for (j=0;j<maxProjectLength;++j)
			sb.append(" ");
		blankProject=sb.toString();
	}

		public void
	show()
	{
		System.out.println();
		System.out.println();
		System.out.println(heading1());
		System.out.println();
		System.out.println(heading2());
		System.out.println();
		int i;
		int s=days.size();
		totals=new float[s];
		for (i=0;i<s;++i)
			totals[i]=0;
		for (i=0;i<projectsLength;++i)
			System.out.println(detail(i));
		System.out.println();
		System.out.println(summary());
		System.out.println();
		System.out.println();
	}

		protected String
	heading1()
	{
		return blankProject+"          Timesheet for "+name;
	}

		protected String
	heading2()
	{
		StringBuffer sb=new StringBuffer();
		sb.append(blankProject);
		int i;
		int s=days.size();
		for (i=0;i<s;++i)
		{
			Day day=(Day)days.get(i);
			Date date=day.date;
			sb.append("    ");
			sb.append(getMonth(date));
			sb.append("/");
			sb.append(getDay(date));
		}
		sb.append("    total");
		return sb.toString();
	}

		protected String
	detail(int p)
	{
		StringBuffer sb=new StringBuffer();
		sb.append(paddedProjects[p]);
		int i;
		int s=days.size();
		float total=0;
		for (i=0;i<s;++i)
		{
			Day day=(Day)days.get(i);
			Entry entry=day.getEntry(projects[p]);
			float hours=0;
			if (entry!=null)
				hours=entry.hours;
			total+=hours;
			totals[i]+=hours;
			sb.append(right(9,hours));
		}
		sb.append(right(9,total));
		return sb.toString();
	}

		protected String
	summary()
	{
		StringBuffer sb=new StringBuffer();
		sb.append(blankProject);
		int i;
		int s=days.size();
		float total=0;
		for (i=0;i<s;++i)
		{
			sb.append(right(9,totals[i]));
			total+=totals[i];
		}
		sb.append(right(9,total));
		return sb.toString();
	}

		protected String
	getMonth(Date date)
	{
		int mo=date.getMM()+100;
		return (""+mo).substring(1);
	}

		protected String
	getDay(Date date)
	{
		int d=date.getDD()+100;
		return (""+d).substring(1);
	}

		protected String
	right(int w, float v)
	{
		String s=""+v;
		int i;
		int l=w-s.length();
		StringBuffer sb=new StringBuffer();
		for (i=0;i<l;++i)
			sb.append(" ");
		sb.append(s);
		return sb.toString();
	}
}

