// Copyright 2001-2003, Danger, Inc.  All Rights Reserved.
// This file is subject to the Danger, Inc. Sample Code License,
// which is provided in the file SAMPLE_CODE_LICENSE.
// Copies are also available from http://developer.danger.com/

package com.danger.livejournal;

import danger.app.*;
import danger.ui.*;
import danger.util.*;

import java.util.Calendar;
import java.util.Comparator;

public class JournalFolderView extends FolderView implements JournalCommands, JournalResources
{
	//*	--------------------	JournalFolderView

	public
	JournalFolderView()
	{
		mApp = LiveJournal.Instance();
		sFontItalic = Font.findItalicSystemFont();
		sFontDate = Font.findFont("HamiltonCond6");

		mDateNow = Calendar.getInstance();
		mDateThen = Calendar.getInstance();

		setHasScrollbar(true);
	}

	//*	--------------------	DrawFolderLine

	public final void
	drawFolderLine(Pen p, Object inEntry, Rect r, boolean inSelected)
	{
	Folder	entry = (Folder) inEntry;
	int		indent = entry.getOpenIcon().getWidth() + COLUMN_GAP;
	int		xoffset = r.left + indent;
	String	title = entry.getTitle();
	int		sz = entry.size();

		if (sz > 0)
			title += " (" + sz + ")";
		else
			title += " (" + mApp.getString(kRsrc_Empty) + ")";

		title = StringUtils.truncate(title, r.right - xoffset, Font.findBoldSystemFont(), StringUtils.TRUNCATE_AT_END);

		p.setFont(Font.findBoldSystemFont());
		
		p.drawText(xoffset, r.top + mAscent, title);

		if (true == entry.isOpen())
			p.drawBitmap(r.left, r.top + 1, entry.getOpenIcon());
		else
			p.drawBitmap(r.left, r.top + 1, entry.getClosedIcon());
	}

	//*	--------------------	DrawItemLine

	public final void
	drawItemLine(Pen p, Object inItem, Rect inDim, boolean inSelected)
	{
	JournalDataRecord	e = (JournalDataRecord) inItem;
	String				s;
	String				dateString;
	Font				font = Font.findSystemFont();
	int					tempWidth;

		s = e.Subject();
		dateString = StringUtils.makeDateString(e.Date());
		p.setFont(sFontDate);

		//*	Draw the date
		tempWidth = sFontDate.getWidth(dateString);
		p.drawText(inDim.right - tempWidth, inDim.top + mAscent, dateString);

		//*	Draw the subject
		if (0 == e.StateFlags())
			p.setFont(font);
		else
			p.setFont(sFontItalic);
			
		s = StringUtils.makeDisplayString(s, (inDim.right - inDim.left) - (10 + 2 + tempWidth), font);
		p.drawText(inDim.left + 10, inDim.top + mAscent, s);

		//*	If it's in the process of being deleted then draw it with strike-through
		if (e.Deleting())
		{
		int	y = (inDim.top + inDim.bottom) / 2;
		
			p.drawLine(inDim.left, y, inDim.right, y);
		}
	}

	//*	--------------------	EventKeyUp

	public boolean
	eventKeyUp(char inChar, Event event)
	{
		switch (inChar)
		{
			case '\r':
			{
				//*	Check to see if it is a folder	
				if (-1 == mCurrentItem)
					toggleFolder(getFolder(mCurrentFolder));
				else
					selectItem(getFolder(mCurrentFolder).getItem(mCurrentItem));
			}
			return true;
		
			case '\b':
			{
			JournalDataRecord	e;
			
				if (-1 == mCurrentItem)
					return true;

				e = (JournalDataRecord) getSelection();

				if (0 == e.StateFlags())
					sendEventToWindow(new Event(CMD_DELETE_ENTRY, 0, 0, getSelection()));
			}
			return true;
		}
		return false;
	}
	
	//*	--------------------	SelectItem
	
	public final void
	selectItem(Object o)
	{
	JournalDataRecord	e = (JournalDataRecord) o;
	
		if (0 == e.StateFlags())
			sendEventToWindow(new Event(kCmd_ReopenEntry, 0, 0, e));
	}

	//*	--------------------	getFolderFromDate
	
	public final Folder
	getFolderFromDate(int inDate)
	{
	Calendar		date = JournalFolder.MakeFolderDate(inDate);
	int				numFolders = mFolders.size();
	JournalFolder	f;
	
		for (int i = 0; i < numFolders; i++)
		{
			f = (JournalFolder) mFolders.elementAt(i);
		
			if (date.equals(f.GetDate()))
				return f;
		}
		
		f = new JournalFolder(inDate);
		f = (JournalFolder) addFolder(f);
		sortFolders(sFolderComparator);
		
		return f;
	}

	//*	--------------------	GetItemByItemID
	
	public final JournalDataRecord
	GetItemByItemID(int inItemID)
	{
	int		numFolders = mFolders.size();
	
		for (int i = 0; i < numFolders; i++)
		{
		Folder	f = (Folder) mFolders.elementAt(i);
		int		numItems = f.size();
	
			for (int j = 0; j < numItems; j++)
			{
			JournalDataRecord	e = (JournalDataRecord) f.getItem(j);
			
				if (e.ItemID() == inItemID)
					return e;
			}
		}
		
		return null;
	}
	
	//*	--------------------------------	Class Variables
	
	static Font			sFontDate;
	static Font			sFontItalic;
	static Comparator	sFolderComparator = new JournalFolderComparator();
	
	//*	--------------------------------	Instance Variables

	private Calendar	mDateNow;
	private Calendar	mDateThen;

	private LiveJournal	mApp;

	//*	--------------------------------	Constants
}

class JournalFolderComparator implements Comparator
{
	//*	--------------------	compare

	public int
	compare(Object inReferenceObject, Object inObject2)
	{
	JournalFolder	a = (JournalFolder) inReferenceObject;
	JournalFolder	b = (JournalFolder) inObject2;
	
		if (a.GetDate().equals(b.GetDate()))
			return 0;

		//*	Note that the result is returned in reverse order.  This is so that newer
		//*	entries will be at the top and Drafts will always be at the bottom.
			
		if (a.GetDate().getTimeInMillis() < b.GetDate().getTimeInMillis())
			return 1;
			
		return -1;
	}
}
