package com.emc.mongoose.load.step.weighted;

import com.emc.mongoose.env.Extension;
import com.emc.mongoose.exception.InterruptRunException;
import com.emc.mongoose.item.op.OpType;
import com.emc.mongoose.load.step.client.LoadStepClient;
import com.emc.mongoose.load.step.client.LoadStepClientBase;
import com.emc.mongoose.logging.LogUtil;
import com.emc.mongoose.metrics.MetricsManager;
import com.github.akurilov.commons.reflection.TypeUtil;
import com.github.akurilov.commons.system.SizeInBytes;
import com.github.akurilov.confuse.Config;
import com.github.akurilov.confuse.exceptions.InvalidValuePathException;
import com.github.akurilov.confuse.exceptions.InvalidValueTypeException;
import com.github.akurilov.confuse.impl.BasicConfig;
import org.apache.logging.log4j.Level;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import static com.github.akurilov.commons.collection.TreeUtil.reduceForest;
import static com.github.akurilov.confuse.Config.deepToMap;

public class WeightedLoadStepClient
	extends LoadStepClientBase {

	public WeightedLoadStepClient(
		final Config config, final List<Extension> extensions, final List<Config> ctxConfigs,
		final MetricsManager metricsManager
	) {
		super(config, extensions, ctxConfigs, metricsManager);
	}

	@Override
	public String getTypeName() {
		return WeightedLoadStepExtension.TYPE;
	}

	@Override
	@SuppressWarnings("unchecked")
	protected <T extends LoadStepClient> T copyInstance(final Config config, final List<Config> ctxConfigs) {
		return (T) new WeightedLoadStepClient(config, extensions, ctxConfigs, metricsMgr);
	}

	@Override
	protected void init()
	throws InterruptRunException, IllegalStateException {
		final String autoStepId = "weighted_" + LogUtil.getDateTimeStamp();
		final Config stepConfig = config.configVal("load-step");
		if(stepConfig.boolVal("idAutoGenerated")) {
			stepConfig.val("id", autoStepId);
		}
		final int subStepCount = ctxConfigs.size();
		// 2nd pass: initialize the sub steps
		for(int originIndex = 0; originIndex < subStepCount; originIndex++) {
			final Map<String, Object> mergedConfigTree = reduceForest(
				Arrays.asList(deepToMap(config), deepToMap(ctxConfigs.get(originIndex)))
			);
			final Config subConfig;
			try {
				subConfig = new BasicConfig(config.pathSep(), config.schema(), mergedConfigTree);
			} catch(final InvalidValueTypeException | InvalidValuePathException e) {
				LogUtil.exception(Level.FATAL, e, "Scenario syntax error");
				throw new InterruptRunException(e);
			}
			OpType opType;
			try {
				opType = OpType.valueOf(subConfig.stringVal("load-op-type").toUpperCase());
			} catch(final NoSuchElementException e) {
				opType = OpType.CREATE;
			}
			final int concurrencyLimit = config.intVal("storage-driver-limit-concurrency");
			final Config outputConfig = subConfig.configVal("output");
			final Config metricsConfig = outputConfig.configVal("metrics");
			final SizeInBytes itemDataSize;
			final Object itemDataSizeRaw = subConfig.val("item-data-size");
			if(itemDataSizeRaw instanceof String) {
				itemDataSize = new SizeInBytes((String) itemDataSizeRaw);
			} else {
				itemDataSize = new SizeInBytes(TypeUtil.typeConvert(itemDataSizeRaw, long.class));
			}
			final boolean colorFlag = outputConfig.boolVal("color");
			initMetrics(originIndex, opType, concurrencyLimit, metricsConfig, itemDataSize, colorFlag);
		}
	}
}

@@ -2563,36 +2563,21 @@
 %0A%09%09%09
+final 
 OpType 
-opType;%0A%09%09%09try %7B%0A%09%09%09%09
 opTy
@@ -2652,86 +2652,8 @@
 ));%0A
-%09%09%09%7D catch(final NoSuchElementException e) %7B%0A%09%09%09%09opType = OpType.CREATE;%0A%09%09%09%7D%0A
 %09%09%09f

save [skip ci]