package com.restmatic.restautomation.vc.ejf;


import com.acme.model.domain.ejf.entity.Role;
import com.acme.model.domain.ejf.sessionbean.EJFManager;
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.JSFUtils;

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

import javax.ejb.EJB;

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

import javax.naming.NamingException;

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


public class RoleMBean extends ToolbarMBean
{
    private GridData data = new GridData();
    @EJB
    EJFManager managerBean;
    private Vector baseRowCollection = new Vector();
    private static String ENTITYTABLECOMPONENTID = "entityT";

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

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

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

    public GridData getGridData()
    {
        return data;
    }

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

    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 saveButtonPressed(ActionEvent actionEvent)
    {
        try
        {
            ServerResponse responseData = managerBean.synchronizeRoles(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 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 removeButtonPressed(ActionEvent event)
    {
        Iterator iterator = data.getSelectedRowCollection().iterator();
        while (iterator.hasNext())
        {
            BaseRow baseRow = (BaseRow)iterator.next();
            if (data.getNewBuffer().contains(baseRow.getBaseEntity()))
            {
                data.getDataCollection().remove(baseRow.getBaseEntity());
                baseRowCollection.remove(baseRow);
            } else
            {
                data.getGarbageData().add(baseRow.getBaseEntity());
                data.getDataCollection().remove(baseRow.getBaseEntity());
                baseRowCollection.remove(baseRow);
            }
        }
        data.getSelectedRowCollection().clear();
        refreshTable();
    }

    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 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);
    }

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

    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();
        }
    }

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

    public Vector getBaseRowCollection()
    {
        return baseRowCollection;
    }
}
