package com.acme.vcutil.bean;


import com.acme.structure.GridData;
import com.acme.structure.component.BaseRow;
import com.acme.structure.util.datalist.ListManager;
import com.acme.structure.util.datalist.ListSupplier;
import com.acme.util.ui.bean.ToolbarMBean;
import com.acme.util.utilities.JSFUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import oracle.adf.view.rich.component.rich.data.RichTable;


public class ListSupplierMBean extends ToolbarMBean
{
    private GridData data = new GridData();
    private Vector rowCollection = new Vector();
    private final static String MAINTABLECOMPONENTID = "suplistT";

    public void init()
    {
        Collection supplierList = ListManager.getInstance().getSortedLists();
        rowCollection.addAll(getBaseRowList(supplierList));
    }

    public void refreshButtonPressed(ActionEvent actionEvent)
    {
        Iterator iterator = data.getSelectedRowCollection().iterator();
        while (iterator.hasNext())
        {
            ListSupplier supplier = (ListSupplier)((BaseRow)iterator.next()).getDataObject();
            supplier.refreshList();
        }
    }

    private void refreshTable()
    {
        JSFUtils.refreshComponent(MAINTABLECOMPONENTID);
        FacesContext fctx = FacesContext.getCurrentInstance();
        fctx.renderResponse();
    }

    public void selectAllValueChange(ValueChangeEvent valueChangeEvent)
    {
        if (valueChangeEvent.getNewValue() != null && valueChangeEvent.getNewValue().equals(true))
        {
            for (int i = 0; i < rowCollection.size(); i++)
            {
                BaseRow baseRow = (BaseRow)rowCollection.get(i);
                baseRow.getRowProperties().setSelectedRow(true);
                data.getSelectedRowCollection().add(baseRow);
            }
            refreshTable();
        } else
        {
            for (int i = 0; i < rowCollection.size(); i++)
            {
                BaseRow baseRow = (BaseRow)rowCollection.get(i);
                baseRow.getRowProperties().setSelectedRow(false);
                data.getSelectedRowCollection().remove(baseRow);
            }
            refreshTable();
        }
    }

    public void checkBoxValueChange(ValueChangeEvent valueChangeEvent)
    {
        Object newValue = valueChangeEvent.getNewValue();
        RichTable richTable = (RichTable)JSFUtils.findComponentInRoot(MAINTABLECOMPONENTID);
        BaseRow baseRow = (BaseRow)richTable.getSelectedRowData();
        System.out.println(baseRow);

        if (newValue.equals(true))
        {
            if (!data.getSelectedRowCollection().contains(baseRow))
                data.getSelectedRowCollection().add(baseRow);
        } else if (data.getSelectedRowCollection().contains(baseRow))
            data.getSelectedRowCollection().remove(baseRow);
    }


    public void setRowCollection(Vector baseRowCollection)
    {
        this.rowCollection = baseRowCollection;
    }

    public Vector getRowCollection()
    {
        return rowCollection;
    }

    public void getGridData(GridData data)
    {
        this.data = data;
    }

    public GridData getGridData()
    {
        return data;
    }

    private Collection getBaseRowList(Collection supplierList)
    {
        Collection<BaseRow> tmpCollection = new ArrayList<BaseRow>();
        Iterator iterator = supplierList.iterator();
        BaseRow row = null;
        while (iterator.hasNext())
        {
            row = new BaseRow(iterator.next(), false);
            tmpCollection.add(row);
        }
        return tmpCollection;
    }

    @Override
    public void addButtonPressed(ActionEvent actionEvent)
    {
        super.addButtonPressed(actionEvent);
    }

    @Override
    public void clearButtonPressed(ActionEvent actionEvent)
    {
        super.clearButtonPressed(actionEvent);
    }

    @Override
    public void loadButtonPressed(ActionEvent actionEvent)
    {
        super.loadButtonPressed(actionEvent);
    }

    @Override
    public void saveButtonPressed(ActionEvent actionEvent)
    {
        super.saveButtonPressed(actionEvent);
    }

    @Override
    public void removeButtonPressed(ActionEvent actionEvent)
    {
        super.removeButtonPressed(actionEvent);
    }

    @Override
    public void searchButtonPressed(ActionEvent actionEvent)
    {
        super.searchButtonPressed(actionEvent);
    }
}
