package ru.ifmo.neerc.chat.client;

import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChatInputArea extends JTextArea {
    private AbstractChatClient client;
    private String prefix;

    private final MessageLocalHistory messageLocalHistory = new MessageLocalHistory(0);

    private class KeyListener extends KeyAdapter {

        public void keyTyped(KeyEvent e1) {
            if (e1.getKeyChar() == KeyEvent.VK_ENTER) {
                boolean hasCtrl = e1.isControlDown();
                if (hasCtrl != client.sendOnEnter()) {
                    String text = getText().trim();
                    if (text.isEmpty())
                        return;

                    Matcher privateMatcher = Pattern.compile("^([a-zA-Z0-9%]+)>", Pattern.DOTALL).matcher(text);
                    if (privateMatcher.find() && privateMatcher.groupCount() > 0) {
                        messageLocalHistory.setLastPrivateAddressees(privateMatcher.group(1));
                    }

                    messageLocalHistory.add(text);
                    client.send(prefix + text);
                    setText("");
                } else {
                    if (hasCtrl)
                        append("\n");
                }
            }
        }

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_UP && !e.isControlDown()) {
                try {
                    int caretPosition = getCaretPosition();
                    int lineNum = getLineOfOffset(caretPosition);
                    if (lineNum == 0) {
                        setPrivateAddressees(messageLocalHistory.getLastPrivateAddressees());
                    }

                } catch (BadLocationException e1) {
                    // it's not like anything can be done here
                }
            } else if (e.getKeyCode() == KeyEvent.VK_DOWN && e.isControlDown()) {
                String message = messageLocalHistory.moveDown();
                if (message != null) {
                    setText(message);
                }
            } else if (e.getKeyCode() == KeyEvent.VK_UP && e.isControlDown()) {
                String message = messageLocalHistory.moveUp();
                if (message != null) {
                    setText(message);
                }
            } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                setText("");
            }
        }
    }

    public ChatInputArea(AbstractChatClient client, String user) {
        super(2, 45);
        setLineWrap(true);
        setWrapStyleWord(true);
        addKeyListener(new KeyListener());

        this.client = client;
        if (user == null || user.isEmpty())
            this.prefix = "";
        else
            this.prefix = user + "> ";
    }

    public void setPrivateAddressees(String addressees) {
        if (!"".equals(addressees)) {
            String text = getText();
            text = text.replaceAll("\\A[a-zA-Z0-9%]+>\\s*", "");
            text = addressees + "> " + text;
            setText(text);
        }
        requestFocus();
    }
}

@@ -491,16 +491,34 @@
 pter %7B%0A%0A
+        @Override%0A
         
@@ -536,11 +536,13 @@
  key
-Typ
+Press
 ed(K
@@ -550,17 +550,16 @@
 yEvent e
-1
 ) %7B%0A    
@@ -575,17 +575,16 @@
 f (e
-1
 .getKeyC
 har(
@@ -583,11 +583,11 @@
 KeyC
-har
+ode
 () =
@@ -644,17 +644,16 @@
 Ctrl = e
-1
 .isContr
@@ -1280,32 +1280,66 @@
     setText(%22%22);
+%0A%0A                    e.consume();
 %0A               
@@ -1380,16 +1380,18 @@
 hasCtrl)
+ %7B
 %0A       
@@ -1411,19 +1411,39 @@
     
-append(%22%5Cn%22
+insert(%22%5Cn%22, getCaretPosition()
 );%0A 
@@ -1445,34 +1445,24 @@
 ));%0A        
-        %7D%0A
             
@@ -1471,27 +1471,24 @@
         
-%7D%0A%0A
         
 public v
@@ -1483,44 +1483,9 @@
     
-public void keyPressed(KeyEvent e) %7B
+%7D
 %0A   
@@ -1485,32 +1485,39 @@
   %7D%0A            
+%7D else 
 if (e.getKeyCode

Fix input when sending messages on Enter