/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usda.weru.mcrew;

import java.util.Collection;

/**
 * Classifies operations into planting, harvest or other 
 * 
 * See http://wind.weru.ksu.edu/wiki/Operation_Classification
 * 
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public enum OperationClassification {
    Planting,
    Harvest,
    Other;
    
    public static OperationClassification classify(OperationObject op){
        
        //planting requires process 51
        if(hasProcess(op.getAllActions(), 51)){
            return Planting;
        }
        else if(hasAnyProcess(op.getAllActions(), 32, 33, 37, 38, 42, 43, 47, 48)){
            return Harvest;
        }
        else if (hasAnyProcess(op.getAllActions(), 61, 62)){
            //must also have selpool not set to 4
            String[] values = op.getValues("selpool", -1);
            boolean valid = true;
            for(String value : values){
                if(Integer.valueOf(value) == 4){
                    valid = false;
                    break;
                }                
            }
            if(valid){
                return Harvest;
            }
        }
        
        return Other;
    }
    
    private static boolean hasAnyProcess(Collection<Action> actions, int... ids){
        for(int id : ids){
            if(hasProcess(actions, id)){
                return true;
            }
        }
        return false;
    }
    
//    private static boolean hasAllProcess(Collection<Action> actions, int... ids){
//        for(int id : ids){
//            if(!hasProcess(actions, id)){
//                return false;
//            }
//        }
//        return true;
//    }
    
    private static boolean hasProcess(Collection<Action> actions, int id){
        for(Action action : actions){
            if(action.getIdentity().equals(new Identity(id, "P"))){
                return true;
            }
        }
        return false;        
    }
    
}
