package cs342.Chat.GUI.custom;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.text.*;

import java.awt.*;
import java.util.HashMap;

public class ChatArea {

	private ChatPane cstmPane;
	private JScrollBar vertical;
	public final JScrollPane SCROLL_PANE;
	private HashMap<String, StyledDocument> CONVO_TO_DOC;
	
	public ChatArea() {
		SCROLL_PANE = new JScrollPane();
		CONVO_TO_DOC = new HashMap<String, StyledDocument>();
		cstmPane = new ChatPane();
		SCROLL_PANE.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
		SCROLL_PANE.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		SCROLL_PANE.setViewportView(cstmPane);
		vertical = SCROLL_PANE.getVerticalScrollBar();
	}
	
	public void showChat(String entry) {
		cstmPane.doc = CONVO_TO_DOC.get(entry);
		cstmPane.setStyledDocument(cstmPane.doc);
	}

	public void createNewChat(String convo) {
		CONVO_TO_DOC.put(convo, new DefaultStyledDocument());
	}

	public void removeChat(String convo) {
		CONVO_TO_DOC.remove(convo);
	}
	
	public void clientPrint(String line, String who, String where) {
		cstmPane.doc = CONVO_TO_DOC.get(where);
		cstmPane.clientPrint(line, who);
		setScrollBarMinimum();
	}

	public void serverPrint(String line, String who, String where) {
		cstmPane.doc = CONVO_TO_DOC.get(where);
		cstmPane.serverPrint(line, who);
		setScrollBarMinimum();
	}

	private void setScrollBarMinimum() {
		vertical.setValue( vertical.getMinimum());
	}
	
	private class ChatPane extends JTextPane {

		private static final long serialVersionUID = 1L;

		private StyledDocument doc;
		private Style style;
		private SimpleAttributeSet align;
		private Color BACKGROUND = new Color(47, 79, 79);
		private Color CLIENT_COLOR = new Color(255, 69, 0);
		private Color SERVER_COLOR = new Color(0, 69, 255).brighter();
		private Insets MARGINS = new Insets(5, 5, 5, 5);
		private String FONT_NAME = "Calibri";
		private int FONT_SIZE = 16;

		private ChatPane() {
			super();
			super.setEditorKit(new WrapEditorKit());
			super.setEditable(false);
			super.setBackground(BACKGROUND);
			super.setMargin(MARGINS);
			style = this.addStyle("STYLE", null);
			align = new SimpleAttributeSet();
			StyleConstants.setFontFamily(style, FONT_NAME);
			StyleConstants.setFontSize(style, FONT_SIZE);
			StyleConstants.setBold(style, true);
		}
		
		private void clientPrint(String line, String who) {
			StyleConstants.setAlignment(align, StyleConstants.ALIGN_RIGHT);
			StyleConstants.setForeground(style, CLIENT_COLOR);
			print(line + "\n");
		}

		private void serverPrint(String line, String who) {
			StyleConstants.setAlignment(align, StyleConstants.ALIGN_LEFT);
			StyleConstants.setForeground(style, SERVER_COLOR.brighter());
			print(who + ": ");
			StyleConstants.setForeground(style, SERVER_COLOR);
			print(line + "\n");
		}

		private void print(String line) {
			try {
				doc.setParagraphAttributes(doc.getLength(), line.length(),
						align, false);
				doc.insertString(doc.getLength(), line, style);
			} catch (BadLocationException e) {
			}
		}
	}
}