/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.messaging;

/**
 *
 * @author  Administrator
 */
public class MessageTreeNodeComparator implements java.util.Comparator
{
    public final static int REVERSE_CREATED_DATE = 1;
    public final static int CREATED_DATE = 2;
    public final static int REVERSE_UPDATED_DATE = 3;
    public final static int UPDATED_DATE = 4;
    public final static int REVERSE_AUTHOR = 5;
    public final static int AUTHOR = 6;
    public final static int REVERSE_SUBJECT = 7;
    public final static int SUBJECT = 8;
    
    private int type = REVERSE_CREATED_DATE;
    private MessagingRoom room;
    
    /** Creates a new instance of MessageTreeNodeComparator */
    public MessageTreeNodeComparator( MessagingRoom room, int type )
    {
        if ( type < REVERSE_CREATED_DATE || type > SUBJECT )
            throw new IllegalArgumentException( "Unknown type of sort." );
        this.room = room;
        this.type = type;
    }
    
    public int compare(Object o1, Object o2)
    {
        if ( o1 == null && o2 == null ) return 0;
        if ( o1 == null ) return -1;
        if ( o1 == null ) return 1;
        
        if ( !(o1 instanceof MessageTreeNode) && !(o1 instanceof MessageTreeNode) ) return 0;
        if ( !(o1 instanceof MessageTreeNode) ) return -1;
        if ( !(o2 instanceof MessageTreeNode) ) return 1;
        
        MessageTreeNode node1 = (MessageTreeNode)o1;
        MessageTreeNode node2 = (MessageTreeNode)o2;
        MessageSummary s1, s2;
        java.sql.Timestamp time1, time2;
        
        try
        {
            s1 = room.getMessageSummary( node1.getPrimaryKey() );
            if ( type == UPDATED_DATE || type == REVERSE_UPDATED_DATE )
                time1 = s1.getUpdatedTime();
            else
                time1 = s1.getCreatedTime();
            if ( time1 == null )
                return -1;
        }
        catch ( Exception e )
        {
            return -1;
        }
        
        try
        {
            s2 = room.getMessageSummary( node2.getPrimaryKey() );
            if ( type == UPDATED_DATE || type == REVERSE_UPDATED_DATE )
                time2 = s2.getUpdatedTime();
            else
                time2 = s2.getCreatedTime();
            if ( time2 == null )
                return 1;
        }
        catch ( Exception e )
        {
            return 1;
        }
        
        int alpha_result=0;
        if ( type == AUTHOR || type == REVERSE_AUTHOR )
        {
            String author1 = s1.getAuthorName();
            if ( author1 == null ) author1 = "";
            String author2 = s2.getAuthorName();
            if ( author2 == null ) author2 = "";
            alpha_result = author1.compareTo( author2 );
            if ( type == REVERSE_AUTHOR ) alpha_result = -alpha_result;
            if ( alpha_result != 0 )
                return alpha_result;
        }
        if ( type == SUBJECT || type == REVERSE_SUBJECT )
        {
            String subject1 = s1.getSubject();
            if ( subject1 == null ) subject1 = "";
            String subject2 = s2.getSubject();
            if ( subject2 == null ) subject2 = "";
            alpha_result = subject1.compareTo( subject2 );
            if ( type == REVERSE_SUBJECT ) alpha_result = -alpha_result;
            if ( alpha_result != 0 )
                return alpha_result;
        }
        
        int result = time1.compareTo( time2 );
        
        if ( type == REVERSE_CREATED_DATE || type == REVERSE_UPDATED_DATE )
            return -result;
        return result;
    }
    
    public boolean equals(Object obj)
    {
        if ( obj == null ) return false;
        if ( !(obj instanceof MessageTreeNodeComparator) ) return false;
        MessageTreeNodeComparator other = (MessageTreeNodeComparator)obj;
        return other.type == this.type;
    }
    
}
