/*******************************************************************************
 * Copyright (c) 2010 Attila Kovacs <attila_kovacs[AT]post.harvard.edu>.
 * All rights reserved. 
 * 
 * This file is part of crush.
 * 
 *     crush is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     crush is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with crush.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Attila Kovacs <attila_kovacs[AT]post.harvard.edu> - initial API and implementation
 ******************************************************************************/
// Copyright (c) 2009,2010 Attila Kovacs

package crush;

import java.lang.reflect.*;

import util.Configurator;
import util.Range;

public class CorrelatedModality extends Modality<CorrelatedMode> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1124638494612727550L;
	
	public boolean solveSignal = true;
	public Range gainRange = new Range();
	
	protected CorrelatedModality(String name, String id) {
		super(name, id);
	}
	
	public CorrelatedModality(String name, String id, ChannelDivision<?> division) {
		super(name, id, division, CorrelatedMode.class);
	}
	
	public CorrelatedModality(String name, String id, ChannelDivision<?> division, Field gainField) { 
		super(name, id, division, gainField, CorrelatedMode.class);
	}
	
	public CorrelatedModality(String name, String id, ChannelDivision<?> division, Class<? extends CorrelatedMode> modeClass) {
		super(name, id, division, modeClass);		
	}
	
	public CorrelatedModality(String name, String id, ChannelDivision<?> division, Field gainField, Class<? extends CorrelatedMode> modeClass) {
		super(name, id, division, gainField, modeClass);
	}
	
	@Override
	public void setOptions(Configurator option) {
		super.setOptions(option);
		solveSignal = !option.isConfigured("nosignals");
	}
	
	public void setSkipFlags(int pattern) {
		for(CorrelatedMode mode : this) mode.skipChannels = pattern;
	}
	

	public void getSignal(Integration<?, ?> integration, boolean isRobust) {
		for(CorrelatedMode mode : this) if(!mode.fixedSignal) {
			if(!Double.isNaN(resolution)) mode.resolution = resolution;
			mode.decorrelate(integration, isRobust);
		}
	}
	
	@Override
	public boolean decorrelate(Integration<?, ?> integration, boolean isRobust, boolean isGainRobust) {		
		if(trigger != null) if(!integration.hasOption(trigger)) return false;
		
		String left = isRobust ? "[" : "";
		String right = isRobust ? "]" : "";
		
		integration.comments += left + id + right;
		int frameResolution = integration.framesFor(resolution);
		if(frameResolution > 1) integration.comments += "(" + frameResolution + ")";	
		
		if(solveSignal) getSignal(integration, isRobust);
		if(solveGains) getGains(integration, isGainRobust);
		return true;
	}
	
}
