package com.k_int.ciim.ui.json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.springframework.stereotype.Component;

import com.k_int.ciim.data.hdo.AugmentationItemHDO;
import com.k_int.ciim.data.hdo.ContextHDO;
import com.k_int.ciim.data.hdo.ContextSchemaHDO;
import com.k_int.ciim.data.hdo.MaintenanceHDO;
import com.sun.jersey.api.view.ImplicitProduces;

@XmlRootElement(name="context") 
@ImplicitProduces("application/json;qs=5")
@XmlAccessorType(XmlAccessType.FIELD)
@Component
public class ContextJSON extends AbstractJSON
{

	private String id = null;
	private String name = null;
	private String description = null;
	private String creator = "unknown";
	private String created = null;
	private String last_author = "unknown";
	private String last_updated = null;
	private String purpose = null;
	
	private SchemaJSON schema = null;
	private String note = "";
	private String op_request = "";
	private String op_status = "";
	private Boolean changed = Boolean.FALSE;
	private Boolean is_global = Boolean.FALSE;
	
	private Map<String, ContextSchemaJSON> assigned_schemas = new HashMap<String, ContextSchemaJSON>();
	
	//required for workbench
	private String source_identifier = null;
	
	protected ContextJSON(){;}
	
	public ContextJSON(ContextHDO hdo)
	{
		if(hdo != null)
		{
			this.id = hdo.getId().toString();
			
			this.source_identifier = "context-ciim-" + this.id;
			
			this.name = hdo.getName();
			this.description = hdo.getDescription();
			this.purpose = hdo.getPurpose();
			
			//global never has a creator as it is created on deployment by system thus:
			if(hdo.getIsGlobal())
			{
				this.creator = "ciim";
				this.last_author = "ciim";
				this.is_global = hdo.getIsGlobal();
			}
			
			if(hdo.getCreationDate() != null)
			{
				this.created = DEFAULT_FORMAT.format(hdo.getCreationDate());
			}
			if(hdo.getCreator() != null)
			{
				this.creator = hdo.getCreator().getName();
			}
			
			if(hdo.getMaintenanceData() != null)
			{
				MaintenanceHDO maint_hdo = hdo.getMaintenanceData();
				
				this.note = maint_hdo.getNote();
				
				if(maint_hdo.getOpRequest() != null)
				{
					this.op_request = maint_hdo.getOpRequest().toString();
				}
				if(maint_hdo.getOpStatus() != null)
				{
					this.op_status = maint_hdo.getOpStatus().toString();
				}
				if(maint_hdo.getLastUpdated() != null)
				{
					this.last_updated = DEFAULT_FORMAT.format(maint_hdo.getLastUpdated());
				}
				if(maint_hdo.getLastAuthor() != null)
				{
					this.last_author = maint_hdo.getLastAuthor().getName();
				}
				if(maint_hdo.getChanged() != null)
				{
					this.changed = maint_hdo.getChanged();				
					
					if(this.op_request.length() == 0)
					{
						this.op_request = "none (unpublished changes)";
					}
				}
			}
			if(hdo.getSchema() != null)
			{
				this.schema = new SchemaJSON(hdo.getSchema());
			}
			if(schema != null && hdo.getAugmentedData() != null && hdo.getAugmentedData().size() > 0)	
			{
				for(AugmentationItemHDO augmented_data : hdo.getAugmentedData())
				{
					if(augmented_data != null)
					{
						for(SchemaSectionJSON section : schema.getOrderedSections())	
						{
							for(SchemaElementJSON field : section.getOrderedFields())
							{
								if(field != null && field.getId() != null)
								{
									if(augmented_data.getDefinition().getId().toString().equalsIgnoreCase(field.getId()))
									{
										field.setValues(augmented_data.getValues());
									}
								}
							}
						}
					}
				}
			}
		}
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id) 
	{
		this.id = id;
	}

	public String getName() 
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getDescription() 
	{
		return description;
	}

	public void setDescription(String description) 
	{
		this.description = description;
	}

	public Map<String, ContextSchemaJSON> getAssignedSchemas() 
	{
		return assigned_schemas;
	}

	public void setAssignedSchemas(Map<String, ContextSchemaJSON> assigned_schemas) 
	{
		this.assigned_schemas = assigned_schemas;
	}
	
	public void setAssignedSchemas(List<ContextSchemaHDO> schemas)
	{
		if(schemas != null && schemas.size() > 0)
		{
			for(ContextSchemaHDO context_schema_hdo : schemas)
			{
				if(context_schema_hdo != null && context_schema_hdo.getMuseumDataType() != null)
				{
					this.assigned_schemas.put(context_schema_hdo.getMuseumDataType().toString(), new ContextSchemaJSON(context_schema_hdo));
				}
			}
		}
	}

	public String getCreator() 
	{
		return creator;
	}

	public void setCreator(String creator) 
	{
		this.creator = creator;
	}

	public String getCreated() 
	{
		return created;
	}

	public void setCreated(String created) 
	{
		this.created = created;
	}

	public String getLastAuthor()
	{
		return last_author;
	}

	public void setLastAuthor(String lastAuthor) 
	{
		last_author = lastAuthor;
	}

	public String getLastUpdated() {
		return last_updated;
	}

	public void setLastUpdated(String lastUpdated) 
	{
		last_updated = lastUpdated;
	}

	public SchemaJSON getSchema()
	{
		return schema;
	}

	public void setSchema(SchemaJSON schema) 
	{
		this.schema = schema;
	}

	public String getNote() 
	{
		return note;
	}

	public void setNote(String note)
	{
		this.note = note;
	}

	public String getOpRequest()
	{
		return op_request;
	}

	public void setOpRequest(String opRequest) 
	{
		op_request = opRequest;
	}

	public String getOpStatus() 
	{
		return op_status;
	}

	public void setOpStatus(String opStatus) 
	{
		op_status = opStatus;
	}

	public Boolean getChanged()
	{
		return changed;
	}

	public void setChanged(Boolean changed) 
	{
		this.changed = changed;
	}

	public String getPurpose() 
	{
		return purpose;
	}

	public void setPurpose(String purpose)
	{
		this.purpose = purpose;
	}

	public String getSourceIdentifier() 
	{
		return source_identifier;
	}

	public void setSourceIdentifier(String sourceIdentifier) 
	{
		source_identifier = sourceIdentifier;
	}

	public Boolean getIsGlobal()
	{
		return is_global;
	}

	public void setIsGlobal(Boolean isGlobal) 
	{
		is_global = isGlobal;
	}
}