package org.rstudio.studio.client.workbench.views.vcs.dialog;

import org.rstudio.core.client.files.FileSystemItem;
import org.rstudio.core.client.widget.FullscreenPopupPanel;
import org.rstudio.core.client.widget.Toolbar;
import org.rstudio.studio.client.common.filetypes.FileTypeRegistry;
import org.rstudio.studio.client.common.filetypes.TextFileType;
import org.rstudio.studio.client.workbench.views.source.PanelWithToolbars;
import org.rstudio.studio.client.workbench.views.source.editors.text.AceEditor;
import org.rstudio.studio.client.workbench.views.source.editors.text.DocDisplay;
import org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTargetFindReplace;
import org.rstudio.studio.client.workbench.views.source.editors.text.findreplace.FindReplaceBar;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class ViewFilePanel extends Composite 
                           implements ViewFilePresenter.Display
{
   @Inject
   public ViewFilePanel(DocDisplay docDisplay,
                        FileTypeRegistry fileTypeRegistry)
   {
      fileTypeRegistry_ = fileTypeRegistry;
      docDisplay_ = docDisplay; 
      docDisplay_.setReadOnly(true);
      
      findReplace_ = new TextEditingTargetFindReplace(
            new TextEditingTargetFindReplace.Container() {

               @Override
               public AceEditor getEditor()
               {
                  return (AceEditor)docDisplay_;
               }

               @Override
               public void insertFindReplace(FindReplaceBar findReplaceBar)
               {
                  panel_.insertNorth(findReplaceBar,
                                     findReplaceBar.getHeight(),
                                     null);
               }

               @Override
               public void removeFindReplace(FindReplaceBar findReplaceBar)
               {
                  panel_.remove(findReplaceBar);
               }
              
            },
            false); // don't show replace UI
      
      panel_ = new PanelWithToolbars(createToolbar(),
                                     null,
                                     docDisplay_.asWidget(),
                                     null);
      panel_.setSize("100%", "100%");
      
      initWidget(panel_);
   }
   
   @Override
   public void showFile(FileSystemItem file, String commitId, String contents)
   {
      docDisplay_.setCode(contents, false);  
      
      adaptToFileType(fileTypeRegistry_.getTextTypeForFile(file));
      
      // header widget has icon + label
      HorizontalPanel panel = new HorizontalPanel();
     
      Image imgFile = new Image(fileTypeRegistry_.getIconForFile(file));
      imgFile.addStyleName(RES.styles().captionIcon());
      panel.add(imgFile);
      
      Label lblCaption = new Label(file.getPath() + " @ " + commitId);
      lblCaption.addStyleName(RES.styles().captionLabel());
      panel.add(lblCaption);
      
      new FullscreenPopupPanel(panel,asWidget()).center();
   }
    
   private Toolbar createToolbar()
   {
      return new Toolbar();
   }  
   
   @Override
   public void onActivate()
   {
      docDisplay_.onActivate();
      
   }

   @Override
   public void adaptToFileType(TextFileType fileType)
   {
      docDisplay_.setFileType(fileType, true);
   }

   @Override
   public void setFontSize(double size)
   {
      docDisplay_.setFontSize(size);
   }

   @Override
   public Widget asWidget()
   {
      return this;
   }

   @Override
   public DocDisplay getDocDisplay()
   {
      return docDisplay_;
   }
   
   public interface Resources extends ClientBundle
   {
      @Source("ViewFilePanel.css")
      Styles styles();
   }
   
   public interface Styles extends CssResource
   {
      String captionIcon();
      String captionLabel();
   }
   
   public static void ensureStylesInjected()
   {
      RES.styles().ensureInjected();
   }
  
   private final FileTypeRegistry fileTypeRegistry_;
   private final DocDisplay docDisplay_;
   
   private final PanelWithToolbars panel_;
   @SuppressWarnings("unused")
   private final TextEditingTargetFindReplace findReplace_;
   
   private static final Resources RES = GWT.<Resources>create(Resources.class);
}

@@ -216,16 +216,69 @@
 oolbar;%0A
+import org.rstudio.core.client.widget.ToolbarButton;%0A
 import o
@@ -337,16 +337,16 @@
 gistry;%0A
-
 import o
@@ -401,16 +401,78 @@
 leType;%0A
+import org.rstudio.studio.client.workbench.commands.Commands;%0A
 import o
@@ -935,16 +935,120 @@
 nt.GWT;%0A
+import com.google.gwt.event.dom.client.ClickEvent;%0Aimport com.google.gwt.event.dom.client.ClickHandler;%0A
 import c
@@ -1640,16 +1640,59 @@
 Registry
+,%0A                        Commands commands
 )%0A   %7B%0A 
@@ -1734,16 +1734,44 @@
 gistry;%0A
+      commands_ = commands;%0A
       do
@@ -3724,28 +3724,852 @@
     
-return new Toolbar()
+Toolbar toolbar = new Toolbar();%0A      %0A      toolbar.addLeftWidget(new ToolbarButton(%0A         %22Save As...%22, %0A         commands_.saveSourceDoc().getImageResource(),%0A         new ClickHandler() %7B%0A            @Override%0A            public void onClick(ClickEvent event)%0A            %7B%0A              %0A               %0A            %7D%0A            %0A         %7D));%0A      toolbar.addLeftSeparator();%0A      %0A      toolbar.addLeftWidget(new ToolbarButton(%0A         null,%0A         commands_.printSourceDoc().getImageResource(),%0A         new ClickHandler() %7B%0A%0A            @Override%0A            public void onClick(ClickEvent event)%0A            %7B%0A               docDisplay_.print();%0A            %7D%0A            %0A         %7D));%0A      toolbar.addLeftSeparator();%0A      %0A      toolbar.addLeftWidget(findReplace_.createFindReplaceButton());%0A      %0A      return toolbar
 ;%0A  
@@ -5444,16 +5444,53 @@
 istry_;%0A
+   private final Commands commands_;%0A
    priva
@@ -5522,24 +5522,24 @@
 splay_;%0A   %0A
+
    private f
@@ -5573,39 +5573,8 @@
 l_;%0A
-   @SuppressWarnings(%22unused%22)%0A
    p

add print and file button to view file toolbar