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 characters.</p>
* @see ICharOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultCharAlternation extends AbstractCharOperation {
	
	private final AltMode mode;
	private boolean odd;
	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 DefaultCharAlternation(final AltMode mode, final Map<RuleParam, ParamValue> params) {
		super(params);
		this.mode = mode;
		this.odd = true;
		this.random = new Random(params.getOrDefault(RuleParam.RANDOM_SEED, AltOperation.DEFAULT_RANDOM_SEED.getValue()).asInt());
	}
	
	@Override
	public void process(final int c1, final int c2, final CharResult result) throws MixException {
		result.reset();
		final int[] array = new int[1];
		if (c1 == -1) {
			array[0] = c2;
		} else if (c2 == -1) {
			array[0] = c1;
		} else {
			switch (this.mode) {
				case RANDOM:					
					array[0] = this.random.nextBoolean() ? c1 : c2;
					break;
				case NORMAL:
				default:
					array[0] = this.odd ? c1 : c2;
					this.odd = !this.odd;
			}
		}
		result.setResult(Arrays.stream(array));
	}
	
	@Override
	public void process(final int[] charRange, final CharResult result) throws MixException {
		//TODO
	}

}

@@ -1603,14 +1603,961 @@
 %7B%0A%09%09
-//TODO
+result.reset();%0A%09%09final int%5B%5D array = new int%5B1%5D;%0A%09%09int channel = this.mode == AltMode.NORMAL ? this.channel : this.random.nextInt(charRange.length);%0A%09%09int c = charRange%5Bchannel%5D;%0A%09%09System.out.println(%22RANGE=+%22+Arrays.toString(charRange)+%22 CHANNEL=%22+channel+%22 CHAR=%22+c);%0A%09%09if (c == -1) %7B%09%09%09%09%09%0A%09%09%09channel = nextChannel(charRange, channel);%0A%09%09%09c = charRange%5Bchannel%5D;%0A%09%09%09System.out.println(%22NEW_CHANNEL=%22+channel+%22 CHAR=%22+c);%0A%09%09%7D%0A%09%09array%5B0%5D = c;%0A%09%09if (this.mode == AltMode.NORMAL) %7B%0A%09%09%09this.channel = channel + 1;%0A%09%09%09if (this.channel == charRange.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%09%0A%09private int nextChannel(final int%5B%5D charRange, final int curChannel) %7B%0A%09%09int channel = curChannel+1;%0A%09%09while (channel != curChannel) %7B%0A%09%09%09if (channel %3C charRange.length) %7B%0A%09%09%09%09final int c = charRange%5Bchannel%5D;%0A%09%09%09%09if (c != -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

Update DefaultCharAlternation.java