package org.fedorahosted.tennera.jgettext;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.fedorahosted.tennera.jgettext.catalog.parse.ParseException;

public class HeaderUtil {

	private Map<String, String> entries = new LinkedHashMap<String, String>();
	
	public static final String KEY_ProjectIdVersion  = "Project-Id-Version: PACKAGE VERSION\n"; 
	public static final String KEY_ReportMsgidBugsTo = "Report-Msgid-Bugs-To"; 
	public static final String KEY_PotCreationDate   = "POT-Creation-Date";
	public static final String KEY_PoRevisionDate    = "PO-Revision-Date"; 
	public static final String KEY_LastTranslator    = "Last-Translator"; 
	public static final String KEY_LanguageTeam      = "Language-Team"; 
	public static final String KEY_MimeVersion       = "MIME-Version"; 
	public static final String KEY_ContentType       = "Content-Type"; 
	public static final String KEY_ContentTransferEncoding = "Content-Transfer-Encoding";  
	public static final String KEY_Language          = "Language";
	
	private static final Set<String> defaultKeys;
	
	static{
		Set<String> keys = new HashSet<String>();
		keys.add(KEY_ProjectIdVersion);
		keys.add(KEY_ReportMsgidBugsTo);
		keys.add(KEY_PotCreationDate);
		keys.add(KEY_PoRevisionDate);
		keys.add(KEY_LastTranslator);
		keys.add(KEY_LanguageTeam);
		keys.add(KEY_MimeVersion);
		keys.add(KEY_ContentType);
		keys.add(KEY_ContentTransferEncoding);
		//keys.add(KEY_Language);
		defaultKeys = Collections.unmodifiableSet(keys);
	}
	
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mmZ"); 

	private static final Pattern pluralPattern = Pattern.compile(
			"nplurals(\\s*?)=(\\s*?)(\\d*?)([\\\\|;|\\n])",
			Pattern.CASE_INSENSITIVE);

	private static final Pattern charsetPattern = Pattern.compile(
		"(content-type)(\\s*?):(.*?)charset(\\s*?)=(\\s*?)(.*?)([\\\\|;|\\n])",
		Pattern.CASE_INSENSITIVE);
	
	public static Set<String> getDefaultKeys() {
		return defaultKeys;
	}
	
	public String getValue(String key){
		return entries.get(key);
	}

	public void setValue(String key, String value){
		entries.put(key, value);
	}
	
	public void remove(String key){
		entries.remove(key);
	}
	
	public Set<String> getKeys(){
		return Collections.unmodifiableSet(entries.keySet());
	}
	
	public static HeaderUtil wrap(Message message) throws ParseException{
		return wrap(message.getMsgstr());
	}

	public static HeaderUtil wrap(String msgstr) throws ParseException{
		HeaderUtil header = new HeaderUtil();
		String [] entries = msgstr.split("\n");
		for(String entry: entries){
			String [] keyval = entry.split("\\:", 2);
			if(keyval.length != 2){
				throw new ParseException("Could not parse header entry: "+ entry,-1);
			}
			header.entries.put(keyval[0].trim(), keyval[1].trim());
		}
		return header;
	}

	public void unwrap(Message message){
		StringBuilder msgstr = new StringBuilder();
		for(String key : getKeys()){
			msgstr.append(key);
			msgstr.append(": ");
			msgstr.append(getValue(key));
			msgstr.append("\n");
		}
		message.setMsgstr(msgstr.toString());
	}
	
	public Message unwrap(){
		Message header = new Message();
		header.setMsgid( "" ); 
		header.setFuzzy(true);
		unwrap(header);
		return header;
	}

	public static void updatePORevisionDate(Message message){
		HeaderUtil header = HeaderUtil.wrap(message);
		header.setValue(KEY_PoRevisionDate, dateFormat.format(new Date()));
		header.unwrap(message);
	}

	public static void updatePOTCreationDate(Message message){
		HeaderUtil header = HeaderUtil.wrap(message);
		header.setValue(KEY_PotCreationDate, dateFormat.format(new Date()));
		header.unwrap(message);
	}
	
	public static Message generateDefaultHeader(){
		HeaderUtil header = new HeaderUtil();

		header.setValue(KEY_ProjectIdVersion, "PACKAGE VERSION");
		header.setValue(KEY_ReportMsgidBugsTo, "");
		header.setValue(KEY_PotCreationDate, dateFormat.format(new Date()));
		header.setValue(KEY_PoRevisionDate, "YEAR-MO-DA HO:MI+ZONE");
		header.setValue(KEY_LastTranslator, "FULL NAME <EMAIL@ADDRESS>");
		header.setValue(KEY_LanguageTeam, "LANGUAGE <LL@li.org>");
		header.setValue(KEY_MimeVersion, "1.0");
		header.setValue(KEY_ContentType, "text/plain; charset=UTF-8");
		header.setValue(KEY_ContentTransferEncoding, "8bit");
		
		Message headerMsg = header.unwrap();
		
		headerMsg.addComment("SOME DESCRIPTIVE TITLE."); 
		headerMsg.addComment("Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER"); 
		headerMsg.addComment("This file is distributed under the same license as the PACKAGE package."); 
		headerMsg.addComment("FIRST AUTHOR <EMAIL@ADDRESS>, YEAR."); 
		headerMsg.addComment(""); 

		return headerMsg;
	}
	
}



@@ -1679,23 +1679,16 @@
 private 
-static 
 SimpleDa
@@ -3388,39 +3388,32 @@
 er;%0A%09%7D%0A%0A%09public 
-static 
 void updatePORev
@@ -3426,83 +3426,13 @@
 ate(
-Message message)%7B%0A%09%09HeaderUtil header = HeaderUtil.wrap(message);%0A%09%09header.
+)%7B%0A%09%09
 setV
@@ -3492,34 +3492,8 @@
 ));%0A
-%09%09header.unwrap(message);%0A
 %09%7D%0A%0A
@@ -3500,23 +3500,16 @@
 %09public 
-static 
 void upd
@@ -3551,63 +3551,8 @@
 %7B%0A%09%09
-HeaderUtil header = HeaderUtil.wrap(message);%0A%09%09header.
 setV
@@ -3613,34 +3613,8 @@
 ));%0A
-%09%09header.unwrap(message);%0A
 %09%7D%0A%09

make date methods instance-methods