package org.multibit.hd.ui.views.components.enter_recipient;

import com.google.common.base.Optional;
import net.miginfocom.swing.MigLayout;
import org.multibit.hd.core.dto.Recipient;
import org.multibit.hd.ui.gravatar.Gravatars;
import org.multibit.hd.ui.utils.ClipboardUtils;
import org.multibit.hd.ui.views.components.*;
import org.multibit.hd.ui.views.components.auto_complete.AutoCompleteFilter;
import org.multibit.hd.ui.views.components.auto_complete.AutoCompleteFilters;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

/**
 * <p>View to provide the following to UI:</p>
 * <ul>
 * <li>Presentation of a dual-purpose combobox</li>
 * <li>Support for locating contacts by name</li>
 * <li>Support for entering recipient Bitcoin address representations (address, seed, key etc)</li>
 * </ul>
 *
 * @since 0.0.1
 *  
 */
public class EnterRecipientView extends AbstractComponentView<EnterRecipientModel> {

  // View components
  private JComboBox<Recipient> recipientComboBox;
  private JLabel imageLabel;
  private JButton pasteButton;
  private Optional<Recipient> currentRecipient=Optional.absent();

  /**
   * @param model The model backing this view
   */
  public EnterRecipientView(EnterRecipientModel model) {
    super(model);

  }

  @Override
  public JPanel newComponentPanel() {

    AutoCompleteFilter<Recipient> filter = AutoCompleteFilters.newRecipientFilter();

    // Bind a key listener to allow instant update of UI to matched passwords
    recipientComboBox = ComboBoxes.newRecipientComboBox(filter);
    recipientComboBox.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        updateModelFromView();
      }
    });

    pasteButton = Buttons.newPasteButton(getPasteAction());

    JPanel panel = Panels.newPanel(new MigLayout(
      "fillx,insets 0", // Layout
      "[][][][]", // Columns
      "[]" // Rows
    ));

    // Start with an invisible label
    imageLabel = Labels.newImageLabel(Optional.<BufferedImage>absent());
    imageLabel.setVisible(false);

    panel.add(Labels.newRecipient());
    // Specify minimum width for consistent appearance across contact names and locales
    panel.add(recipientComboBox, "growx,w min:350:,push");
    panel.add(pasteButton, "shrink");
    panel.add(imageLabel, "shrink,wrap");

    return panel;

  }

  @Override
  public void updateModelFromView() {

    Object selectedItem = recipientComboBox.getSelectedItem();

    boolean showGravatar = false;
    if (selectedItem instanceof Recipient) {

      // We have a select from the drop down
      Recipient recipient = (Recipient) selectedItem;

      // Avoid double events triggering calls
      if (currentRecipient.isPresent() && currentRecipient.get().equals(recipient)) {
        return;
      }
      currentRecipient = Optional.of(recipient);

      getModel().get().setValue(recipient);

      // Display a gravatar if we have a contact
      if (recipient.getContact().isPresent()) {
        if (recipient.getContact().get().getEmail().isPresent()) {

          // We have an email address
          String emailAddress = recipient.getContact().get().getEmail().get();

          Optional<BufferedImage> image = Gravatars.retrieveGravatar(emailAddress);

          if (image.isPresent()) {
            imageLabel.setIcon(new ImageIcon(image.get()));
            imageLabel.setVisible(true);
            showGravatar = true;
          }
        }
      }
    }

    // There is no gravatar to show
    if (!showGravatar) {
      imageLabel.setVisible(false);
    }
  }

  /**
   * @return A new action for pasting the recipient information
   */
  private Action getPasteAction() {
    // Show or hide the seed phrase
    return new AbstractAction() {

      @Override
      public void actionPerformed(ActionEvent e) {

        Optional<String> pastedText = ClipboardUtils.pasteStringFromClipboard();

        if (pastedText.isPresent()) {
          recipientComboBox.getEditor().setItem(pastedText.get());
        }

      }
    };
  }

}

@@ -1124,74 +1124,8 @@
 ton;
-%0A  private Optional%3CRecipient%3E currentRecipient=Optional.absent();
 %0A%0A  
@@ -2467,24 +2467,101 @@
 tedItem();%0A%0A
+    Optional%3CRecipient%3E currentRecipient = getModel().get().getRecipient();%0A%0A
     boolean 
@@ -2689,17 +2689,25 @@
 cipient 
-r
+selectedR
 ecipient
@@ -2854,17 +2854,25 @@
 .equals(
-r
+selectedR
 ecipient
@@ -2895,24 +2895,71 @@
 urn;%0A      %7D
+%0A%0A      // Update the current with the selected
 %0A      curre
@@ -2984,17 +2984,25 @@
 onal.of(
-r
+selectedR
 ecipient
@@ -3037,17 +3037,25 @@
 etValue(
-r
+selectedR
 ecipient
@@ -3109,33 +3109,41 @@
 ntact%0A      if (
-r
+selectedR
 ecipient.getCont
@@ -3175,17 +3175,25 @@
     if (
-r
+selectedR
 ecipient
@@ -3309,17 +3309,25 @@
 dress = 
-r
+selectedR
 ecipient
@@ -3650,24 +3650,165 @@
      %7D%0A    %7D
+ else %7B%0A      // Create a recipient based on the text entry%0A      currentRecipient = Optional.of(new Recipient((String) selectedItem));%0A    %7D
 %0A%0A    // The
@@ -3900,16 +3900,97 @@
 ;%0A    %7D%0A
+%0A    // Update the model%0A    getModel().get().setValue(currentRecipient.get());%0A%0A
   %7D%0A%0A  /

Fix for sending to entered address not triggering "Next"