package com.k_int.ciim.json;

import java.util.ArrayList;
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.aggr2.mimsy.data.hdo.SchemaDefinitionHDO;
import com.k_int.aggr2.mimsy.data.hdo.SchemaElementHDO;
import com.k_int.aggr2.mimsy.data.hdo.SchemaOptionValueHDO;
import com.sun.jersey.api.view.ImplicitProduces;

@XmlRootElement
@ImplicitProduces("application/json;qs=5")
@XmlAccessorType(XmlAccessType.FIELD)
@Component
public class SchemaDefinitionJSON 
{
	private String title;
	private String description;
	private String mimsy_data_type;
	private Long id;
	private List<SchemaElementJSON> options;
	
	protected SchemaDefinitionJSON(){;}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String title) 
	{
		this.title = title;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description) 
	{
		this.description = description;
	}

	public Long getId()
	{
		return id;
	}

	public void setId(Long id) 
	{
		this.id = id;
	}
	
	public static SchemaDefinitionJSON schemaDefinitionHDOtoJSON(SchemaDefinitionHDO hdo)
	{
		SchemaDefinitionJSON json = new SchemaDefinitionJSON();
		
		if(hdo != null)
		{
			json.setDescription(hdo.getDescription());
			json.setTitle(hdo.getTitle());
			json.setId(hdo.getId());
			json.setMimsyDataType(hdo.getApplicableMimsyDataType().toString());
			
			List<SchemaElementJSON> options_list = new ArrayList<SchemaElementJSON>();
			
			if(hdo.getSchemaElements() != null && hdo.getSchemaElements().size() > 0)
			{
				for(SchemaElementHDO element : hdo.getSchemaElements())
				{
					if(element != null)
					{
						options_list.add(SchemaElementJSON.schemaElementHDOtoJSON(element));
					}
				}
			}
			
			json.setOptions(options_list);
		}
		return json;
	}
	
	public static SchemaDefinitionJSON schemaDefinitionHDOtoJSON(SchemaDefinitionHDO hdo, Map<SchemaElementHDO, SchemaOptionValueHDO> values)
	{
		SchemaDefinitionJSON json = new SchemaDefinitionJSON();
		
		if(hdo != null)
		{
			json.setDescription(hdo.getDescription());
			json.setTitle(hdo.getTitle());
			json.setId(hdo.getId());
			json.setMimsyDataType(hdo.getApplicableMimsyDataType().toString());
			
			List<SchemaElementJSON> options_list = new ArrayList<SchemaElementJSON>();
			
			if(hdo.getSchemaElements() != null && hdo.getSchemaElements().size() > 0)
			{
				for(SchemaElementHDO element : hdo.getSchemaElements())
				{
					if(element != null)
					{
						options_list.add(SchemaElementJSON.schemaElementHDOtoJSON(element, values.get(element)));
					}
				}
			}
				
			json.setOptions(options_list);
		}
		return json;
	}

	public List<SchemaElementJSON> getOptions() 
	{
		return options;
	}

	public void setOptions(List<SchemaElementJSON> options) 
	{
		this.options = options;
	}

	public String getMimsyDataType()
	{
		return mimsy_data_type;
	}

	public void setMimsyDataType(String mimsy_data_type)
	{
		this.mimsy_data_type = mimsy_data_type;
	}
}
