/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.sample.accessible.selection;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.PhaseId;
import javax.faces.model.SelectItem;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;
import java.util.Arrays;
import java.lang.Integer;
/**
 * <p>The SelectionTagsBean Class is the backing bean for the selection
 * components demonstration. It is used to store the options and selected values
 * of the various selection components.</p>
 * <p>For Developers new to JSF, the SelectItem object is a key class for
 * working with ICEfaces standard enhanced components. It can be added
 * as the &lt;f:selectItems /&gt; tags value attribute to generate  list data.
 * JSF takes care of the default value and value binding in general.</p>
 * <p>Two gotcha when using and of the selection tags with JSF 1.1.
 * <li>use boolean primative instead of the Boolean Object</li>
 * <li>selection tags require String object.  If you want to use another
 * object type for list use a Converter.</li>
 * </p>
 *
 * @sinse 0.3.0
 */
@ManagedBean
@ApplicationScoped
public class SelectionBean {
	private SelectItem[] carListItems = new SelectItem[] {new SelectItem("1","Vroom"), new SelectItem("2","Zoom")};
	private SelectItem[] componentItems = new SelectItem[] {new SelectItem("1","Widget"), new SelectItem("2","Sprocket")};
	private SelectItem[] cityItems = new SelectItem[] {new SelectItem("1","Rome"), new SelectItem("2","Ulaanbaatar")};
	private SelectItem[] countryItems = new SelectItem[] {new SelectItem("1","UAE"), new SelectItem("2","Austro-Hungary")};
	private SelectItem[] drinkItems = new SelectItem[] {new SelectItem("1","Bloody Mary"), new SelectItem("2","Car Bomb")};
	private SelectItem[] languageItems = new SelectItem[] {new SelectItem("1","English"), new SelectItem("2","French")};
    // check box example value
    private boolean newUser = false;
    // selectOneMenu for components
    private String selectedComponent = "2";
    // selectManyMenu cars values
    private String[] selectedCars = new String[]{"2"};
    // radio button example
    private String selectedDrink = "2";
    // checkbox multiselect languanges example
    private String[] selectedLanguages = new String[]{"2"};
    // selectOneListbox example value
    private String selectedCountry = "2";
    // selectManyListbox example value
    private String[] selectedCities = new String[]{"2"};

	private int[] stringArrayToIntArray(String[] sarray) {
		if (sarray != null) {
			int intarray[] = new int[sarray.length];
			for (int i = 0; i < sarray.length; i++) {
				System.out.println(sarray[i]);
				intarray[i] = Integer.parseInt(sarray[i]);
			}
			return intarray;
		}
		return null;
	}
	
	public boolean getNewUser() { return this.newUser; }
	public void setNewUser(boolean newUser) { this.newUser = newUser; }
	
	public String getSelectedComponent() { return this.selectedComponent; }
	public void setSelectedComponent(String selectedComponent) { this.selectedComponent = selectedComponent; }
	
	public String[] getSelectedCars() { return this.selectedCars; }
	public void setSelectedCars(String[] selectedCars) { this.selectedCars = selectedCars; }
	
	public String getSelectedDrink() { return this.selectedDrink; }
	public void setSelectedDrink(String selectedDrink) { this.selectedDrink = selectedDrink; }

	public String getSelectedCountry() { return this.selectedCountry; }
	public void setSelectedCountry(String selectedCountry) {
		if (Integer.parseInt(this.selectedCountry) != Integer.parseInt(selectedCountry))	
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Country changed.", null));
		this.selectedCountry = selectedCountry; 
	}
	
	public String[] getSelectedLanguages() { return this.selectedLanguages; }
	public void setSelectedLanguages(String[] selectedLanguages) { this.selectedLanguages = selectedLanguages; }
		
	public String[] getSelectedCities() { return this.selectedCities; }
	public void setSelectedCities(String[] selectedCities) { 
		if (!Arrays.equals(stringArrayToIntArray(this.selectedCities),stringArrayToIntArray(selectedCities)))
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "City changed.", null));
		this.selectedCities = selectedCities; 
	}
	
	public SelectItem[] getCarListItems() { return this.carListItems; }
	public void setCarListItems(SelectItem[] carListItems) { this.carListItems = carListItems; }
	
	public SelectItem[] getComponentItems() { return this.componentItems; }
	public void setComponentItems(SelectItem[] componentItems) { this.componentItems = componentItems; }
	
	public SelectItem[] getCityItems() { return this.cityItems; }
	public void setCityItems(SelectItem[] cityItems) { this.cityItems = cityItems; }
	
	public SelectItem[] getCountryItems() { return this.countryItems; }
	public void setCountryItems(SelectItem[] countryItems) { this.countryItems = countryItems; }

	public SelectItem[] getDrinkItems() { return this.drinkItems; }
	public void setDrinkItems(SelectItem[] drinkItems) { this.drinkItems = drinkItems; }
	
	public SelectItem[] getLanguageItems() { return this.languageItems; }
	public void setLanguageItems(SelectItem[] languageItems) { this.languageItems = languageItems; }
}