package innovimax.mixthem.operation;

import innovimax.mixthem.MixException;
import innovimax.mixthem.arguments.ParamValue;
import innovimax.mixthem.arguments.RuleParam;
import innovimax.mixthem.arguments.TokenType;
import innovimax.mixthem.io.Token;

//import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
* <p>Alternate two or more bytes.</p>
* <p>When a byte is missing then next available byte is selected.</p>
* @see IByteOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultByteAlternation extends AbstractTokenOperation /*AbstractByteOperation*/ {
	
	private final AltMode mode;
	private int channel;
	private final Random random;	
	
	/**
	* Constructor
	* @param mode The alternate mode to process
	* @param selection The file index selection (maybe empty)
 	* @param params The list of parameters (maybe empty)
	* @see innovimax.mixthem.arguments.RuleParam
	* @see innovimax.mixthem.arguments.ParamValue
	*/
	public DefaultByteAlternation(final AltMode mode, final Set<Integer> selection, final TokenType tokenType, final Map<RuleParam, ParamValue> params) {
		super(selection, tokenType, params);
		this.mode = mode;
		this.channel = 0;
		this.random = new Random(params.getOrDefault(RuleParam.RANDOM_SEED, AltOperation.DEFAULT_RANDOM_SEED.getValue()).asInt());
	}
	
	/*@Override	
	public void process(final byte[] byteRange, final ByteResult result) throws MixException {
		final int[] array = new int[1];
		int channel = this.mode == AltMode.NORMAL ? this.channel : this.random.nextInt(byteRange.length);
		byte b = byteRange[channel];
		//System.out.println("RANGE="+Arrays.toString(byteRange)+" CHANNEL="+channel+" BYTE="+b);
		if (b == -1) {					
			channel = nextChannel(byteRange, channel);
			b = byteRange[channel];
			//System.out.println("NEW_CHANNEL="+channel+" BYTE="+b);
		}
		array[0] = b;
		if (this.mode == AltMode.NORMAL) {
			this.channel = channel + 1;
			if (this.channel == byteRange.length) {
				this.channel = 0;
			}		
		}
		result.setResult(Arrays.stream(array));
	}

	private int nextChannel(final byte[] byteRange, final int curChannel) {
		int channel = curChannel+1;
		while (channel != curChannel) {
			if (channel < byteRange.length) {
				final byte b = byteRange[channel];
				if (b != -1) {
					break;
				}
				channel++;
			} else {
				channel = 0;
			}
		}
		return channel;
	}*/

	@Override
	public void process(List<Token> tokenRange, TokenResult result) throws MixException {		
		int channel = this.mode == AltMode.NORMAL ? this.channel : this.random.nextInt(tokenRange.size());
		byte b = tokenRange.get(channel).asByte();
		//System.out.println("RANGE="+tokenRange.toString()+" CHANNEL="+channel+" BYTE="+b);
		if (b == -1) {					
			channel = nextChannel(tokenRange, channel);
			b = tokenRange.get(channel).asByte();
			//System.out.println("NEW_CHANNEL="+channel+" BYTE="+b);
		}		
		if (this.mode == AltMode.NORMAL) {
			this.channel = channel + 1;
			if (this.channel == tokenRange.size()) {
				this.channel = 0;
			}		
		}
		result.setResult(Token.createByteToken(b));
	}
	
	private int nextChannel(List<Token> tokenRange, final int curChannel) {
		int channel = curChannel+1;
		while (channel != curChannel) {
			if (channel < tokenRange.size()) {
				final byte b = tokenRange.get(channel).asByte();
				if (b != -1) {
					break;
				}
				channel++;
			} else {
				channel = 0;
			}
		}
		return channel;
	}

}

@@ -249,35 +249,8 @@
 n;%0A%0A
-//import java.util.Arrays;%0A
 impo
@@ -294,33 +294,8 @@
 ap;%0A
-import java.util.Random;%0A
 impo
@@ -546,129 +546,21 @@
 oken
-Operation /*AbstractByteOperation*/ %7B%0A%09%0A%09private final AltMode mode;%0A%09private int channel;%0A%09private final Random random;%09
+Alternation %7B
 %0A%09%0A%09
@@ -993,16 +993,22 @@
 %09%09super(
+mode, 
 selectio
@@ -1033,1232 +1033,13 @@
 ms);
-%0A%09%09this.mode = mode;%0A%09%09this.channel = 0;%0A%09%09this.random = new Random(params.getOrDefault(RuleParam.RANDOM_SEED, AltOperation.DEFAULT_RANDOM_SEED.getValue()).asInt());%0A%09%7D%0A%09%0A%09/*@Override%09%0A%09public void process(final byte%5B%5D byteRange, final ByteResult result) throws MixException %7B%0A%09%09final int%5B%5D array = new int%5B1%5D;%0A%09%09int channel = this.mode == AltMode.NORMAL ? this.channel : this.random.nextInt(byteRange.length);%0A%09%09byte b = byteRange%5Bchannel%5D;%0A%09%09//System.out.println(%22RANGE=%22+Arrays.toString(byteRange)+%22 CHANNEL=%22+channel+%22 BYTE=%22+b);%0A%09%09if (b == -1) %7B%09%09%09%09%09%0A%09%09%09channel = nextChannel(byteRange, channel);%0A%09%09%09b = byteRange%5Bchannel%5D;%0A%09%09%09//System.out.println(%22NEW_CHANNEL=%22+channel+%22 BYTE=%22+b);%0A%09%09%7D%0A%09%09array%5B0%5D = b;%0A%09%09if (this.mode == AltMode.NORMAL) %7B%0A%09%09%09this.channel = channel + 1;%0A%09%09%09if (this.channel == byteRange.length) %7B%0A%09%09%09%09this.channel = 0;%0A%09%09%09%7D
 %09%09%0A
-%09
 %09%7D
-%0A%09%09result.setResult(Arrays.stream(array));%0A%09%7D%0A%0A%09private int nextChannel(final byte%5B%5D byteRange, final int curChannel) %7B%0A%09%09int channel = curChannel+1;%0A%09%09while (channel != curChannel) %7B%0A%09%09%09if (channel %3C byteRange.length) %7B%0A%09%09%09%09final byte b = byteRange%5Bchannel%5D;%0A%09%09%09%09if (b != -1) %7B%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%09channel++;%0A%09%09%09%7D else %7B%0A%09%09%09%09channel = 0;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return channel;%0A%09%7D*/
 %0A%0A%09@
@@ -1742,349 +1742,8 @@
 ;%0A%09%7D
-%0A%09%0A%09private int nextChannel(List%3CToken%3E tokenRange, final int curChannel) %7B%0A%09%09int channel = curChannel+1;%0A%09%09while (channel != curChannel) %7B%0A%09%09%09if (channel %3C tokenRange.size()) %7B%0A%09%09%09%09final byte b = tokenRange.get(channel).asByte();%0A%09%09%09%09if (b != -1) %7B%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%09channel++;%0A%09%09%09%7D else %7B%0A%09%09%09%09channel = 0;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return channel;%0A%09%7D
 %0A%0A%7D%0A

Update byte alternation op using abstract class