package com.k_int.ciim.json;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.springframework.stereotype.Component;

import com.sun.jersey.api.view.ImplicitProduces;

@XmlRootElement
@ImplicitProduces("application/json;qs=5")
@XmlAccessorType(XmlAccessType.FIELD)
@Component
public class CIIMGroupJSON {

  public static final int HIERARCHY_NONE = 0;
  public static final int HIERARCHY_NODE = 1;
  public static final int HIERARCHY_TOP = 2;
  
  public String gid = null;
  public String mimsy_identifier = null;
  public String group_name = null;
  public int hier = HIERARCHY_NONE;
  public String owner = null;
  public String items = "0";
  public String date = null;
  public String purpose = null;
  public String valid = null;
  public String published = null;
  public String top_group_editorial = null;
  public SchemaDefinitionJSON schema = null;
  public SchemaDefinitionJSON object_schema = null;
  public String description = null;
  public String long_description = null;
  public String origin = null;
  public HashMap<String, ArrayList<String>> relationships = null;
  public HashMap<String, Long> positions = null;
  public boolean user_can_edit = false;
  public List<CIIMUserJSON> ciim_owners = null;
  public String data_status = null;
  
  public List<CIIMGroupAttachmentJSON> attachments = new ArrayList<CIIMGroupAttachmentJSON>();
  
  @XmlTransient
  public SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy '<I>'HH:mm:ss'</I>'");

  public CIIMGroupJSON() {
  }

  public CIIMGroupJSON(String gid,
	                   String group_name,
	                   int hier,
	                   String owner,
	                   String items,
	                   Date date,
	                   String purpose,
	                   String valid,
	                   String published ,
	                   String description,
	                   String long_description,
	                   String origin,
	                   String mimsy_identifier,
	                   String top_group_editorial,
	                   boolean user_can_edit,
	                   List<CIIMUserJSON> ciim_owners,
	                   String data_status)
  {
    this.gid = gid;
    this.group_name = group_name;
    this.hier = hier;
    this.owner = owner;
    this.items = items;
    
    this.date = format.format(date);
    
    this.purpose = purpose;
    this.valid = valid;
    this.published = published;    
    this.description = description;
    this.long_description = long_description;
    this.origin = origin;
    this.mimsy_identifier = mimsy_identifier;
    this.top_group_editorial = top_group_editorial;
    this.user_can_edit = user_can_edit;
    this.ciim_owners = ciim_owners;
    this.data_status = data_status;
  }
  
  public void addRelationship(String top_level_gid, String child_gid)
  {
	  if(top_level_gid != null && top_level_gid.length() > 0 && child_gid != null && child_gid.length() > 0)
	  {
		  ArrayList<String> lChildren = null;
		  
		  if(this.relationships == null)
		  {
			  this.relationships = new HashMap<String, ArrayList<String>>();
		  }

		  if(this.relationships.containsKey(top_level_gid))
		  {
			  lChildren = (ArrayList<String>) this.relationships.get(top_level_gid);  
		  }

		  if(lChildren == null)
		  {
			  lChildren = new ArrayList<String>();
		  }	

		  lChildren.add(child_gid);
	
		  this.relationships.put(top_level_gid, lChildren);
	  }
  }
  
  public void addPosition(String top_level_gid, Long position)
  {
	  if(top_level_gid != null && position != null)
	  {
		  if(this.positions == null)
		  {
			  this.positions = new HashMap<String, Long>();
		  }	  
		  this.positions.put(top_level_gid, position);
	  }
  }
  
  public Long getPosition(String top_level_gid)
  {
	  Long lReturn = null;
	  
	  if(this.positions != null && this.positions.size() > 0)
	  {
		  if(top_level_gid != null && top_level_gid.length() > 0)
		  {
			  lReturn = this.positions.get(top_level_gid);  
		  }
	  }
	  
	  return lReturn;
  }
  
  public void setAttachments( List<CIIMGroupAttachmentJSON> attachments)
  {
	  this.attachments = attachments;
  }
  
}
