package org.rstudio.core.client.widget;


import java.util.ArrayList;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Wizard<I,T> extends ModalDialog<T>
{
   public Wizard(String caption, 
                 String subCaption,
                 I initialData,
                 final ProgressOperationWithInput<T> operation)
   {
      super(caption, operation);
      subCaption_ = subCaption;
      
      setOkButtonCaption("Create Project");
      setOkButtonVisible(false);
   }
   
   protected void addPage(WizardPage<I,T> page)
   {
      pages_.add(page);
   }
   

   @Override
   protected Widget createMainWidget()
   {
      WizardResources.Styles styles = WizardResources.INSTANCE.styles();
      
      VerticalPanel mainWidget = new VerticalPanel();
      mainWidget.addStyleName(styles.mainWidget());
      
      Label subCaptionLabel = new Label(subCaption_);
      subCaptionLabel.addStyleName(styles.headerLabel());
      mainWidget.add(subCaptionLabel);
      
      // main body panel for transitions
      bodyPanel_ = new LayoutPanel();
      bodyPanel_.addStyleName(styles.wizardBodyPanel());
      mainWidget.add(bodyPanel_);
     
      // page selection panel
      pageSelectorPanel_ = new VerticalPanel();
      pageSelectorPanel_.addStyleName(styles.wizardPageSelector());
      pageSelectorPanel_.setSize("100%", "100%");
      for (int i=0; i<pages_.size(); i++)
      {
         PageSelectorItem pageSelector = new PageSelectorItem(pages_.get(i));
         
         if (i==0)
            pageSelector.addStyleName(styles.wizardPageSelectorItemFirst());
         
         if (i == (pages_.size() -1))
            pageSelector.addStyleName(styles.wizardPageSelectorItemLast());
         
         pageSelectorPanel_.add(pageSelector);
      }
      bodyPanel_.add(pageSelectorPanel_);
      bodyPanel_.setWidgetTopBottom(pageSelectorPanel_, 0, Unit.PX, 0, Unit.PX);
      bodyPanel_.setWidgetLeftRight(pageSelectorPanel_, 0, Unit.PX, 0, Unit.PX);
      bodyPanel_.setWidgetVisible(pageSelectorPanel_, true);
    
      // add pages and make them invisible
      for (int i=0; i<pages_.size(); i++)
      {
         WizardPage<I,T> page = pages_.get(i);
         page.setSize("100%", "100%");
         
         bodyPanel_.add(page);
         bodyPanel_.setWidgetTopBottom(page, 0, Unit.PX, 0, Unit.PX);
         bodyPanel_.setWidgetLeftRight(page, 0, Unit.PX, 0, Unit.PX);
         bodyPanel_.setWidgetVisible(page, false);
      }
      
     
      
      return mainWidget;
   }
   
   @Override
   protected T collectInput()
   {
      
      return null;
   }

   @Override
   protected boolean validate(T input)
   {
      
      return true;
   }
   
   
   private class PageSelectorItem extends Composite
   {
      PageSelectorItem(WizardPageInfo pageInfo)
      {
         WizardResources.Styles styles = WizardResources.INSTANCE.styles();
         
         LayoutPanel layoutPanel = new LayoutPanel();
         layoutPanel.addStyleName(styles.wizardPageSelectorItem());
         
         Label label =  new Label(pageInfo.getTitle());
         layoutPanel.add(label);
         
         initWidget(layoutPanel);
      }
   }
   
    
   private final String subCaption_;
   
   private LayoutPanel bodyPanel_;
   private VerticalPanel pageSelectorPanel_;
  
   private ArrayList<WizardPage<I,T>> pages_ = new ArrayList<WizardPage<I,T>>();

}

@@ -157,16 +157,60 @@
 posite;%0A
+import com.google.gwt.user.client.ui.Image;%0A
 import c
@@ -3114,36 +3114,26 @@
 ardResources
-.Styles styl
+ r
 es = WizardR
@@ -3141,32 +3141,78 @@
 sources.INSTANCE
+;%0A         WizardResources.Styles styles = res
 .styles();%0A     
@@ -3361,32 +3361,31 @@
     
-Label label
+Image image
  =
- 
  new 
-Label
+Image
 (pag
@@ -3397,50 +3397,1007 @@
 .get
-Title());%0A         layoutPanel.add(label);
+Image());%0A         layoutPanel.add(image);%0A         layoutPanel.setWidgetLeftWidth(image, %0A                                        10, Unit.PX, %0A                                        image.getWidth(), Unit.PX);%0A         layoutPanel.setWidgetTopHeight(image, %0A                                        40-(image.getHeight()/2), Unit.PX, %0A                                        image.getHeight(), Unit.PX);%0A         %0A        %0A         %0A         %0A         Image arrowImage = new Image(res.wizardDisclosureArrow());%0A         layoutPanel.add(arrowImage);%0A         layoutPanel.setWidgetRightWidth(arrowImage, %0A                                         10, Unit.PX, %0A                                         arrowImage.getWidth(), %0A                                         Unit.PX);%0A         layoutPanel.setWidgetTopHeight(arrowImage,%0A                                        40-(arrowImage.getHeight()/2), Unit.PX,%0A                                        arrowImage.getHeight(), Unit.PX);%0A         %0A       
 %0A   

add images to page selector