package com.rjconsultores.ventaboletos.web.gui.componente.esquemaoperacional;

import java.math.BigDecimal;

import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.web.utilerias.ConstraintNumberBD;
import com.rjconsultores.ventaboletos.web.utilerias.StringDecimalToDecimalConverter;

public class MyCustomTextboxDecimal extends Textbox {

	private static final long serialVersionUID = 1L;

	public MyCustomTextboxDecimal() {
		super();
		this.setConstraint(new ConstraintNumberBD(8, 2, true));
		ejecutaJavaScript();
	}

	public void setValueBigDecimal(BigDecimal kmPagoConductor) {
		this.setValue(getBigDecimal(kmPagoConductor));

	}

	public MyCustomTextboxDecimal(String value) throws WrongValueException {
		super(value);
		this.setConstraint(new ConstraintNumberBD(8, 2, true));
		ejecutaJavaScript();
	}

	public MyCustomTextboxDecimal(BigDecimal value) throws WrongValueException {
		super();
		this.setValue(getBigDecimal(value));
		this.setConstraint(new ConstraintNumberBD(8, 2, true));
		ejecutaJavaScript();
	}

	private void ejecutaJavaScript() {
		Clients.evalJavaScript("jQuery('#" + this.getUuid() + "').format({allow_negative:false,precision: 2,decimal:',',autofix:true});");
	}

	private String getBigDecimal(BigDecimal val) {
		return (String) new StringDecimalToDecimalConverter().coerceToUi(val, this);

	}

	public BigDecimal getValueBigDecimal() {
		return getBigDecimal(this.getValue());
	}

	private BigDecimal getBigDecimal(String val) {
		return (BigDecimal) new StringDecimalToDecimalConverter().coerceToBean(val, this);
	}
}