package com.k_int.aggr2.mimsy.solr;

import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.solr.common.SolrInputDocument;

import com.k_int.aggr2.mimsy.data.ControlledReferenceDTO;
import com.k_int.aggr2.mimsy.data.DescriptionDTO;
import com.k_int.aggr2.mimsy.data.InstructionDTO;
import com.k_int.aggr2.mimsy.data.MimsyDataDTO;
import com.k_int.aggr2.mimsy.data.MimsyDateDTO;
import com.k_int.aggr2.mimsy.data.MimsyMediaDataDTO;
import com.k_int.aggr2.mimsy.data.MimsyObjectDataDTO;
import com.k_int.aggr2.mimsy.data.NameDTO;
import com.k_int.aggr2.mimsy.data.RightsDTO;
import com.k_int.aggr2.mimsy.data.TextTypePairDTO;
import com.k_int.aggr2.mimsy.data.TitleDTO;
import com.k_int.aggr2.mimsy.data.conversion.AbstractObjectFormatConverter;
import com.k_int.mimsy.ref.DataException;
import com.k_int.mimsy.ref.MediaRecordTypeEnum;

public class SolrObjectConverter extends AbstractObjectFormatConverter {
	private SolrInputDocument doc;
	private Properties solr_props;
	
	public SolrObjectConverter(SolrInputDocument doc, Properties solr_props)
	{
		this.doc=doc;
		this.solr_props=solr_props;
	}
	@Override
	protected void convertControlledRefs(String prefix,
			Set<ControlledReferenceDTO> controlled_refs) throws DataException {
		if(controlled_refs.size()==0)
			return;
			       
    	Iterator<ControlledReferenceDTO> i = controlled_refs.iterator();
    	while(i.hasNext())
    	{
    		ControlledReferenceDTO ref = i.next();
    		if(ref.getIdentifier()==null)
    			continue;
    		
    		convertReference(prefix+"Reference","urn://mol/"+ref.getIdentifier());
    		if(prefix.equals("mo:maker"))
    			SolrHelper.addField(solr_props,doc,"mo:makerString", null, ref.getText(),true);
    			
            String rel ="undefined";
            if(ref.getRelationship()!=null)
            	rel=ref.getRelationship();
            
            convert(prefix+"Relation",rel);
    	}
	}

	@Override
	protected void convertDate(String date_str, Set<MimsyDateDTO> dates)
			throws DataException {

		if(dates.size()==0)
			return;
		
     	Iterator<MimsyDateDTO> i = dates.iterator();
     	while(i.hasNext())
     	{
        	MimsyDateDTO date = i.next();	        	
     		convert(date_str,date.getDateText());
     		convert(date_str+"Earliest",date.getEarliest());
     		convert(date_str+"Latest",date.getLatest());
     		convert(date_str+"Method",date.getMethod());
     		convert(date_str+"Era",date.getEra());
     		String rel = "undefined" 	;
     		if(date.getRelation()!=null)
     			rel=date.getRelation();
     		
     		convert(date_str+"Relation",rel);
     		    
     		//convert("mo:prior",Boolean.toString(date.getPrior()));
 			SolrHelper.writeAttribution(solr_props,doc, date_str, date.getAttribution());  		            		
     	}	     		
	}

	@Override
	protected void convertDescriptions(Set<DescriptionDTO> descriptions)
			throws DataException {
		if(descriptions.size()==0)
			return;
		
        Iterator<DescriptionDTO> i = descriptions.iterator();
        while(i.hasNext())
        {
        	DescriptionDTO description = i.next();
    		if(description.getDescription()==null)
    			continue;
        	convert("mo:otherDescription",description.getDescription());
        	String type="undefined";
        	if(description.getType()!=null)
        		type=description.getType();
        	convert("mo:otherDescriptionType",type);
        	//convert("mo:prior",Boolean.toString(description.getPrior()));
        	convert("mo:descriptionAuthor",description.getSource());
        	convert("mo:descriptionDate",description.getDate());   		
        }	      		
	}

	@Override
	protected void convertFunctionalClassification(Set<NameDTO> functional_classification)
			throws DataException {
		if(functional_classification.size()==0)
			return;
		
         Iterator<NameDTO> i = functional_classification.iterator();
         while(i.hasNext())
         {
         	NameDTO name = i.next();
         	if(name.getName()==null)
         		continue;
         		   
         	convert("mo:functionalClassification",name.getName());
         	String type="undefined";
         	if(name.getType()!=null)
         		type=name.getType();
         	
         	convert("mo:functionalClassificationType",type);
         	//convert("mo:prior",Boolean.toString(name.getPrevious()));
         }
	}

	@Override
	protected void convertMediaLinks(Map<Integer, MimsyMediaDataDTO> linked_media)
			throws DataException {
		if(linked_media.size()==0)
			return;
        
        	Iterator<Map.Entry<Integer, MimsyMediaDataDTO>> i = linked_media.entrySet().iterator();
        	while(i.hasNext())
        	{
        		Map.Entry<Integer, MimsyMediaDataDTO> entry =i.next();
        		MimsyMediaDataDTO ref = entry.getValue();
        		Integer key=entry.getKey();
        		convertReference("mo:mediaReference","urn://mol/"+ref.getMimsyIdentifier());
	            if(key!=null)
        			convert("mo:mediaIndex",key.toString());
                if(ref.getRecordType()==MediaRecordTypeEnum.IMAGE)
                {
                	convert("mo:previewMediaLocation",ref.getPreviewRelativePath());
                	convert("mo:midMediaLocation",ref.getMidRelativePath());
                	convert("mo:largeMediaLocation",ref.getLargeRelativePath());
                	convert("mo:originalMediaLocation",ref.getLargeDonorRelativePath());	 	              
                }
                else if(ref.getDonorRelativePath()!=null)
                {
                	convert("mo:originalMediaLocation",ref.getDonorRelativePath());
                }
                String rel="undefined";
                if(ref.getRelationship()!=null)
                	rel=ref.getRelationship();
                convert("mo:mediaRelation",rel);
        	}
        
	}

	@Override
	protected void convertNames(Set<NameDTO> names) throws DataException {
		if(names.size()==0)
			return;
		
	     Iterator<NameDTO> i = names.iterator();
	     while(i.hasNext())
	     {
	     	NameDTO name = i.next();
	     	if(name.getName()==null)
	     		continue;	     		    
	     		
	     	convert("mo:name",name.getName());
	     	String type="undefined";
	     	if(name.getType()!=null)
	     		type=name.getType();
	     	convert("mo:nameType",type);
	     	//convert("mo:prior",Boolean.toString(name.getPrevious()));      		
	     
	     }
	}

	@Override
	protected void convertOtherNumbers(Set<TextTypePairDTO> other_numbers)
			throws DataException {
		if(other_numbers.size()==0)
			return;
		
        Iterator<TextTypePairDTO> i = other_numbers.iterator();
        while(i.hasNext())
        {
        	TextTypePairDTO pair = i.next();        		
        	convert("mo:otherNumber",pair.getText());  
        	String type="undefined";
        	if(pair.getType()!=null)
        		type=pair.getType();
        	convert("mo:otherNumberType",type);
        	
        }
	}

	@Override
	protected void convertOtherTitles(Set<TitleDTO> other_titles) throws DataException {
		if(other_titles.size()==0)
			return;
		
        Iterator<TitleDTO> i = other_titles.iterator();
        while(i.hasNext())
        {
        	TitleDTO title = i.next();
        	if(title.getTitle()==null)
        		continue;
        		
        	convert("mo:otherTitle",title.getTitle());
        	String type="undefined";
        	if(title.getType()!=null)
        		type=title.getType();
        	convert("mo:otherTitleType",type);
        	//convert("mo:prior",Boolean.toString(title.getPrior()));
        	
        	SolrHelper.writeAttribution(solr_props,doc, "otherTitle", title.getAttribution());
        	
        }
	}

	@Override
	protected void convertRights(RightsDTO rights) throws DataException {
		if(rights==null)
			return;
		
         convert("mo:rightsCreditLine",rights.getCreditLine());
         convert("mo:rightsDetails",rights.getDetails());
         convert("mo:rightsFromDate",rights.getFromDate());
         convert("mo:rightsToDate",rights.getToDate());
         String type="undefined";
         if(rights.getType()!=null)
         	type=rights.getType();
         convert("mo:rightsType",type);
         String holder = rights.getHolder();
         if(holder!=null && holder.trim().equals("-"))
        	 holder="digital image copyright Museum of London";
         convert("mo:rightsHolder",holder);      
	}

	@Override
	protected void convertStringList(String name, Set<String> data)
			throws DataException {
		
		Iterator<String> m = data.iterator();
		while(m.hasNext())
		{			
			convert(name,m.next()); 					
		}		
	}

	@Override
	protected void convert(String elementVal, String val) throws DataException {
				
		if(elementVal.equals("mo:idNumber")) // add these specificalyl to the general text field
		{
			SolrHelper.addField(solr_props,doc,elementVal, null, val,true);
		}
		else if(elementVal.equals("mo:name"))
		{
			SolrHelper.addField(solr_props,doc,elementVal, null, val,true);
		}
		else if(elementVal.equals("mo:material"))
		{
			SolrHelper.addField(solr_props,doc,elementVal, null, val,true);
		}
		else if(elementVal.equals("mo:otherDescription"))
		{
			SolrHelper.addField(solr_props,doc,elementVal, null, val,true);
		}
		else if(elementVal.equals("mo:title"))
		{
			SolrHelper.addField(solr_props,doc,elementVal, null, val,true);
		}
		else // dont add to general text field
			SolrHelper.addField(solr_props,doc,elementVal, null, val,null);

	}

	@Override
	protected void convertConfAndLastUpdate(MimsyDataDTO data)
			throws DataException {
		SolrHelper.addConfAndLastUpdate(solr_props,doc,data);

	}

	@Override
	protected void convertInstructions(Set<InstructionDTO> instructions)
			throws DataException {
		//not used

	}

	@Override
	public void prefix(MimsyObjectDataDTO Type) throws DataException {
		// not used

	}

	@Override
	public void suffix(MimsyObjectDataDTO Type) throws DataException {
		// not used

	}
	
	public void convertReference(String element, String val) throws DataException {
		
		SolrHelper.addField(solr_props,doc,element, null, val,false);
		
	}
	
}
