/******************************************************************************
 GMessageFrom.cc

	<Description>

	BASE CLASS = <NONE>

	Copyright © 1997 by Glenn W. Bach.  All rights reserved.
	
	Base code generated by Codemill v0.1.0

 *****************************************************************************/

#include <GMessageFrom.h>
#include <GMessageHeader.h>
#include <JString.h>

#include <time.h>
#include <jAssert.h>

/******************************************************************************
 Constructor

 *****************************************************************************/

GMessageFrom::GMessageFrom
	(
	GMessageHeader* header
	)
{
	itsFrom = new JString(header->GetHeader());
	assert(itsFrom != NULL);
	
	itsHeader = header;
	
	itsPosition = header->GetHeaderStart();
}

/******************************************************************************
 Constructor

 *****************************************************************************/

GMessageFrom::GMessageFrom
	(
	const JString& from, 
	const JIndex position
	)
{
	itsFrom = new JString(from);
	assert(itsFrom != NULL);
	
	itsPosition = position;
}


/******************************************************************************
 Destructor

 *****************************************************************************/

GMessageFrom::~GMessageFrom()
{
	delete itsFrom;
}

/******************************************************************************
 CompareSuffixes (static private)

 ******************************************************************************/

JOrderedSetT::CompareResult
GMessageFrom::CompareFromLines
	(
	GMessageFrom* const & n1,
	GMessageFrom* const & n2
	)
{
	int r;

	GMessageHeader* header1 = n1->GetHeader();
	GMessageHeader* header2 = n2->GetHeader();

	r = header1->GetYear() - header2->GetYear();
	if (r == 0)
		{
		if (header1->GetMonth() == header2->GetMonth())
			{
			r = header1->GetDay() - header2->GetDay();
			}
		else
			{
			JString month1 = header1->GetMonth();
			JString month2 = header2->GetMonth();
			JString months = "JanFebMarAprMayJunJulAugSepOctNovDec";
			JIndex val1;
			JIndex val2;
			if (months.LocateSubstring(month1, &val1) && 
				months.LocateSubstring(month2, &val2))
				{
				r = val1 - val2;
				}
			}
		}
		
	if (r == 0)
		{
		r = JStringCompare(header1->GetTime(), header2->GetTime(), kFalse);
		}

	if (r == 0)
		{
		r = JStringCompare(header1->GetSubject(), header2->GetSubject(), kFalse);
		}

	if (r == 0)
		{
		r = JStringCompare(n1->GetFrom(), n2->GetFrom(), kFalse);
		}

	if (r == 0)
		{
		r = (header1->GetMessageEnd() - header1->GetHeaderStart()) -
			(header2->GetMessageEnd() - header2->GetHeaderStart());
		}
	
//	const int r = JStringCompare(n1->GetFrom(), n2->GetFrom(), kFalse);

	if (r > 0)
		{
		return JOrderedSetT::kFirstGreaterSecond;
		}
	else if (r < 0)
		{
		return JOrderedSetT::kFirstLessSecond;
		}
	else
		{
		return JOrderedSetT::kFirstEqualSecond;
		}
}
