/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.corejsf;

import java.io.IOException;
import java.util.Map;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.convert.IntegerConverter;

/**
 *
 * @author CNOC
 */
public class UISpinner extends UIInput {

    private static String MORE = ".more";
    private static String LESS = ".less";

    public UISpinner() {
        setConverter(new IntegerConverter()); //moze i bez ovoga,sama klasa UIInput u trecoj
        //fazi kada se radi konverzija pokusava da dohvati converter a ako ga nema na osnovu
        //toga kakav je bin property povezan sa value instancira konverter
        //ako u decode metodi value ne sacuvamo kao string nego kao neku drugu klasu 
        //podrazumeva se da nije potrebno pozivati konverter

        setRendererType(null);
    }

    public void encodeBegin(FacesContext context) throws IOException {
        //dohvatamo atribute spinera
        ResponseWriter writer = context.getResponseWriter();
        Integer min = (Integer) getAttributes().get("minimum");
        Integer max = (Integer) getAttributes().get("maximum");
        Integer size = (Integer) getAttributes().get("size");
        Boolean readonly = (Boolean) getAttributes().get("readonly");
        Integer step = (Integer) getAttributes().get("step");
        if (step == null) {
            step = new Integer(10);
        }
        String clientId = getClientId(context);
        String id = getId();
        //ubacujemo skript fju koja treba da uvecava spiner za odredjeni korak na pritisak dugmica
        System.out.println("promena" + id + "(increment)");
        writer.write("<script type=\"text/javascript\">");

        writer.write("function promena" + id + "(increment){");
        if (min != null) {
            writer.write("var min=" + min.intValue() + ";");
        } else {
            writer.write("var min=-100;");
        }
        if (max != null) {
            writer.write("var max=" + max.intValue() + ";");
        } else {
            writer.write("var max=100;");
        }
        writer.write("var spinner=document.getElementById(\"" + clientId + "\");");
        //   if(min!=null) writer.write("spinner.min="+min.intValue()+";");
        //  else writer.write("spinner.min=-100;");
        // if(max!=null) writer.write("spinner.max="+max.intValue()+";");
        //else writer.write("spinner.max=100;");
        writer.write("var vr=parseInt(spinner.value);");
        writer.write("if(isNaN(vr))return;");
        writer.write("vr+=increment;");
        //   writer.write("if('min' in spinner && vr<spinner.min) return;");
        //  writer.write("if('max' in spinner && vr>spinner.max) return;");
        writer.write("if(vr<min) return;");
        writer.write("if(vr>max) return;");
        writer.write("spinner.value=vr; }");

        writer.write("</script>");
        //rendanje elemenata tekst polje i dva dugmeta
        writer.startElement("input", this);
        writer.writeAttribute("type", "text", null);
        writer.writeAttribute("name", clientId, null);
        writer.writeAttribute("id", clientId, null);
        Object v = getValue();
        if (v != null) {
            writer.writeAttribute("value", v.toString(), null);
        }
        if (size != null) {
            writer.writeAttribute("size", size, null);
        }
        if (readonly != null) {
            writer.writeAttribute("readonly", readonly.booleanValue(), null);
        }
        writer.endElement("input");

        writer.startElement("input", this);
        writer.writeAttribute("type", "button", null);
        writer.writeAttribute("value", "<", null);
        writer.writeAttribute("onclick", "promena" + id + "(-" + step.intValue() + ")", null);
        writer.endElement("input");

        writer.startElement("input", this);
        writer.writeAttribute("type", "button", null);
        writer.writeAttribute("value", ">", null);
        writer.writeAttribute("onclick", "promena" + id + "(" + step.intValue() + ")", null);
        writer.endElement("input");


    }

    public void decode(FacesContext context) {
        //dohvatamo vrednost koja je upisana u tekst polje i upisujemo je kao submittedValue nase komponente
        Map requestMap = context.getExternalContext().getRequestParameterMap();
        String clientId = getClientId(context);
        setSubmittedValue((String) requestMap.get(clientId));
        setValid(true);
    }
    /*   public void encodeBegin(FacesContext context)throws IOException{
    
    ResponseWriter writer=context.getResponseWriter();
    String clientID=getClientId(context);
    encodeInputField(writer,clientID);
    encodeDecrementButton(writer,clientID);
    encodeIncrementButton(writer,clientID);
    }
    private void encodeInputField(ResponseWriter writer,String clientId)throws IOException{
    System.out.println("U encode input field clientId:"+clientId);
    writer.startElement("input", this);
    writer.writeAttribute("name",clientId,null);
    Object v=getValue();
    System.out.println(v.toString());
    if(v!=null){
    writer.writeAttribute("value", v.toString(), null);
    }
    Integer size= (Integer)this.getAttributes().get("size");
    if(size!=null) writer.writeAttribute("size", size, null);
    writer.endElement("input");
    
    }
    
    private void encodeDecrementButton(ResponseWriter writer,String clientId)throws IOException{
    writer.startElement("input", this);
    writer.writeAttribute("type", "submit",null );
    writer.writeAttribute("value", "<", null);
    writer.writeAttribute("name", clientId+LESS, null);
    writer.endElement("input");  
    }
    private void encodeIncrementButton(ResponseWriter writer,String clientId)throws IOException{
    writer.startElement("input", this);
    writer.writeAttribute("type", "submit",null );
    writer.writeAttribute("value", ">", null);
    writer.writeAttribute("name", clientId+MORE, null);
    writer.endElement("input");
    }
    
    public void decode(FacesContext context){
    Map requestMap=context.getExternalContext().getRequestParameterMap();
    String clientId=getClientId(context);
    System.out.println("U decode clientId:"+clientId);
    int increment=0;
    if(requestMap.containsKey(clientId+LESS)) increment=-1;
    else if(requestMap.containsKey(clientId+MORE)) 
    {increment=1; System.out.println("increment");}
    try{
    String pomoc=(String)requestMap.get(clientId);
    
    int submittedValue=(Integer.parseInt((String)requestMap.get(clientId)));
    int newValue=getIncrementedValue(submittedValue,increment);
    System.out.println("newVAlue:"+newValue);
    setSubmittedValue(newValue+"");
    setValid(true);
    }catch(NumberFormatException ne){
    setSubmittedValue((String)requestMap.get(clientId));
    System.out.println("BLA");
    }
    }
    
    private int getIncrementedValue(int submittedValue,int increment){
    Integer minimum=(Integer)getAttributes().get("minimum");
    Integer maximum=(Integer)getAttributes().get("maximum");
    int newValue = submittedValue + increment;
    if ((minimum == null || newValue >= minimum.intValue()) &&
    (maximum == null || newValue <= maximum.intValue()))
    return newValue;
    else
    return submittedValue;
    
    }*/
}
