package org.rstudio.studio.client.workbench.views.environment.view;

import java.util.ArrayList;

import org.rstudio.core.client.StringUtil;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.builder.shared.TableCellBuilder;
import com.google.gwt.dom.builder.shared.TableRowBuilder;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.user.cellview.client.AbstractCellTable;
import com.google.gwt.user.cellview.client.AbstractCellTableBuilder;
import com.google.gwt.user.cellview.client.AbstractHeaderOrFooterBuilder;
import com.google.gwt.user.cellview.client.Column;

public class EnvironmentObjectGrid extends EnvironmentObjectDisplay
{
   public interface Style extends CssResource
   {
      String objectGridColumn();
      String objectGridHeader();
   }

   public interface Resources extends ClientBundle
   {
      @Source("EnvironmentObjectGrid.css")
      Style style();
   }

   public EnvironmentObjectGrid(EnvironmentObjectDisplay.Host host,
                                EnvironmentObjectsObserver observer)
   {
      super(host, observer);
      style_ = ((Resources)GWT.create(Resources.class)).style();
      style_.ensureInjected();
      createColumns();
      setTableBuilder(new EnvironmentObjectGridBuilder(this));
      setHeaderBuilder(new GridHeaderBuilder(this, false));
   }

   private void createColumns()
   {
      columns_.add(new ObjectGridColumn(
              new ClickableTextCell(), "Name", 20, 
              ObjectGridColumn.COLUMN_NAME)
              {
                  @Override
                  public String getValue(RObjectEntry object)
                  {
                     return object.rObject.getName();
                  }
              });
      columns_.add(new ObjectGridColumn(
              new ClickableTextCell(), "Type", 15, ObjectGridColumn.COLUMN_TYPE)
              {
                  @Override
                  public String getValue(RObjectEntry object)
                  {
                     return object.rObject.getType();
                  }
              });
      columns_.add(new ObjectGridColumn(
              new ClickableTextCell(), "Length", 10, ObjectGridColumn.COLUMN_LENGTH)
              {
                  @Override
                  public String getValue(RObjectEntry object)
                  {
                     return (new Integer(object.rObject.getLength())).toString();
                  }
              });
      columns_.add(new ObjectGridColumn(
              new ClickableTextCell(), "Size", 15, ObjectGridColumn.COLUMN_SIZE)
              {
                  @Override
                  public String getValue(RObjectEntry object)
                  {
                     return StringUtil.formatFileSize(object.rObject.getSize());
                  }
              });
      columns_.add(new ObjectGridColumn(
              new ClickableTextCell(), "Value", 40, ObjectGridColumn.COLUMN_VALUE)
              {
                  @Override
                  public String getValue(RObjectEntry object)
                  {
                     return object.rObject.getValue();
                  }
              });
      for (Column<RObjectEntry, String> column: columns_)
      {
         addColumn(column);
      }
   }

   private class GridHeaderBuilder 
           extends AbstractHeaderOrFooterBuilder<RObjectEntry>
   {
      public GridHeaderBuilder(AbstractCellTable<RObjectEntry> table,
                               boolean isFooter)
      {
         super(table, isFooter);
      }

      @Override
      protected boolean buildHeaderOrFooterImpl()
      {
         TableRowBuilder row = startRow();
         for (ObjectGridColumn col: columns_)
         {
            TableCellBuilder cell = row.startTD();
            cell.className(style_.objectGridHeader());
            cell.style().width(col.getWidth(), Unit.PCT);
            cell.text(col.getName());
            cell.endTD();
         }
         row.end();
         return true;
      }
   }

   // builds individual rows of the object table
   private class EnvironmentObjectGridBuilder
           extends AbstractCellTableBuilder<RObjectEntry>
   {

      public EnvironmentObjectGridBuilder(
            AbstractCellTable<RObjectEntry> cellTable)
      {
         super(cellTable);
      }

      @Override
      protected void buildRowImpl(RObjectEntry rowValue, int absRowIndex)
      {
         TableRowBuilder row = startRow();

         if (!rowValue.visible)
            return;

         for (int i = 0; i < columns_.size(); i++)
         {
            ObjectGridColumn col = columns_.get(i);
            TableCellBuilder nameCol = row.startTD();
            nameCol.className(style_.objectGridColumn());
            nameCol.style().width(col.getWidth(), Unit.PCT);
            renderCell(nameCol, createContext(i), col, rowValue);
            nameCol.endTD();
         }
         
         row.end();
      }
   }
   
   private ArrayList<ObjectGridColumn> columns_ = 
         new ArrayList<ObjectGridColumn>();
   private Style style_;
}

@@ -1599,24 +1599,39 @@
 bleTextCell(
+filterRenderer_
 ), %22Name%22, 2
@@ -1634,16 +1634,16 @@
 %22, 20, %0A
-
         
@@ -1980,32 +1980,47 @@
 (), %22Type%22, 15, 
+%0A              
 ObjectGridColumn
@@ -2345,16 +2345,31 @@
 h%22, 10, 
+%0A              
 ObjectGr
@@ -2730,16 +2730,31 @@
 e%22, 15, 
+%0A              
 ObjectGr
@@ -3101,23 +3101,53 @@
 ell(
-), %22Value%22, 40,
+filterRenderer_), %22Value%22, 40, %0A             
  Obj
@@ -4646,52 +4646,8 @@
   %7B%0A
-         TableRowBuilder row = startRow();%0A%0A
     
@@ -4695,16 +4695,60 @@
 eturn;%0A%0A
+         TableRowBuilder row = startRow();%0A%0A
         
@@ -4882,23 +4882,18 @@
 Builder 
-nameCol
+td
  = row.s
@@ -4914,23 +4914,18 @@
         
-nameCol
+td
 .classNa
@@ -4967,23 +4967,18 @@
         
-nameCol
+td
 .style()
@@ -5034,23 +5034,18 @@
 derCell(
-nameCol
+td
 , create
@@ -5088,15 +5088,10 @@
     
-nameCol
+td
 .end

make filtering work in grid view