package pt.link.utils.format;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class FormElementMetaData {
	static Log log = LogFactory.getLog(FormElementMetaData.class);
	
	public String name;
	public String type;
	public int length;
	public String label;
	public String format;
	
	/**
	 * @throws PaddingException 
	 * 
	 */
	public String pad(Object value) throws PaddingException{
		String padded;
		
		if(type.equals("numeric")){
			DecimalFormat formatter = new DecimalFormat(this.format);
			formatter.getDecimalFormatSymbols().setDecimalSeparator('.');
			
			DecimalFormatSymbols newSymbols = formatter.getDecimalFormatSymbols();
			newSymbols.setDecimalSeparator('.');
			formatter.setDecimalFormatSymbols(newSymbols);
			String formatted;
			if(value instanceof String){
				formatted = formatter.format(new Double(value.toString()));
			}
			else{
				formatted = formatter.format(value);
			}
			
			int dotIndex = formatted.indexOf(newSymbols.getDecimalSeparator());
			if(dotIndex != -1){
				StringBuffer sb = new StringBuffer(formatted);
				sb.deleteCharAt(dotIndex);
				return sb.toString();
			}
				
			return formatted;
		}
		else if(type.equals("character")){
			padded = ""+value;
			padded = padded.trim();
		}
		else if(type.equals("date")){
			if(this.format == null){
				String messageError = "Format not found for element " + this.name;
				log.error(messageError);
				throw new PaddingException(messageError);
			}
			DateFormat formater = new SimpleDateFormat(this.format);
			String str = formater.format((Date) value);
			return str;
		}
		else{
			String messageError = "Element type not supported " + this.name;
			log.error(messageError);
			throw new PaddingException(messageError);
		}
		
		return padded;
	}
	
	/**
	 * @param value
	 * @return
	 * @throws PaddingException 
	 */
	public Object unParse(String value) throws PaddingException {
		try {
			if (type.equals("numeric")) {
				if (this.format == null) {
					String messageError = "Format not found for element "
							+ this.name;
					log.error(messageError);
					throw new PaddingException(messageError);
				}
				DecimalFormat formatter = new DecimalFormat(this.format);
				DecimalFormatSymbols newSymbols = formatter.getDecimalFormatSymbols();
				newSymbols.setDecimalSeparator('.');
				formatter.setDecimalFormatSymbols(newSymbols);
				
				return formatter.parse(value);
			} else if (type.equals("character")) {
				return value;
			} else if (type.equals("date")) {
				if (this.format == null) {
					String messageError = "Format not found for element "
							+ this.name;
					log.error(messageError);
					throw new PaddingException(messageError);
				}
				DateFormat formatter = new SimpleDateFormat(this.format);
				Date date;
				date = formatter.parse(value);
				return date;
			} else {
				String messageError = "type not supported"
					+ this.name;
				log.error(messageError);
				throw new PaddingException(messageError);
			}
		} catch (ParseException e) {
			String messageError = "ParseException caught while padding element "
					+ this.name;
			log.error(messageError, e);
			throw new PaddingException(messageError);
		}
	}
	
	
	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("[");
		buf.append("name: ");buf.append(this.name);buf.append(",");
		buf.append("label: ");buf.append(this.label);buf.append(",");
		buf.append(" type: ");buf.append(this.type);buf.append(",");
		buf.append(" length: ");buf.append(this.length);
		buf.append("]");
		return buf.toString();
	}
	
	private String removeDot(String str){
		StringBuffer buf = new StringBuffer(str);
		for(int j = 0;j<str.length();j++){
			if(str.charAt(j) == '.')
				buf.deleteCharAt(j);
		}
		return buf.toString();
	}
	
	public static void main2(String[] args) throws PaddingException {
		Integer i = new Integer(126);
		Double d = new Double(1.25);
		String str = i.toString();
		FormElementMetaData aElement = new FormElementMetaData();
		str = aElement.removeDot(str);
		
		
		str = d.toString();
		str = aElement.removeDot(str);
		
		aElement.label="message";
		aElement.type="numeric";
		aElement.length=9;
		aElement.name="ola";
		String padded = aElement.pad("12.3");
		
		FormElementMetaData aCElement = new FormElementMetaData();
		aCElement.label="message";
		aCElement.type="character";
		aCElement.length=9;
		aCElement.name="ola";
		padded = aCElement.pad(" 12 3 ");
	}
	
	public static void main(String[] args) throws ParseException {
		DecimalFormat formatter = new DecimalFormat("000000.00");
		double numberInt = 12.5;
		double numberDouble = 125.998;
		System.out.println(formatter.format(numberInt));
		System.out.println(formatter.format(numberDouble));
		String a = formatter.format(numberDouble);
		System.out.println(formatter.parse(a).getClass().getName());
		System.out.println((Double)formatter.parse(a));

	}
	
}
