package view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.Document;

import controller.MessageBoardController;
import model.User;
import model.ViewConstraints;

public class MessageBoard extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private User user;
	
	private JLabel currentChat;
	private JButton sendButton;
	private JTextArea inputText;
	private JTextPane messagePane;
	private Document doc;
	
	private GridBagConstraints c;
	
	public MessageBoard(User user, MessageBoardController controller){
		if(user == null){
			this.user = new User("All users");			
		}else{
			this.user = user;
		}
		initializeComponents(controller);
		addComponents();
	}
	
	private void initializeComponents(MessageBoardController controller){
		currentChat = new JLabel("You chat with: "+user.getUserName());
		currentChat.setFont(ViewConstraints.TITLE_LABLE_FONT);
		sendButton = new JButton(ViewConstraints.SEND_BUTTON_TEXT);
		inputText = new JTextArea();

		inputText.setBorder(BorderFactory.createCompoundBorder(
					ViewConstraints.BASIC_BORDER, 
		            BorderFactory.createEmptyBorder(10, 10, 10, 10)));
		
		sendButton.addActionListener(controller);
		
		messagePane = new JTextPane();
		doc = messagePane.getDocument();
		
		messagePane.setEditable(false);
		messagePane.setBackground(ViewConstraints.BASIC_BG_COLOR);
		messagePane.setBorder(BorderFactory.createCompoundBorder(
				ViewConstraints.BASIC_BORDER, 
	            BorderFactory.createEmptyBorder(10, 10, 10, 10)));
		c = new GridBagConstraints();
		this.setLayout(new GridBagLayout());
		this.setPreferredSize(new Dimension(ViewConstraints.LEFT_PANEL_WIDTH-10,ViewConstraints.PANEL_HEIGHT-10));
	}
	private void addComponents(){
		c.fill = GridBagConstraints.BOTH;
		c.gridy = 0;
		c.gridx = 0;
		c.weightx = 1;
		c.weighty = 1;
		this.add(currentChat, c);		
		c.weighty = 20;
		c.fill = GridBagConstraints.BOTH;
		c.gridy = 1;	
		this.add(messagePane,c);
		c.weighty = 5;
		c.gridy = 2;
		c.insets = new Insets(10, 0, 0, 0);
		this.add(inputText, c);
		c.anchor = GridBagConstraints.PAGE_END;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weighty = 1;
		c.gridy = 3;
		this.add(sendButton,c);
	}
	
	public void setUser(User user){
		this.user = user;
	}
	
	public User getUser(){
		return user;
	}	
	
	public Document getDoc(){
		return doc;
	}
	
	public String getMessage(){		
		String mess = inputText.getText();
		System.out.println("in get" + mess);
		inputText.setText("");
		return mess;
	}
}

@@ -239,24 +239,56 @@
 ing.JPanel;%0A
+import javax.swing.JScrollPane;%0A
 import javax
@@ -305,16 +305,16 @@
 xtArea;%0A
-
 import j
@@ -715,16 +715,49 @@
 gePane;%0A
+%09private JScrollPane scrollPane;%0A
 %09private
@@ -1284,16 +1284,18 @@
 N_TEXT);
+%09%09
 %0A%09%09input
@@ -1632,26 +1632,189 @@
 (false);
+%09%09
 %0A%09%09
-message
+scrollPane = new JScrollPane(messagePane);%0A%09%09scrollPane.setPreferredSize(new Dimension(ViewConstraints.LEFT_PANEL_WIDTH-10,ViewConstraints.PANEL_HEIGHT/3-10));%0A%09%09scroll
 Pane.set
@@ -1859,23 +1859,22 @@
 LOR);%0A%09%09
-message
+scroll
 Pane.set
@@ -2015,16 +2015,19 @@
  10)));%0A
+%09%09%0A
 %09%09c = ne
@@ -2331,16 +2331,18 @@
 hty = 1;
+%09%09
 %0A%09%09this.
@@ -2445,23 +2445,22 @@
 his.add(
-message
+scroll
 Pane,c);

Fix Client MessageBoard autoresize