/*******************************************************************************
 * Copyright (c) 2010 Attila Kovacs <attila_kovacs@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@post.harvard.edu> - initial API and implementation
 ******************************************************************************/

// Copyright (c) 2010 Attila Kovacs

package crush;

import java.lang.reflect.*;
import java.util.*;


import util.Configurator;
import util.Range;
import util.Unit;

public class Modality<ModeType extends Mode> extends ArrayList<ModeType> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2573110865041365370L;
	
	
	public String name;
	public String id;
	public String trigger;
	
	public boolean solveGains = true;
	
	public Range gainRange = new Range();
	public int gainFlag = 0;
	public int gainType = Instrument.GAINS_BIDIRECTIONAL;

	public double resolution = Double.NaN;
	
	protected Modality(String name, String id) {
		this.name = name;
		this.id = id;
		gainRange.fullRange();
	}
	
	public Modality(String name, String id, ChannelDivision<?> division, Field gainField, Class<? extends ModeType> modeClass) { 
		this(name, id, division, modeClass);
		for(Mode mode : this) mode.setGainField(gainField);
	}
	
	public Modality(String name, String id, ChannelDivision<?> division, Class<? extends ModeType> modeClass) {
		this(name, id);
		for(ChannelGroup<?> group : division) {
			try {
				ModeType mode = modeClass.newInstance();
				mode.setChannels(group);
				add(mode);
			}
			catch(Exception e) { e.printStackTrace(); }
		}
		setDefaultNames();
	}
	
		
	public void setDefaultNames() {
		for(int i=0; i<size(); i++) get(i).name = name + ":" + get(i).channels.name;
	}
	 
	public void setOptions(Configurator option) {
		solveGains = !option.isConfigured("nogains");
		resolution = option.isConfigured("resolution") ? option.get("resolution").getDouble() * Unit.s : 0.0;
		gainRange = option.isConfigured("gainrange") ? option.get("gainrange").getRange() : new Range();
		trigger = option.isConfigured("trigger") ? option.get("trigger").getValue() : null;
		gainType = option.isConfigured("positivegains") ? Instrument.GAINS_POSITIVE : Instrument.GAINS_BIDIRECTIONAL;
	}
	
	public boolean decorrelate(Integration<?, ?> integration, boolean isRobust, boolean isGainRobust) {
		if(trigger != null) if(!integration.hasOption(trigger)) return false;
		
		String left = isGainRobust ? "[" : "";
		String right = isGainRobust ? "]" : "";
		
		integration.comments += left + id + right;
		int frameResolution = integration.framesFor(resolution);
		if(frameResolution > 1) integration.comments += "(" + frameResolution + ")";	
		
		if(solveGains) getGains(integration, isGainRobust);
		
		return true;
	}

	
	public void getGains(Integration<?, ?> integration, boolean isRobust) {
		boolean isFlagging = false;
		
		for(Mode mode : this) if(!mode.fixedGains) {
			mode.getGains(integration, isRobust);
			try { 
				mode.flagGains(gainRange, gainFlag, gainType);
				isFlagging = true;
			}
			catch(IllegalAccessException e) { e.printStackTrace(); }
		}
		
		if(isFlagging) {
			integration.instrument.census();
			integration.comments += integration.instrument.mappingChannels;
		}	
	}
	
}
