package eu.dnetlib.actionmanager.actions;

import java.util.List;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import com.google.common.collect.Lists;

import eu.dnetlib.actionmanager.ActionManagerConstants;
import eu.dnetlib.actionmanager.ActionManagerConstants.ACTION_TYPE;
import eu.dnetlib.actionmanager.common.Agent;
import eu.dnetlib.actionmanager.common.Operation;
import eu.dnetlib.actionmanager.common.Provenance;
import eu.dnetlib.actionmanager.hbase.HBasePutFactory;

public class AtomicAction extends AbstractAction {

	private String targetRowKey;
	private String targetColumnFamily;
	private String targetColumn;

	private byte[] targetValue;

	public AtomicAction(final String rawSet, final Agent agent) {
		super(ACTION_TYPE.aac, rawSet, agent);
	}

	public AtomicAction(final String rawSet, final Agent agent, final Operation operation, final String targetRowKey, final String targetColumnFamily,
			final String targetColumn, final byte[] targetValue) {
		this(rawSet, agent);
		this.targetRowKey = targetRowKey;
		this.targetColumnFamily = targetColumnFamily;
		this.targetColumn = targetColumn;
		this.targetValue = targetValue;
	}

	public List<Put> asPutOperations(final String parentId, final Provenance provenance, final String trust, final String nsprefix) {
		final Put put = HBasePutFactory.createPutOperation(getRowKey(), getRawSet(), getAgent());

		put.add(ActionManagerConstants.SET_COLFAMILY, Bytes.toBytes(getRawSet()), Bytes.toBytes(getRawSet()));
		put.add(ActionManagerConstants.TARGET_COLFAMILY, ActionManagerConstants.TARGET_KEY_COL, Bytes.toBytes(targetRowKey));
		put.add(ActionManagerConstants.TARGET_COLFAMILY, ActionManagerConstants.TARGET_COLFAMILY_COL, Bytes.toBytes(targetColumnFamily));
		put.add(ActionManagerConstants.TARGET_COLFAMILY, ActionManagerConstants.TARGET_COL_COL, Bytes.toBytes(targetColumn));
		put.add(ActionManagerConstants.TARGET_COLFAMILY, ActionManagerConstants.TARGET_OAF_COL, targetValue);

		if ((parentId != null) && !parentId.isEmpty()) {
			put.add(ActionManagerConstants.RELATION_COLFAMILY, Bytes.toBytes(parentId), ActionManagerConstants.ISPARTOF);
		}

		return Lists.newArrayList(put);
	}

	public String getTargetRowKey() {
		return targetRowKey;
	}

	public void setTargetRowKey(final String targetRowKey) {
		this.targetRowKey = targetRowKey;
	}

	public String getTargetColumnFamily() {
		return targetColumnFamily;
	}

	public void setTargetColumnFamily(final String targetColumnFamily) {
		this.targetColumnFamily = targetColumnFamily;
	}

	public String getTargetColumn() {
		return targetColumn;
	}

	public void setTargetColumn(final String targetColumn) {
		this.targetColumn = targetColumn;
	}

	public byte[] getTargetValue() {
		return targetValue;
	}

	public void setTargetValue(final byte[] targetValue) {
		this.targetValue = targetValue;
	}

	public boolean isValid() {
		return ((getRowKey() != null) && !getRowKey().isEmpty() && (getRawSet() != null) && !getRawSet().isEmpty() && (targetRowKey != null)
				&& !targetRowKey.isEmpty() && (targetColumnFamily != null) && !targetColumnFamily.isEmpty() && (targetColumn != null)
				&& !targetColumn.isEmpty() && (targetValue != null) && (targetValue.length > 0));
	}

}
