/******************************************************************************
 SyGColUtils.cc


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

#include "SyGColUtils.h"

const JCoordinate kUIDWidth 		= 60;
const JCoordinate kGIDWidth 		= 60;
const JCoordinate kSizeWidth 		= 60;
const JCoordinate kModeWidth 		= 90;
const JCoordinate kDateWidth 		= 180;

const JCharacter* kUIDTitleStr		= "User";
const JCharacter* kGIDTitleStr 		= "Group";
const JCharacter* kSizeTitleStr 	= "Size";
const JCharacter* kModeTitleStr 	= "Permissions";
const JCharacter* kDateTitleStr 	= "Date";

const JCoordinate kGFMBufferWidth 	= 6;


/******************************************************************************
 InsertFMCol

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

JBoolean  
InsertFMCol
	(
	JXTable* 			table, 
	JBoolean 			visibleCols[],
	const JIndex 		leadingCols,
	const GFMColType	type,
	JIndex* 			index,
	JCoordinate* 		retWidth
	)
{
	if (GFMTypeVisible(visibleCols, type))
		{
		return kFalse;
		}

	*index = leadingCols + 1;

	if (type == kGFMUID)
		{
		*retWidth = kUIDWidth;
		visibleCols[kGFMUID] = kTrue;
		return kTrue;
		}
		
	if (type >= kGFMGID)
		{
		JBoolean found = kFalse;
		JSize i = 3; 
		
		while(i <= GetFMColCount(visibleCols, leadingCols) && !(found))
			{
			if (GetFMColType(visibleCols, leadingCols, i) > type)
				{
				*index = i;
				found = kTrue;
				}	
			i++;		
			}
			
		if (!(found))
			{
			*index = GetFMColCount(visibleCols, leadingCols)+1;
			}
			
		JCoordinate width;
		
		if (type == kGFMUID)
			{
			*retWidth = kUIDWidth;
			visibleCols[kGFMUID] = kTrue;
			}
			
		else if (type == kGFMGID)
			{
			*retWidth = kGIDWidth;
			visibleCols[kGFMGID] = kTrue;
			}
			
		else if (type == kGFMSize)
			{
			*retWidth = kSizeWidth;
			visibleCols[kGFMSize] = kTrue;
			}
			
		else if (type == kGFMMode)
			{
			*retWidth = kModeWidth;
			visibleCols[kGFMMode] = kTrue;
			}
			
		else if (type == kGFMDate)
			{
			*retWidth = kDateWidth;
			visibleCols[kGFMDate] = kTrue;
			}
		}
		
	return kTrue;
		
	
}

/******************************************************************************
 RemoveFMCol

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

JBoolean  
RemoveFMCol
	(
	JXTable* 			table, 
	JBoolean 			visibleCols[],
	const JIndex 		leadingCols,
	const GFMColType	type,
	JIndex* 			index
	)
{
	JSize i 		= leadingCols + 1;
	JBoolean found 	= kFalse;
	
	while (i <= GetFMColCount(visibleCols, leadingCols) && !(found))
		{
		if (type == GetFMColType(visibleCols, leadingCols, i))
			{
			*index = i;
			found = kTrue;
			}
		i++;
		}
		
	if (found)
		{
		
		if (type == kGFMUID)
			{
			visibleCols[kGFMUID] = kFalse;
			}
			
		else if (type == kGFMGID)
			{
			visibleCols[kGFMGID] = kFalse;
			}
			
		else if (type == kGFMSize)
			{
			visibleCols[kGFMSize] = kFalse;
			}
			
		else if (type == kGFMMode)
			{
			visibleCols[kGFMMode] = kFalse;
			}
			
		else if (type == kGFMDate)
			{
			visibleCols[kGFMDate] = kFalse;
			}
		
		return kTrue;
		}
		
	return kFalse;
}

/******************************************************************************
 GetFMColCount

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

JSize 
GetFMColCount
	(
	const JBoolean 	 visibleCols[],
	const JIndex 	leadingCols
	) 
{
	JSize temp = leadingCols;
	
	if (visibleCols[kGFMUID])
		{
		temp++;
		}
		
	if (visibleCols[kGFMGID])
		{
		temp++;
		}
	
	if (visibleCols[kGFMSize])
		{
		temp++;
		}
		
	if (visibleCols[kGFMMode])
		{
		temp++;
		}

	if (visibleCols[kGFMDate])
		{
		temp++;
		}
		
	return temp;
}

/******************************************************************************
 GetFMColWidth

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

JSize 
GetFMColWidth
	(
	const JBoolean	visibleCols[],
	const JIndex 	leadingCols,
	const JIndex 	index
	) 
{
	GFMColType type = GetFMColType(visibleCols, leadingCols, index);
	
	if (type == kGFMUID)
		{
		return kUIDWidth;
		}
		
	else if (type == kGFMGID)
		{
		return kGIDWidth;
		}
		
	else if (type == kGFMSize)
		{
		return kSizeWidth;
		}

	else if (type == kGFMMode)
		{
		return kModeWidth;
		}
		
	else if (type == kGFMDate)
		{
		return kDateWidth;
		}
	
	return 0;
		
}

/******************************************************************************
 SetFMCols

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

void 
SetFMCols
	(
	JBoolean kShowUID, 
	JBoolean kShowGID, 
	JBoolean kShowSize, 
	JBoolean kShowMode, 
	JBoolean kShowDate
	)
{

}

/******************************************************************************
 GetFMCols

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

void 
GetFMCols
	(
	JBoolean* kShowUID, 
	JBoolean* kShowGID, 
	JBoolean* kShowSize, 
	JBoolean* kShowMode, 
	JBoolean* kShowDate
	) 
{

}

/******************************************************************************
 GetFMColType

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

GFMColType 
GetFMColType
	(
	const JBoolean 	visibleCols[],
	const JIndex 	leadingCols,
	const JIndex 	index
	)
{
	JIndex temp = leadingCols;
	
	if (visibleCols[kGFMUID] == kTrue)
		{
		temp++;
		if (temp == index)
			{
			return kGFMUID;
			}
		}
		
	if (visibleCols[kGFMGID] == kTrue)
		{
		temp++;
		if (temp == index)
			{
			return kGFMGID;
			}
		}
	
	if (visibleCols[kGFMSize] == kTrue)
		{
		temp++;
		if (temp == index)
			{
			return kGFMSize;
			}
		}
		
	if (visibleCols[kGFMMode] == kTrue)
		{
		temp++;
		if (temp == index)
			{
			return kGFMMode;
			}
		}

	if (visibleCols[kGFMDate] == kTrue)
		{
		temp++;
		if (temp == index)
			{
			return kGFMDate;
			}
		}
		
	return kGFMNone;
}

/******************************************************************************
 GFMTypeVisible

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

JBoolean 
GFMTypeVisible
	(
	const JBoolean 	 visibleCols[],
	const GFMColType type
	)
{
	if (type == kGFMUID)
		{
		if (visibleCols[kGFMUID] == kTrue)
			{
			return kTrue;
			}
		}
		
	else if (type == kGFMGID)
		{
		if (visibleCols[kGFMGID] == kTrue)
			{
			return kTrue;
			}
		}
		
	else if (type == kGFMSize)
		{
		if (visibleCols[kGFMSize] == kTrue)
			{
			return kTrue;
			}
		}

	else if (type == kGFMMode)
		{
		if (visibleCols[kGFMMode] == kTrue)
			{
			return kTrue;
			}
		}
		
	else if (type == kGFMDate)
		{
		if (visibleCols[kGFMDate] == kTrue)
			{
			return kTrue;
			}
		}

	return kFalse;
}

/******************************************************************************
 GetFMColTitle

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

void
GetFMColTitle
	(
	const JBoolean	visibleCols[],
	const JIndex 	leadingCols,
	const JIndex 	index,
	JString* 	 	str
	)
{
	GFMColType type = GetFMColType(visibleCols, leadingCols, index);
	
	if (type == kGFMUID)
		{
		*str =  kUIDTitleStr;
		}
		
	else if (type == kGFMGID)
		{
		*str =  kGIDTitleStr;
		}
		
	else if (type == kGFMSize)
		{
		*str =  kSizeTitleStr;
		}

	else if (type == kGFMMode)
		{
		*str =  kModeTitleStr;
		}
		
	else if (type == kGFMDate)
		{
		*str =  kDateTitleStr;
		}
		
		
}

/******************************************************************************
 GetFMBufferWidth

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

JCoordinate 
GetFMBufferWidth
	(
	const JBoolean	visibleCols[],
	const JIndex 	leadingCols,
	const JIndex 	index
	) 
{
	GFMColType type = GetFMColType(visibleCols, leadingCols, index);
	
	if (type == kGFMSize)
		{
		return (-kGFMBufferWidth);
		}
		
	return kGFMBufferWidth;
}



/******************************************************************************
 GetRealColType

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

GFMColType 
GetRealColType
	(
	const JBoolean	visibleCols[],
	const JIndex 	leadingCols,
	const JIndex 	index, 
	JIndex* 		subIndex
	) 
{
	GFMColType type = GetFMColType(visibleCols, leadingCols, index);

	if (type <= kGFMMode)
		{
		*subIndex = 1;
		return type;
		}
		
	else if (type == kGFMDate && !(GFMTypeVisible(visibleCols, kGFMMode)))
		{
		*subIndex = 1;
		return type;
		}
		
	JSize i 		= leadingCols + 1;
	JBoolean found 	= kFalse;
	JIndex tempIndex;
	
	while (i <= GetFMColCount(visibleCols, leadingCols) && !(found))
		{
		if (GetFMColType(visibleCols, leadingCols, i) == kGFMMode)
			{
			tempIndex = i;
			found = kTrue;
			}
		i++;
		}
			
	if (index - tempIndex >= 9)
		{
		*subIndex = 1;
		return kGFMDate;
		}
	
	*subIndex = index - tempIndex + 1;
	return kGFMMode;
}

/******************************************************************************
 GetFMWidthUsed

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


JSize 
GetFMWidthUsed
	(
	const JBoolean visibleCols[]
	)
{
	JSize width = 0;
	
	if (GFMTypeVisible(visibleCols, kGFMUID))
		{
		width += kUIDWidth;
		}
		
	if (GFMTypeVisible(visibleCols, kGFMGID))
		{
		width += kGIDWidth;
		}
		
	if (GFMTypeVisible(visibleCols, kGFMSize))
		{
		width += kSizeWidth;
		}
		
	if (GFMTypeVisible(visibleCols, kGFMMode))
		{
		width += kModeWidth;
		}
		
	if (GFMTypeVisible(visibleCols, kGFMDate))
		{
		width += kDateWidth;
		}

	return width;		
}