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

package beans;

/**
 *
 * @author CNOC
 */
import java.util.Iterator;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author CNOC
 */
public class BackingBean2 {

    private String zip;
    private String city;
    private String state;
    private String errorMessage;
    private UIInput cityInput = null;
    private UIInput stateInput = null;

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public UIInput getCityInput() {
        return cityInput;
    }

    public void setCityInput(UIInput cityInput) {
        this.cityInput = cityInput;
    }

    public UIInput getStateInput() {
        return stateInput;
    }

    public void setStateInput(UIInput stateInput) {
        this.stateInput = stateInput;
    }

    public void validateZip(ActionEvent ae) {

        try {
            Thread.sleep(900);//kako bi se videla animacija

        } catch (Exception e) {
            e.printStackTrace();
        }

        UIInput input = (UIInput) ae.getComponent() //ajax4jsf komponenta!!!
                .getParent();//nasa zip input komponenta

        if (input != null) {

            String zip = (String) input.getSubmittedValue();//uzimamo vrednost koja je 
            //submitovana 
            if (zip != null) {

                setCityAndState(zip);
                System.out.println("ZIP JE "+zip);
                FacesContext context = FacesContext.getCurrentInstance();
                input.validate(context); // rucno iteriramo kroz sve validatore
                //pridruzene ovoj komponenti

                System.out.println("ZIP JE "+zip);
                if (!input.isValid()) {
                    setErrorMessage(context, input);
                }

                //ako ne postavimo ove vrednosti na null, nece se
                //prikazati nove vrednosti za city i state
                cityInput.setSubmittedValue(null);
                stateInput.setSubmittedValue(null);

            }

        }
    }

    private void setErrorMessage(FacesContext context, UIInput input) {
        Iterator it = context.getMessages(input.getClientId(context));
        if (it.hasNext()) {
            FacesMessage facesMessage = (FacesMessage) it.next();
            errorMessage = facesMessage.getSummary();
        }
    }

    private void setCityAndState(String zip) {
        String cityAndState = ZipCodeDataBase.getCityAndState(zip);
        if (cityAndState != null) { // zip recognized

            String[] cityStateArray = cityAndState.split(",");
            setCity(cityStateArray[0]);
            setState(cityStateArray[1]);
        } else { // unknown zip with 5 chars

            if (zip.length() == 5) {
                setCity("nema podataka za " + zip);
                setState("nema podataka za " + zip);
            } else { 

                setCity(null);
                setState(null);
            }
        }

    }
}

