package com.restmatic.restautomation.vc.ad;


import com.acme.model.domain.ad.entity.Currency;
import com.acme.model.domain.ad.sessionbean.ADManager;
import com.acme.structure.BaseEntity;
import com.acme.structure.GridData;
import com.acme.structure.ServerResponse;
import com.acme.structure.ServiceLocator;
import com.acme.structure.component.BaseRow;
import com.acme.structure.util.ConversionUtil;
import com.acme.util.ui.UIHelper;
import com.acme.util.ui.WindowManager;
import com.acme.util.ui.bean.ToolbarMBean;
import com.acme.util.utilities.ADFUtils;
import com.acme.util.utilities.JSFUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

import javax.ejb.EJB;

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

import javax.naming.NamingException;

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

import org.apache.myfaces.trinidad.model.RowKeySet;
import org.apache.myfaces.trinidad.model.RowKeySetImpl;


public class CurrencyMBean extends ToolbarMBean
{
    private GridData data = new GridData();
    @EJB
    ADManager managerBean;
    private Vector baseRowCollection = new Vector();
    private static String ENTITYTABLECOMPONENTID = "entityT";
    private RowKeySet entityTableRowKeySet = null;

    public CurrencyMBean()
    {
        try
        {
            managerBean = (ADManager)ServiceLocator.getInstance().getResource("ejb/ADManager/remote", ADManager.class);
        }
        catch (NamingException e)
        {
            e.printStackTrace();
        }
    }

    public void init()
    {
        try
        {
            if (managerBean != null)
            {
                Collection<BaseEntity> collection = managerBean.findCurrencies();
                Collections.sort((List<BaseEntity>)collection);
                data.initializeGridData(collection);
                baseRowCollection.clear();
                baseRowCollection.addAll(ConversionUtil.getTransformedBaseRowCollection(data.getDataCollection()));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private BaseRow createBaseRow()
    {
        BaseRow row = new BaseRow(new Currency());
        return row;
    }

    public void saveButtonPressed(ActionEvent actionEvent)
    {
        try
        {
            ServerResponse responseData = managerBean.synchronizeCurrencies(data);
            Collection exceptionList = responseData.getExceptionList();
            if (exceptionList.isEmpty())
            {
                Collection collection = responseData.getReturnData();
                data.updateDataCollectionWithSynch(collection);
                Collections.sort((List)data.getDataCollection());
                baseRowCollection.clear();
                baseRowCollection.addAll(ConversionUtil.getTransformedBaseRowCollection(data.getDataCollection()));
                UIHelper.showInformationMessage(WindowManager.getInstance().getStatusUtil().incomingSuccessMsg());
            } else
                UIHelper.showInformationMessages(WindowManager.getInstance().getHandler().handleExceptions(exceptionList));
        }
        catch (Exception exc)
        {
            UIHelper.showInformationMessages(WindowManager.getInstance().getHandler().handleException(exc));
        }
    }

    public void addButtonPressed(ActionEvent actionEvent)
    {
        BaseRow baseRow = createBaseRow();
        baseRowCollection.add(0, baseRow);
        data.getDataCollection().add(0, baseRow.getBaseEntity());
        data.getNewBuffer().add(baseRow.getBaseEntity());
    }

    public void removeButtonPressed(ActionEvent event)
    {
        BaseRow baseRow = getSelectedMainBaseRow();
        if (data.getNewBuffer().contains(baseRow.getBaseEntity()))
        {
            data.getNewBuffer().remove(baseRow.getBaseEntity());
            data.getDataCollection().remove(baseRow.getBaseEntity());
            baseRowCollection.remove(baseRow);
        } else
        {
            data.getGarbageData().add(baseRow.getBaseEntity());
            data.getDataCollection().remove(baseRow.getBaseEntity());
            baseRowCollection.remove(baseRow);
        }
    }

    public void rollbackButtonPressed(ActionEvent actionEvent)
    {
        data.getDataCollection().clear();
        data.getGarbageData().clear();
        data.getNewBuffer().clear();
        data.getUpdatedBuffer().clear();
        data.getDataCollection().addAll(data.getSynchBuffer());
        baseRowCollection.clear();
        baseRowCollection.addAll(ConversionUtil.getTransformedBaseRowCollection(data.getDataCollection()));
    }

    public void update(ActionEvent actionEvent)
    {
        RichTable entityT = (RichTable)JSFUtils.findComponentInRoot(ENTITYTABLECOMPONENTID);
        BaseRow baseRow = (BaseRow)entityT.getSelectedRowData();
        baseRow.getRowProperties().setUpdateable(true);
        if (!data.getUpdatedBuffer().contains(baseRow.getBaseEntity()))
        {
            data.getUpdatedBuffer().add(baseRow.getBaseEntity());
        }
    }

    //    public void checkBoxValueChange(ValueChangeEvent valueChangeEvent)
    //    {
    //        Object newValue = valueChangeEvent.getNewValue();
    //        RichTable entityT = (RichTable)JSFUtils.findComponentInRoot(ENTITYTABLECOMPONENTID);
    //        BaseRow baseRow = (BaseRow)entityT.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 selectAllValueChange(ValueChangeEvent valueChangeEvent)
    //    {
    //        if (valueChangeEvent.getNewValue() != null && valueChangeEvent.getNewValue().equals(true))
    //        {
    //            for (int i = 0; i < baseRowCollection.size(); i++)
    //            {
    //                BaseRow baseRow = (BaseRow)baseRowCollection.get(i);
    //                baseRow.getRowProperties().setSelectedRow(true);
    //                data.getSelectedRowCollection().add(baseRow);
    //            }
    //            refreshTable();
    //        } else
    //        {
    //            for (int i = 0; i < baseRowCollection.size(); i++)
    //            {
    //                BaseRow baseRow = (BaseRow)baseRowCollection.get(i);
    //                baseRow.getRowProperties().setSelectedRow(false);
    //                data.getSelectedRowCollection().remove(baseRow);
    //            }
    //            refreshTable();
    //        }
    //    }

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

    public Currency getSelectedMainEntity()
    {
        return (Currency)getSelectedMainBaseRow().getBaseEntity();
    }

    public BaseRow getSelectedMainBaseRow()
    {
        if (getSelectedEntityTableRowObject() != null)
            return (BaseRow)getSelectedEntityTableRowObject();
        else if (!baseRowCollection.isEmpty())
            return (BaseRow)baseRowCollection.get(0);
        else
            return new BaseRow();
    }

    private Object getSelectedEntityTableRowObject()
    {
        return ADFUtils.getSelectedTableRowObject(ENTITYTABLECOMPONENTID);
    }

    public void setEntityTableRowKeySet(RowKeySet entityTableRowKeySet)
    {
        this.entityTableRowKeySet = entityTableRowKeySet;
    }

    public RowKeySet getEntityTableRowKeySet()
    {
        if (entityTableRowKeySet == null)
        {
            RowKeySet selectedRowKeySet = new RowKeySetImpl();
            selectedRowKeySet.add(0);
            entityTableRowKeySet = selectedRowKeySet;
        }
        return entityTableRowKeySet;
    }

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

    public Vector getBaseRowCollection()
    {
        return baseRowCollection;
    }

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

    public GridData getGridData()
    {
        return data;
    }
}
