/*******************************************************************************
 * 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) 2009,2010 Attila Kovacs

package crush;

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

import util.Range;

public class Mode {
	public String name;
	public ChannelGroup<?> channels;
	public Field gainField;
	
	public boolean fixedGains = false;
	public double resolution;
	
	private static int counter = 0;
	private float[] gain;
		
	public Mode() {
		name = "mode-" + (++counter);
	}

	public Mode(ChannelGroup<?> group) {
		setChannels(group);
	}
	
	public Mode(ChannelGroup<?> group, Field gainField) { 
		setGainField(gainField);
		setChannels(group);
	}
	
	@Override
	public int hashCode() {
		return name.hashCode();
	}

	public void setGainField(Field f) {
		gainField = f;
	}	
	
	public void setChannels(ChannelGroup<?> group) {
		channels = group;
		name = group.name;
	}
	
	public synchronized float[] getGains() throws IllegalAccessException {
		if(gainField == null) {
			if(gain == null) {
				gain = new float[channels.size()];
				Arrays.fill(gain, 0.0F);
			}
			if(gain.length != channels.size()) {
				gain = new float[channels.size()];
				Arrays.fill(gain, 0.0F);
			}
			return gain;
		}
		else {
			if(gain == null) gain = new float[channels.size()];
			if(gain.length != channels.size()) gain = new float[channels.size()];
			for(int c=0; c<channels.size(); c++) gain[c] = (float) gainField.getDouble(channels.get(c));
			return gain;
		}
	}
	
	public synchronized void setGains(float[] gain) throws IllegalAccessException { 
		if(gainField == null) this.gain = gain; 
		else {
			Class<?> fieldClass = gainField.getClass();
			if(fieldClass.equals(float.class)) for(int c=0; c<channels.size(); c++) gainField.setFloat(channels.get(c), gain[c]);
			else for(int c=0; c<channels.size(); c++) gainField.setDouble(channels.get(c), gain[c]);
		}
	}
	
	
	public synchronized void flagGains(Range acceptable, int pattern, int normMethod) throws IllegalAccessException {
		if(pattern == 0) return;
		
		float[] gain = getGains();
		
		for(int k=0; k<channels.size(); k++) {
			Channel pixel = channels.get(k);
			
			float G = Float.NaN;
			if(normMethod == Instrument.GAINS_POSITIVE) G = gain[k];
			else if(normMethod == Instrument.GAINS_BIDIRECTIONAL) G = Math.abs(gain[k]);

			if(!acceptable.contains(G)) pixel.flag(pattern);
			else pixel.unflag(pattern);
		}
	}
	
	
	public void getGains(Integration<?, ?> integration, boolean isRobust) {
		if(fixedGains) throw new IllegalStateException("WARNING! Cannot solve gains for fixed gain modes.");
		try { integration.getGains(this, isRobust); }
		catch(IllegalAccessException e) { e.printStackTrace(); }
	}
	
	public int getFrameResolution(Integration<?, ?> integration) {
		return integration.framesFor(resolution);
	}
	
	public int getSize(Integration<?, ?> integration) {
		return (int)Math.ceil((double) integration.size() / getFrameResolution(integration));
	}
	
	protected static int nextMode = 0;
	public final static int TOTAL_POWER = nextMode++;
	public final static int CHOPPED = nextMode++;
	
}
