package divconq.db.update;

import java.util.UUID;

import divconq.db.IDatabaseManager;
import divconq.struct.FieldStruct;
import divconq.struct.RecordStruct;
import divconq.struct.Struct;
import divconq.util.StringUtil;

/**
 * Use this field to update StaticList structures in dcDatabase.
 * 
 * @author Andy
 *
 */
public class ListField extends ScalarField {
	protected String subkey = null;
	
	/**
	 * @return subkey (id for this item in the list)
	 */
	public String getSubkey() {
		return this.subkey;
	}
	
	/**
	 * @param name of field
	 * @param subkey id for this item in the list (leave null to assign new id)
	 * @param value of field
	 * @param tags meta data for the field (alphanumeric and ':' are allowed in tag)
	 */
	public ListField(String name, String subkey, Object value, FieldTags tags) {
		super(name, value, tags);
		
		if (StringUtil.isEmpty(subkey))
			subkey = UUID.randomUUID().toString().replace("-", "");
		
		this.subkey = subkey;
	}
	
	/**
	 * @param name of field
	 * @param subkey id for this item in the list (leave null to assign new id)
	 * @param value of field
	 */
	public ListField(String name, String subkey, Object value) {
		super(name, value);
		
		if (StringUtil.isEmpty(subkey))
			subkey = UUID.randomUUID().toString().replace("-", "");
		
		this.subkey = subkey;
	}
	
	/**
	 * Mark a field as Retired.
	 * 
	 * @param name of field
	 * @param subkey id for this item in the list (leave null to assign new id)
	 * @param tags meta data for the field (alphanumeric and ':' are allowed in tag)
	 */
	public ListField(String name, String subkey, FieldTags tags) {
		super(name, tags);
		
		if (StringUtil.isEmpty(subkey))
			subkey = UUID.randomUUID().toString().replace("-", "");
		
		this.subkey = subkey;
	}
	
	/**
	 * Mark a field as Retired.
	 * 
	 * @param name of field
	 * @param subkey id for this item in the list (leave null to assign new id)
	 */
	public ListField(String name, String subkey) {
		super(name);
		
		if (StringUtil.isEmpty(subkey))
			subkey = UUID.randomUUID().toString().replace("-", "");
		
		this.subkey = subkey;
	}

	/* (non-Javadoc)
	 * @see divconq.db.update.ScalarField#convert(divconq.struct.RecordStruct, divconq.db.IDatabaseManager)
	 */
	@Override
	public Struct convert(RecordStruct fields, IDatabaseManager dbm) {
		RecordStruct data = new RecordStruct( 
				new FieldStruct("Data", dbm.convertValue(this.value).getResult())
		);
		
		if (this.tags != null)
			data.setField("Tags", this.tags);
		
		if (this.retired)
			data.setField("Retired", true);
		
		Struct ff = (fields != null) ? fields.getField(this.name) : null;
		
		// create a new one if needed, add to existing otherwise
		RecordStruct ret = (ff instanceof RecordStruct) ? (RecordStruct)ff : new RecordStruct();

		ret.setField(this.subkey, data);
		
		return ret;		
	}
}
