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;
				}
		}					
		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;
	}

}

@@ -1686,16 +1686,21 @@
 int%5B1%5D;%0A
+%09%09/*%0A
 %09%09switch
@@ -1714,16 +1714,16 @@
 mode) %7B%0A
-
 %09%09%09case 
@@ -2054,17 +2054,17 @@
 l+%22 BYTE
-+
+=
 %22+br);%0A%09
@@ -2388,21 +2388,531 @@
 %09%09%09%7D%0A%09%09%7D
-%09%09%09%09%09
+%0A%09%09*/%0A%09%09int channel = this.mode == AltMode.NORMAL ? this.channel : this.random.nextInt(channels);%0A%09%09byte b = bytes%5Bchannel%5D;%0A%09%09System.out.println(%22BYTES+%22+Arrays.toString(bytes)+%22 CHANNEL=%22+channel+%22 BYTE=%22+b);%0A%09%09if (b == -1) %7B%09%09%09%09%09%0A%09%09%09channel = nextChannel(bytes, channel);%0A%09%09%09b = bytes%5Bchannel%5D;%0A%09%09%09System.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 == bytes.length) %7B%0A%09%09%09%09this.channel = 0;%0A%09%09%09%7D%09%09%0A%09%09%7D
 %0A%09%09resul

Update DefaultByteAlternation.java