/*
 * AskSecretDialog.java
 *
 * Copyright (C) 2009-18 by RStudio, Inc.
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */

package org.rstudio.studio.client.common.rstudioapi.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.Widget;

import org.rstudio.core.client.StringUtil;
import org.rstudio.core.client.widget.MessageDialog;
import org.rstudio.core.client.widget.ModalDialog;
import org.rstudio.core.client.widget.Operation;
import org.rstudio.core.client.widget.ProgressOperationWithInput;

public class AskSecretDialog extends ModalDialog<AskSecretDialogResult>
{
   interface Binder extends UiBinder<Widget, AskSecretDialog>
   {}

   public AskSecretDialog(String title,
                          String prompt,
                          boolean canRemember,
                          ProgressOperationWithInput<AskSecretDialogResult> okOperation,
                          Operation cancelOperation)
   {
      super(title, okOperation, cancelOperation);

      mainWidget_ = GWT.<Binder>create(Binder.class).createAndBindUi(this);
     
      label_.setText(prompt);
      textbox_.setFocus(true);

      rememberEnabled_.setVisible(false);
      rememberDisabled_.setVisible(false);

      if (canRemember)
        rememberEnabled_.setVisible(true);
      else
        rememberDisabled_.setVisible(true);
   }

   @Override
   protected Widget createMainWidget()
   {
      return mainWidget_;
   }

   @Override
   protected AskSecretDialogResult collectInput()
   {
      AskSecretDialogResult result = new AskSecretDialogResult(textbox_.getText(), true);

      return result;
   }

   @Override
   protected void onDialogShown()
   {
      textbox_.setFocus(true);
   }

   @Override
   protected boolean validate(AskSecretDialogResult input)
   {
      if (StringUtil.isNullOrEmpty(input.getSecret()))
      {
         MessageDialog dialog = new MessageDialog(MessageDialog.ERROR,
                                                  "Error",
                                                  "You must enter a value.");
         dialog.addButton("OK", (Operation)null, true, true);
         dialog.showModal();
         textbox_.setFocus(true);

         return false;
      }
      
      return true;
   }

   @Override
   protected void positionAndShowDialog(final Command onCompleted)
   {
     setPopupPositionAndShow(new PositionCallback() {

        @Override
        public void setPosition(int offsetWidth, int offsetHeight)
        {
           int left = (Window.getClientWidth()/2) - (offsetWidth/2);
           int top = 50;

           setPopupPosition(left, top);
           onCompleted.execute();
        }

     });
   }

   private Widget mainWidget_;

   @UiField Label label_;
   @UiField PasswordTextBox textbox_;
   @UiField CheckBox remember_;
   @UiField Label install_;

   @UiField HTMLPanel rememberEnabled_;
   @UiField HTMLPanel rememberDisabled_;
}

@@ -676,16 +676,120 @@
 nt.GWT;%0A
+import com.google.gwt.event.dom.client.ClickEvent;%0Aimport com.google.gwt.event.dom.client.ClickHandler;%0A
 import c
@@ -1003,24 +1003,67 @@
 i.CheckBox;%0A
+import com.google.gwt.user.client.ui.HTML;%0A
 import com.g
@@ -1192,24 +1192,76 @@
 ordTextBox;%0A
+import com.google.gwt.user.client.ui.VerticalPanel;%0A
 import com.g
@@ -1555,16 +1555,66 @@
 thInput;
+%0Aimport org.rstudio.studio.client.common.HelpLink;
 %0A%0Apublic
@@ -2168,62 +2168,1385 @@
     
-label_.setText(prompt);%0A      textbox_.setFocus(true);
+setHelpLink(new HelpLink(%0A         %22Using Keyring%22,%0A         %22using_keyring%22,%0A         false,%0A         true)%0A      );%0A%0A      label_.setText(prompt);%0A      textbox_.setFocus(true);%0A%0A      install_.addClickHandler(new ClickHandler() %7B%0A         @Override%0A         public void onClick(ClickEvent event) %7B%0A            VerticalPanel verticalPanel = new VerticalPanel();%0A            verticalPanel.getElement().setAttribute(%0A               %22style%22,%0A               %22padding-left: 6px; width: 320px;%22%0A            );%0A            %0A            Label infoLabel = new Label(%0A               %22Keyring is an R package that provides access to %22 +%0A               %22the operating systems credential store to allow you %22 +%0A               %22to remember, securely, passwords and secrets. %22%0A            );%0A            %0A            HTML questionHtml = new HTML(%0A               %22%3Cbr%3E%3Cb%3EWould you like to install Keyring?%3C/b%3E%3Cbr%3E%3Cbr%3E%22%0A            );%0A            %0A            verticalPanel.add(infoLabel);%0A            verticalPanel.add(questionHtml);%0A            %0A            MessageDialog dialog = new MessageDialog(%0A               MessageDialog.QUESTION,%0A               %22Keyring%22,%0A               verticalPanel);%0A            dialog.addButton(%22Install%22, (Operation)null, true, false);%0A            dialog.addButton(%22Cancel%22, (Operation)null, false, true);%0A            dialog.showModal();%0A         %7D%0A      %7D);%0A
 %0A%0A  

add prompt to install keyring