package com.emc.mongoose.model.impl.io.task;

import com.emc.mongoose.model.api.io.task.DataIoTask;
import com.emc.mongoose.model.api.item.DataItem;
import com.emc.mongoose.model.util.LoadType;

import java.io.IOException;

public class BasicDataIoTask<T extends DataItem>
extends BasicIoTask<T>
implements DataIoTask<T> {

	protected final long contentSize;
	protected volatile long countBytesDone = 0;
	protected volatile DataItem currRange = null;
	protected volatile long currRangeSize = 0;
	protected volatile long nextRangeOffset = 0;
	protected volatile int currRangeIdx = 0;
	protected volatile int currDataLayerIdx = 0;
	protected volatile long respDataTimeStart = 0;

	public BasicDataIoTask(final LoadType ioType, final T item, final String dstPath)
	throws IOException {
		super(ioType, item, dstPath);
		item.reset();
		//currDataLayerIdx = item.getCurrLayerIndex();
		switch(ioType) {
			case CREATE:
			case READ:
				// TODO partial read support
				try {
					contentSize = item.size();
				} catch(IOException e) {
					throw new IllegalStateException();
				}
				break;
			/*case UPDATE:
				if(item.hasScheduledUpdates()) {
					contentSize = item.getUpdatingRangesSize();
				} else if(item.isAppending()) {
					contentSize = item.getAppendSize();
				} else {
					contentSize = item.size();
				}
				break;*/
			default:
				contentSize = 0;
				break;
		}
	}

	@Override
	public final long getCountBytesDone() {
		return countBytesDone;
	}

	@Override
	public final void setCountBytesDone(final long n) {
		this.countBytesDone = n;
		if(contentSize == countBytesDone) {
			status = Status.SUCC;
		}
	}

	@Override
	public final void setRespDataTimeStart(final long respDataTimeStart) {
		this.respDataTimeStart = respDataTimeStart;
	}
	
	@Override
	public final int getDataLatency() {
		return respDataTimeStart > reqTimeDone ? (int) (respDataTimeStart - reqTimeDone) : -1;
	}

	@Override @SuppressWarnings("ResultOfMethodCallIgnored")
	public String toString() {
		super.toString(); // invoked to fill the string builder
		final StringBuilder strb = STRB.get();
		return strb
			.append(',').append(countBytesDone)
			.append(',').append(getDataLatency())
			.toString();
	}
}

@@ -1807,23 +1807,19 @@
 y() %7B%0A%09%09
-return 
+if(
 respData
@@ -1832,25 +1832,26 @@
 art %3E re
-q
+sp
 TimeDone
  ? (int)
@@ -1846,10 +1846,21 @@
 Done
- ?
+) %7B%0A%09%09%09return
  (in
@@ -1899,14 +1899,38 @@
 one)
- : -1;
+;%0A%09%09%7D else %7B%0A%09%09%09return -1;%0A%09%09%7D
 %0A%09%7D%0A

replace ternary operator with ifelse