import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.Dimension;

import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuItem; 
import javax.swing.JPopupMenu; 
import javax.swing.*;
import java.util.*;

public class MessagePopup2 extends JPopupMenu implements ActionListener { 
  
  private static final long serialVersionUID = 1L; 
  
  JMenuItem bmi, ami, emi, kmi, dmi1, dmi2, dmi3, vmi; 
  ChatClient cc;
  Nachricht n;
  ArrayList<Key> keys;
  
  public MessagePopup2( MouseEvent event, ChatClient cc, Nachricht n,  ArrayList<Key> keys){ 
    this.cc = cc;
    this.n = n;
    this.keys = keys;
    
    //      Submenu
    if (!n.getSended() && n.getInhaltString().startsWith("Key:")) {
      ami = new JMenuItem("Schlüssel austauschen");
      ami.addActionListener(this);
      add(ami);
    } // end of if
    if (!n.getSended()) {
      emi = new JMenuItem("Nachricht verändern");
      emi.addActionListener(this);
      add(emi);
      
      this.addSeparator();
      
    } // end of if
    dmi1 = new JMenuItem("Hashcode berechnen"); 
    dmi1.addActionListener(this); 
    add(dmi1); 
    dmi2 = new JMenuItem("Hashcode prüfen"); 
    dmi2.addActionListener(this); 
    add(dmi2); 
    show(event.getComponent(), event.getX(), event.getY()); 
  } 
  
  public void actionPerformed(ActionEvent e) { 
    
    if (e.getSource() == ami && !n.getSended()) {
      n.setStopped(true);
      
      JComboBox cb = new JComboBox();
      for(Key k: keys) {
           cb.addItem(k.getName());
      } // end of for
      
      
      Object[] message = {"Wähle den Schlüssel, der eingesetzt werden soll.", cb};
      
      JOptionPane pane = new JOptionPane( message, 
      JOptionPane.PLAIN_MESSAGE, 
      JOptionPane.OK_CANCEL_OPTION);
      pane.createDialog(null, "Nachricht manipulieren").setVisible(true);
      
      int value = ((Integer)pane.getValue()).intValue();
      if(value == JOptionPane.OK_OPTION) {
        String s = n.getInhaltString();
        String[] ss = s.split(",|=");
        String ks = keys.get(cb.getSelectedIndex()).toString(ss[1]);
        Nachricht nn = n.copy();
        nn.setInhalt(ks);
        cc.nachrichten.add(cc.nachrichten.indexOf(n)+1,nn);
        cc.updateNachrichtenListe();
      } // end of if
      
      
      
    } // end of if
    
    if (e.getSource() == emi && !n.getSended()) {
    n.setStopped(true);
    JTextArea mess = new JTextArea(n.getInhaltString());
    JScrollPane scrollPane = new JScrollPane(mess);  
    mess.setLineWrap(true);  
    mess.setWrapStyleWord(true); 
    scrollPane.setPreferredSize( new Dimension( 500, 200 ) );
    
    Object[] message = {"Nachricht", scrollPane};
    
    JOptionPane pane = new JOptionPane( message, 
    JOptionPane.PLAIN_MESSAGE, 
    JOptionPane.OK_CANCEL_OPTION);
    pane.createDialog(null, "Nachricht manipulieren").setVisible(true);
    
    int value = ((Integer)pane.getValue()).intValue();
    if(value == JOptionPane.OK_OPTION) {
    n.setInhalt(mess.getText());
    } // end of if
    cc.updateNachrichtenListe();
    }
    
    if (e.getSource() == dmi1) { 
    n.generateHash();
    cc.updateNachrichtenListe();
    } 
    if (e.getSource() == dmi2) { 
      Nachricht dummy = new Nachricht("dummy","dummy",n.getInhaltString());
      dummy.generateHash();
      String ha2 = new String(dummy.getHashCode());
      String ha = new String(n.getHashCode());
      System.out.println(ha2+","+ha);
      if(ha.equals(ha2)) {
    JOptionPane.showMessageDialog(this,
    "Der Hashcode der Nachricht stimmt mit dem übermittelten Hashcode überein",
    "Bestätigung",
    JOptionPane.INFORMATION_MESSAGE); 
    } else {
    JOptionPane.showMessageDialog(this,
    "Der Hashcode der Nachricht passt nicht zum übermittelten Hashcode.",
    "Fehler",
    JOptionPane.ERROR_MESSAGE);
    }
    } 
    } 
  } 
