package com.k_int.ciim.ui.json;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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.ContextLinkHDO;
import com.k_int.ciim.data.hdo.MaintenanceHDO;
import com.k_int.ciim.data.hdo.ReferenceHDO;
import com.k_int.ciim.data.hdo.SchemaHDO;
import com.sun.jersey.api.view.ImplicitProduces;

@XmlRootElement(name="context_link") 
@ImplicitProduces("application/json;qs=5")
@XmlAccessorType(XmlAccessType.FIELD)
@Component
public class ContextLinkJSON extends AbstractJSON
{
	private String id = null;
	private ContextJSON context = null;
	private SchemaJSON schema = null;
	private String creator = null;
	private String created = null;
	private String last_author = null;
	private String last_updated = null;
	private String source_identifier = null;
	private String note = "";
	private String op_request = "";
	private String op_status = "";
	private Boolean changed = Boolean.FALSE;
	private List<RefLinkJSON> linked_objects = new ArrayList<RefLinkJSON>();
	private List<RefLinkJSON> linked_auths = new ArrayList<RefLinkJSON>();
	
	/* Constructor from HDO */
	public ContextLinkJSON(ContextLinkHDO hdo, SchemaHDO schema_hdo)
	{
		if(hdo != null)
		{
			this.id = hdo.getId().toString();
			
			this.source_identifier = hdo.getSourceIdentifier();
			
			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(schema_hdo != null)
			{
				this.schema = new SchemaJSON(schema_hdo);
			}
			if(hdo.getContext() != null)
			{
				this.context = new ContextJSON(hdo.getContext());
			}
			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()))
									{
										List<String> vals = augmented_data.getValues();
										vals.removeAll(Arrays.asList(""));
										field.setValues(vals);
									}
								}
							}
						}
					}
				}		
			}
			
			if(hdo.getLinkedAuths() != null && hdo.getLinkedAuths().size() > 0)
			{
				for(ReferenceHDO ref : hdo.getLinkedAuths())
				{
					linked_auths.add(new RefLinkJSON(ref));
				}
				
				Collections.sort(linked_auths); 
			}
			
			if(hdo.getLinkedObjects() != null && hdo.getLinkedObjects().size() > 0)
			{
				for(ReferenceHDO ref : hdo.getLinkedObjects())
				{
					linked_objects.add(new RefLinkJSON(ref));
				}
				
				Collections.sort(linked_objects); 
			}
		}
	}

	public String getId() 
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public ContextJSON getContext() 
	{
		return context;
	}

	public void setContext(ContextJSON context) 
	{
		this.context = context;
	}

	public SchemaJSON getSchema() 
	{
		return schema;
	}

	public void setSchema(SchemaJSON schema) 
	{
		this.schema = schema;
	}

	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 String getSourceIdentifier() 
	{
		return source_identifier;
	}

	public void setSourceIdentifier(String source_identifier)
	{
		this.source_identifier = source_identifier;
	}

	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 List<RefLinkJSON> getLinkedObjects()
	{
		return linked_objects;
	}

	public void setLinkedObjects(List<RefLinkJSON> linkedObjects) 
	{
		linked_objects = linkedObjects;
	}

	public List<RefLinkJSON> getLinkedAuths() 
	{
		return linked_auths;
	}

	public void setLinkedAuths(List<RefLinkJSON> linkedAuths)
	{
		linked_auths = linkedAuths;
	}
}
