package maf.sample.mobile;

import java.util.ArrayList;

import oracle.adfmf.amx.event.ValueChangeEvent;
import oracle.adfmf.bindings.dbf.AmxAttributeBinding;
import oracle.adfmf.bindings.dbf.AmxIteratorBinding;
import oracle.adfmf.bindings.iterator.BasicIterator;
import oracle.adfmf.framework.api.AdfmfJavaUtilities;
import oracle.adfmf.java.beans.PropertyChangeListener;
import oracle.adfmf.java.beans.PropertyChangeSupport;

import oracle.byop.maf.data.entities.Customer;

/**
 * This sample reads the selected index values in a select many list and
 * obtains the model objects the selected index represents to display the
 * names of customers in a text field on the AMX page
 *
 * @author Frank Nimphius
 * @copyright Oracle Corporation, 2015
 */
public class SelectHelper {

    String selectedValues = "";
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public SelectHelper() {
    }

    public void onValueChange(ValueChangeEvent valueChangeEvent) {

        //the newValue property of the valueChangeEvent shows an array of Strings 
        //with the selected values. This array is updated with each new selection 
        //or de-select
        Object[] newVal = (Object[]) valueChangeEvent.getNewValue();
        //the oldValue property of the valueChangeEvent shows an array of Strings 
        //with the values of the previous selection. This property allows you to 
        //tell what exactly has been changed within a new selection
        Object[]  oldVal = (Object[]) valueChangeEvent.getOldValue();

        //converting array of Strings to list of Integer to read the real t 
        //objects from the list binding iterator
        ArrayList<Integer> selectedCustomers = new ArrayList<Integer>();      
        for (int i = 0; i < newVal.length; i++) {
            selectedCustomers.add(new Integer((String)newVal[i]));
        }
        //lists are represented by the  AmxAttributeBinding
        AmxAttributeBinding customerList =
            (AmxAttributeBinding) AdfmfJavaUtilities.evaluateELExpression("#{bindings.customers}");

        StringBuffer selectedCustomerNames = new StringBuffer();

        //access the list iterator to first set the current row by the indexes obtained 
        //from the value change event and then read the row object, which in this sample
        //represents a customer
        
        AmxIteratorBinding amxListIterator =  customerList.getIteratorBinding();
        //the basic iterator in the AmxIteratorBinding is what we need to work with
        BasicIterator      basicIterator = amxListIterator.getIterator();
        
        for (Integer customerIndx : selectedCustomers) {
            //set new current row in list iterator
            basicIterator.setCurrentIndex(customerIndx.intValue());
            //get current row. Note that for POJO models you call getDataProvider and cast to 
            //the entity. For SOAP models you call getCurrentRow and cast it to GenericType 
            Customer customer = (Customer) basicIterator.getDataProvider();
            //for this sample, print selected customers into a text field on the page
            selectedCustomerNames.append("Customer on index  " + (customerIndx.intValue() + 1) + ": " + customer.getName()+"\n");
        }

        this.setSelectedValues(selectedCustomerNames.toString());

    }


    public void setSelectedValues(String selectedValues) {
        String oldSelectedValues = this.selectedValues;
        this.selectedValues = selectedValues;
        propertyChangeSupport.firePropertyChange("selectedValues", oldSelectedValues, selectedValues);
    }

    public String getSelectedValues() {
        return selectedValues;
    }

    public void addPropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.removePropertyChangeListener(l);
    }
}
