// 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.DataRecord;

public class JournalDataRecord
{
	//*	--------------------	JournalDataRecord

	public
	JournalDataRecord()
	{
		mData = new DataRecord(kNumRows);
		
		SetSubject("");
		SetEntry("");
		SetJournal("");
		
		SetFlags(0);
		SetUID(0);
		SetItemID(0);
		SetDate(0);
		SetAllowMask(0);
		SetSecurity(0);
		SetMood("");
		SetMusic("");
		SetPic("");
		SetAllowComments(true);
		SetAllowEmail(true);
	}

	//*	--------------------	JournalDataRecord

	public
	JournalDataRecord(byte[] inData)
	{
		mData = new DataRecord(inData, kNumRows);
	}

	//*	--------------------	Overlay

	public final void
	Overlay(byte[] inData)
	{
		mData.mData = inData;
	}

	//*	--------------------	Subject

	public final String
	Subject()
	{
		return mData.getString(kSubjectRow, 0);
	}
	
	//*	--------------------	SetSubject

	public final void
	SetSubject(String inSubject)
	{
		if (null != inSubject)
			mData.putString(kSubjectRow, 0, inSubject);
	}

	//*	--------------------	Entry

	public final String
	Entry()
	{
		return mData.getString(kEntryRow, 0);
	}
	
	//*	--------------------	SetEntry

	public final void
	SetEntry(String inEntry)
	{
		if (null != inEntry)
			mData.putString(kEntryRow, 0, inEntry);
	}

	//*	--------------------	Journal

	public final String
	Journal()
	{
		return mData.getString(kJournalRow, 0);
	}
	
	//*	--------------------	SetJournal

	public final void
	SetJournal(String inEntry)
	{
		if (null != inEntry)
			mData.putString(kJournalRow, 0, inEntry);
	}

	//*	--------------------	Pic

	public final String
	Pic()
	{
		return mData.getString(kPicRow, 0);
	}
	
	//*	--------------------	SetPic

	public final void
	SetPic(String inEntry)
	{
		if (null != inEntry)
			mData.putString(kPicRow, 0, inEntry);
	}

	//*	--------------------	Mood

	public final String
	Mood()
	{
		return mData.getString(kMoodRow, 0);
	}
	
	//*	--------------------	SetMood

	public final void
	SetMood(String inEntry)
	{
		if (null != inEntry)
			mData.putString(kMoodRow, 0, inEntry);
	}

	//*	--------------------	Music

	public final String
	Music()
	{
		return mData.getString(kMusicRow, 0);
	}
	
	//*	--------------------	SetMusic

	public final void
	SetMusic(String inEntry)
	{
		if (null != inEntry)
			mData.putString(kMusicRow, 0, inEntry);
	}

	//*	--------------------	Date

	public final int
	Date()
	{
		return mData.getInt(kMiscRow, kDateColumn);
	}

	//*	--------------------	SetDate

	public final void
	SetDate(int inDate)
	{
		mData.putInt(kMiscRow, kDateColumn, inDate);
	}

	//*	--------------------	LastCommentDate

	public final int
	LastCommentDate()
	{
		return mData.getInt(kMiscRow, kLastCommentDate);
	}

	//*	--------------------	SetLastCommentDate

	public final void
	SetLastCommentDate(int inDate)
	{
		mData.putInt(kMiscRow, kLastCommentDate, inDate);
	}

	//*	--------------------	ItemID

	public final int
	ItemID()
	{
		return mData.getInt(kMiscRow, kItemIDColumn);
	}

	//*	--------------------	SetItemID

	public final void
	SetItemID(int inID)
	{
		mData.putInt(kMiscRow, kItemIDColumn, inID);
	}

	//*	--------------------	UID

	public final int
	UID()
	{
		return mData.getInt(kMiscRow, kUIDColumn);
	}

	//*	--------------------	SetUID

	public final void
	SetUID(int inUID)
	{
		mData.putInt(kMiscRow, kUIDColumn, inUID);
	}

	//*	--------------------	Security

	public final int
	Security()
	{
		return mData.getInt(kMiscRow, kSecurityColumn);
	}

	//*	--------------------	SetSecurity

	public final void
	SetSecurity(int inSecurity)
	{
		mData.putInt(kMiscRow, kSecurityColumn, inSecurity);
	}

	//*	--------------------	AllowMask

	public final int
	AllowMask()
	{
		return mData.getInt(kMiscRow, kAllowMaskColumn);
	}

	//*	--------------------	SetAllowMask

	public final void
	SetAllowMask(int inMask)
	{
		mData.putInt(kMiscRow, kAllowMaskColumn, inMask);
	}

	//*	--------------------	Flags

	private final int
	Flags()
	{
		return mData.getInt(kMiscRow, kFlagsColumn);
	}

	//*	--------------------	StateFlags

	public final int
	StateFlags()
	{
		return Flags() & kStateFlagsMask;
	}

	//*	--------------------	SetFlags

	public final void
	SetFlags(int inFlags)
	{
		mData.putInt(kMiscRow, kFlagsColumn, inFlags);
	}

	//*	--------------------	SetDeleting

	public final void
	SetDeleting(boolean inState)
	{
		TwiddleFlag(kDeleting, inState);
	}

	//*	--------------------	Deleting

	public final boolean
	Deleting()
	{
		return GetFlag(kDeleting);
	}

	//*	--------------------	SetEditing

	public final void
	SetEditing(boolean inState)
	{
		TwiddleFlag(kEditing, inState);
	}

	//*	--------------------	Editing

	public final boolean
	Editing()
	{
		return GetFlag(kEditing);
	}

	//*	--------------------	SetPosting

	public final void
	SetPosting(boolean inState)
	{
		TwiddleFlag(kPosting, inState);
	}

	//*	--------------------	Posting

	public final boolean
	Posting()
	{
		return GetFlag(kPosting);
	}

	//*	--------------------	SetAlloComments

	public final void
	SetAllowComments(boolean inState)
	{
		TwiddleFlag(kAllowComments, inState);
	}

	//*	--------------------	AllowComments

	public final boolean
	AllowComments()
	{
		return GetFlag(kAllowComments);
	}

	//*	--------------------	SetAllowEmail

	public final void
	SetAllowEmail(boolean inState)
	{
		TwiddleFlag(kAllowEmail, inState);
	}

	//*	--------------------	AllowEmail

	public final boolean
	AllowEmail()
	{
		return GetFlag(kAllowEmail);
	}

	//*	--------------------	ClearStateFlags

	public final void
	ClearStateFlags()
	{
		SetFlags(Flags() & ~kStateFlagsMask);
	}

	//*	--------------------	TwiddleFlag

	private final void
	TwiddleFlag(int inFlag, boolean inState)
	{
	int	flags = Flags();
	
		if (inState)
			flags |= inFlag;
		else
			flags &= ~inFlag;
			
		SetFlags(flags);
	}

	//*	--------------------	GetFlag

	private final boolean
	GetFlag(int inFlag)
	{
		if (0 == (Flags() & inFlag))
			return false;
			
		return true;
	}

	//*	--------------------	RecordVersion

	public static final int
	RecordVersion()
	{
		return 4;
	}

	//*	--------------------------------	Instance Variables

	DataRecord	mData;
	
	//*	--------------------------------	Constants

	private static final int	kSubjectRow			=	0;
	private static final int	kEntryRow			=	1;
	private static final int	kJournalRow			=	2;
	private static final int	kMoodRow			=	3;
	private static final int	kMusicRow			=	4;
	private static final int	kMiscRow			=	5;
	private static final int	kPicRow				=	6;
	private static final int	kNumRows			=	7;
	
	private static final int	kSecurityColumn		=	0;
 	private static final int	kAllowMaskColumn	=	1;
	private static final int	kDateColumn			=	2;
	private static final int	kLastCommentDate	=	3;
	private static final int	kItemIDColumn		=	4;
	private static final int	kUIDColumn			=	5;
	private static final int	kFlagsColumn		=	6;

	//*	State machine flags
	private static final int	kPosting			=	0x0001;
	private static final int	kEditing			=	0x0002;
	private static final int	kDeleting			=	0x0004;
	private static final int	kStateFlagsMask		=	0x00FF;
	//*	Posting flags
	private static final int	kAllowComments		=	0x0100;
	private static final int	kAllowEmail			=	0x0200;
}
