package net.canadensys.dataportal.occurrence.controller.formatter;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.ResourceBundle;
import java.util.TimeZone;

import net.canadensys.dataportal.occurrence.model.OccurrenceModel;

import org.apache.commons.lang3.StringUtils;

/**
 * Formatter utility class until we have enough formatters to create more cohesive classes.
 * @author cgendreau
 *
 */
public class FormatterUtility {
	
	public static final DateFormat ISO_UTC_DATE_FORMAT;
	static{
		ISO_UTC_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
		ISO_UTC_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
	}
	
	public static final String CITATION_DATE_REGEX =  "[\\[{(]date[\\])}]";
	
	/**
	 * Build the Recommended Citation based on an OccurrenceModel
	 * As defined by https://github.com/Canadensys/canadensys-explorer/issues/28
	 * @param occModel
	 * @param urlToResource
	 * @param bundle
	 * @return
	 */
	public static String buildRecommendedCitation(OccurrenceModel occModel, String urlToResource, ResourceBundle bundle){
		String now = ISO_UTC_DATE_FORMAT.format(Calendar.getInstance().getTime());
		
		//if Bibliographiccitation is provided simply check if we have a 'date' variable to replace
		if(StringUtils.isNotBlank(occModel.getBibliographiccitation())){
			return StringUtils.replacePattern(occModel.getBibliographiccitation(), CITATION_DATE_REGEX, now);
		}
	
		String catalogNumber = occModel.getCatalognumber();
		String datasetName = occModel.getDatasetname();
		String institutionCode = occModel.getInstitutioncode();
		
		if(StringUtils.isNoneBlank(catalogNumber,datasetName,institutionCode)){
			return MessageFormat.format(bundle.getString("occpage.recommendedcitation.complete"), catalogNumber, datasetName, institutionCode, urlToResource, now);
		}
		
		if(StringUtils.isNoneBlank(catalogNumber,datasetName)){
			return MessageFormat.format(bundle.getString("occpage.recommendedcitation.missingic"), catalogNumber, datasetName, urlToResource, now);
		}
		
		if(StringUtils.isNoneBlank(catalogNumber,institutionCode)){
			return MessageFormat.format(bundle.getString("occpage.recommendedcitation.missingdn"), catalogNumber, institutionCode, urlToResource, now);
		}
		
		return "";
	}

}

@@ -220,35 +220,8 @@
 dle;
-%0Aimport java.util.TimeZone;
 %0A%0Aim
@@ -526,40 +526,8 @@
 mat 
-ISO_UTC_DATE_FORMAT;%0A%09static%7B%0A%09%09
 ISO_
@@ -580,78 +580,24 @@
 M-dd
-%22);%0A%09%09ISO_UTC_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone(%22UTC%22)
+ '(GMT'XX')'%22
 );%0A
-%09%7D
 %0A%09%0A%09

print GMT offset instead of UTC