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:
				//TODO: manage more than 2 channels...
				boolean first = this.random.nextBoolean();
				byte b = first ? bytes[0] : bytes[1];
				if (b == -1) {
					array[0] = first ? bytes[1] : bytes[0];
				} else {
					array[0] = b;
				}
				break;
			case NORMAL:
			default:				
				byte bn = bytes[this.channel];
				System.out.println("BYTES="+Arrays.toString(bytes)+" CHANNEL="+channel+" BYTE="+bn);
				if (bn == -1) {
					this.channel = nextChannel(bytes, this.channel);
					bn = bytes[this.channel];
					System.out.println("NEW_CHANNEL="+channel+" BYTE="+bn);
				}
				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;
	}

}

@@ -1728,69 +1728,28 @@
 DOM:
+%09%09%09%09
 %0A%09%09%09%09
-//TODO: manage more than 2 channels...%0A%09%09%09%09boolean first
+int channel
  = t
@@ -1763,24 +1763,32 @@
 dom.next
-Boolean(
+Int(bytes.size()
 );%0A%09%09%09%09b
@@ -1796,37 +1796,25 @@
 te b
+r
  = 
-first ? bytes%5B0%5D : bytes%5B1
+bytes%5Bchannel
 %5D;%0A%09
@@ -1821,16 +1821,17 @@
 %09%09%09if (b
+r
  == -1) 
@@ -1841,62 +1841,79 @@
 %09%09%09%09
-array%5B0%5D = first ? bytes%5B1%5D : bytes%5B0%5D;%0A%09%09%09%09%7D else %7B%0A%09
+channel = nextChannel(bytes, channel);%0A%09%09%09%09%09br = bytes%5Bchannel%5D;%0A%09%09%09%09%7D%0A
 %09%09%09%09
@@ -1924,23 +1924,18 @@
 y%5B0%5D = b
+r
 ;
-%0A%09%09%09%09%7D
 %0A%09%09%09%09bre
@@ -2009,97 +2009,8 @@
 l%5D;%0A
-%09%09%09%09System.out.println(%22BYTES=%22+Arrays.toString(bytes)+%22 CHANNEL=%22+channel+%22 BYTE=%22+bn);%0A
 %09%09%09%09
@@ -2114,69 +2114,8 @@
 l%5D;%0A
-%09%09%09%09%09System.out.println(%22NEW_CHANNEL=%22+channel+%22 BYTE=%22+bn);%0A
 %09%09%09%09
@@ -2134,20 +2134,16 @@
 0%5D = bn;
-%09%09%09%09
 %0A%09%09%09%09thi

Update DefaultByteAlternation.java