/*
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 *
 * The Apereo Foundation licenses this file to you 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.unitime.timetable.model.comparators;

import java.util.Comparator;

import org.unitime.timetable.model.Staff;


/**
 * Compares Staff based on specified criteria
 * Defaults to compare by name
 * 
 * @author Heston Fernandes, Tomas Muller
 */
public class StaffComparator implements Comparator {

    public static final short COMPARE_BY_NAME = 1;
    public static final short COMPARE_BY_POSITION = 2;
    
    private short compareBy;
    
    public StaffComparator() {
        compareBy = COMPARE_BY_NAME;
    }
    
    public StaffComparator(short compareBy) {
        if (compareBy!=COMPARE_BY_NAME 
                && compareBy!=COMPARE_BY_POSITION) {
            this.compareBy = COMPARE_BY_NAME;
        }
        else {
            this.compareBy = compareBy;
        }
    }

    public int compare(Object o1, Object o2) {
        if (!(o1 instanceof Staff) || o1==null)
            throw new ClassCastException("o1 Class must be of type Staff and cannot be null");
        if (!(o2 instanceof Staff) || o2==null)
            throw new ClassCastException("o2 Class must be of type Staff and cannot be null");
        
        Staff s1 = (Staff) o1;
        Staff s2 = (Staff) o2;
        
        if (compareBy==COMPARE_BY_POSITION) {
            Integer l1 = Integer.valueOf(-1);
            if (s1.getPositionType()!=null)
                l1 = s1.getPositionType().getSortOrder();
            
            Integer l2 = Integer.valueOf(-1);
            if (s2.getPositionType()!=null)
                l2 = s2.getPositionType().getSortOrder();
            
            int ret = l1.compareTo(l2);
            if (ret!=0)
                return ret;
        }
        
        return  s1.nameLastNameFirst().compareToIgnoreCase(s2.nameLastNameFirst());
    }

}
