package innovimax.mixthem.operation;

import innovimax.mixthem.MixException;
import innovimax.mixthem.arguments.RuleParam;
import innovimax.mixthem.arguments.ParamValue;

import java.util.Arrays;
import java.util.Map;
import java.util.Random;

/**
* <p>Alternate two or more bytes.</p>
* @see IByteOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultByteAlternation extends AbstractByteOperation {
	
	private final AltMode mode;
	private boolean odd;
	private int channel;
	private final Random random;	
	
	/**
	* Constructor
	* @param mode The alternate mode to process
 	* @param params The list of parameters (maybe empty)
	* @see innovimax.mixthem.arguments.RuleParam
	* @see innovimax.mixthem.arguments.ParamValue
	*/
	public DefaultByteAlternation(final AltMode mode, final Map<RuleParam, ParamValue> params) {
		super(params);
		this.mode = mode;
		this.odd = true;
		this.channel = 0;
		this.random = new Random(params.getOrDefault(RuleParam.RANDOM_SEED, AltOperation.DEFAULT_RANDOM_SEED.getValue()).asInt());
	}
	
	@Override
	public void process(final byte b1, final byte b2, final ByteResult result) throws MixException {
		result.reset();
		final int[] array = new int[1];
		if (b1 == -1) {
			array[0] = b2;
		} else if (b2 == -1) {
			array[0] = b1;
		} else {
			switch (this.mode) {
				case RANDOM:					
					array[0] = this.random.nextBoolean() ? b1 : b2;
					break;
				case NORMAL:
				default:
					array[0] = this.odd ? b1 : b2;
					this.odd = !this.odd;	
			}			
		}
		result.setResult(Arrays.stream(array));
	}
	
	@Override
	public void process(byte[] bytes, ByteResult result) throws MixException {
		result.reset();
		final int[] array = new int[1];
		/*
		switch (this.mode) {
			case RANDOM:				
				int channel = this.random.nextInt(bytes.length);
				byte br = bytes[channel];
				System.out.println("BYTES+"+Arrays.toString(bytes)+" CHANNEL="+channel+" BYTE="+br);
				if (br == -1) {					
					channel = nextChannel(bytes, channel);
					br = bytes[channel];
					System.out.println("NEW_CHANNEL="+channel+" BYTE="+br);
				}
				array[0] = br;
				break;
			case NORMAL:
			default:				
				byte bn = bytes[this.channel];
				if (bn == -1) {
					this.channel = nextChannel(bytes, this.channel);
					bn = bytes[this.channel];
				}
				array[0] = bn;
				this.channel++;	
				if (this.channel == bytes.length) {
					this.channel = 0;
				}
		}
		*/
		int channel = this.mode == AltMode.NORMAL ? this.channel : this.random.nextInt(bytes.length);
		byte b = bytes[channel];
		System.out.println("BYTES+"+Arrays.toString(bytes)+" CHANNEL="+channel+" BYTE="+b);
		if (b == -1) {					
			channel = nextChannel(bytes, channel);
			b = bytes[channel];
			System.out.println("NEW_CHANNEL="+channel+" BYTE="+b);
		}
		array[0] = b;
		if (this.mode == AltMode.NORMAL) {
			this.channel = channel + 1;
			if (this.channel == bytes.length) {
				this.channel = 0;
			}		
		}
		result.setResult(Arrays.stream(array));
	}
	
	private int nextChannel(final byte[] bytes, final int channel) {
		int c = channel+1;
		while (c != channel) {
			if (c < bytes.length) {
				final byte b = bytes[c];
				if (b != -1) {
					break;
				}
				c++;
			} else {
				c = 0;
			}
		}
		return c;
	}

}

@@ -1068,1337 +1068,114 @@
 ess(
-final byte b1, final byte b2, final ByteResult result) throws MixException %7B%0A%09%09result.reset();%0A%09%09final int%5B%5D array = new int%5B1%5D;%0A%09%09if (b1 == -1) %7B%0A%09%09%09array%5B0%5D = b2;%0A%09%09%7D else if (b2 == -1) %7B%0A%09%09%09array%5B0%5D = b1;%0A%09%09%7D else %7B%0A%09%09%09switch (this.mode) %7B%0A%09%09%09%09case RANDOM:%09%09%09%09%09%0A%09%09%09%09%09array%5B0%5D = this.random.nextBoolean() ? b1 : b2;%0A%09%09%09%09%09break;%0A%09%09%09%09case NORMAL:%0A%09%09%09%09default:%0A%09%09%09%09%09array%5B0%5D = this.odd ? b1 : b2;%0A%09%09%09%09%09this.odd = !this.odd;%09%0A%09%09%09%7D%09%09%09%0A%09%09%7D%0A%09%09result.setResult(Arrays.stream(array));%0A%09%7D%0A%09%0A%09@Override%0A%09public void process(byte%5B%5D bytes, ByteResult result) throws MixException %7B%0A%09%09result.reset();%0A%09%09final int%5B%5D array = new int%5B1%5D;%0A%09%09/*%0A%09%09switch (this.mode) %7B%0A%09%09%09case RANDOM:%09%09%09%09%0A%09%09%09%09int channel = this.random.nextInt(bytes.length);%0A%09%09%09%09byte br = bytes%5Bchannel%5D;%0A%09%09%09%09System.out.println(%22BYTES+%22+Arrays.toString(bytes)+%22 CHANNEL=%22+channel+%22 BYTE=%22+br);%0A%09%09%09%09if (br == -1) %7B%09%09%09%09%09%0A%09%09%09%09%09channel = nextChannel(bytes, channel);%0A%09%09%09%09%09br = bytes%5Bchannel%5D;%0A%09%09%09%09%09System.out.println(%22NEW_CHANNEL=%22+channel+%22 BYTE=%22+br);%0A%09%09%09%09%7D%0A%09%09%09%09array%5B0%5D = br;%0A%09%09%09%09break;%0A%09%09%09case NORMAL:%0A%09%09%09default:%09%09%09%09%0A%09%09%09%09byte bn = bytes%5Bthis.channel%5D;%0A%09%09%09%09if (bn == -1) %7B%0A%09%09%09%09%09this.channel = nextChannel(bytes, this.channel);%0A%09%09%09%09%09bn = bytes%5Bthis.channel%5D;%0A%09%09%09%09%7D%0A%09%09%09%09array%5B0%5D = bn;%0A%09%09%09%09this.channel++;%09%0A%09%09%09%09if (this.channel == bytes.length) %7B%0A%09%09%09%09%09this.channel = 0;%0A%09%09%09%09%7D%0A%09%09%7D%0A%09%09*/
+byte%5B%5D bytes, ByteResult result) throws MixException %7B%0A%09%09result.reset();%0A%09%09final int%5B%5D array = new int%5B1%5D;
 %0A%09%09i

Update DefaultByteAlternation.java