import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class MainLobby {
	static Stage primaryStage;
	static BorderPane border = new BorderPane();
	static Scene scene;
	Client client;
	static TextArea messageList;
	static ListView userList;
	//static HashMap<String, String> privateMessages = new HashMap<String, String>();
	static HashMap<String, Conversation> privateMessageWindows = new HashMap<String, Conversation>();
	static Image image;

	MainLobby(Stage primaryStage, Client client) {
		this.client = client;
		primaryStage.close();
		primaryStage.setTitle("Main Lobby");

		//Text lobbytitle = new Text("Lobby");
		try {
			image = new Image(new FileInputStream("Officon_Logo.png"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ImageView imageView = new ImageView(image);
		//Setting the preserve ratio of the image view 
	    imageView.setPreserveRatio(true); 
	    imageView.setFitHeight(46);
		StackPane stack = new StackPane();
		stack.setPadding(new Insets(15, 12, 15, 12));
		//lobbytitle.prefWidth(800);
		//lobbytitle.setTextAlignment(TextAlignment.CENTER);
		//lobbytitle.setFont(new Font(20));
		stack.getChildren().add(imageView);
		border.setTop(stack);
		
		VBox vbox = new VBox();
		vbox.setPadding(new Insets(15, 12, 15, 12));
		Text listtitle = new Text("Online users");
		StackPane liststack = new StackPane();
		liststack.setPadding(new Insets(15, 12, 15, 12));
		liststack.getChildren().add(listtitle);
		userList = new ListView();
		for (String user : App.activeUsers) {
			userList.getItems().add(user);
		}
		userList.setPrefHeight(600);

		Button button = new Button("Message User");
		button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
            	String username = (String) userList.getSelectionModel().getSelectedItem();
            	if (privateMessageWindows.get(username) != null && privateMessageWindows.containsKey(username)) {
            		privateMessageWindows.get(username).show();
            	}
            	else {
            		privateMessageWindows.put(username, new Conversation(username, client));
            	}
            	
            }
        });
		
		button.setPrefWidth(250);
		vbox.getChildren().addAll(liststack, userList, button);
		border.setLeft(vbox);

		HBox hbox = new HBox();
		hbox.setPadding(new Insets(15, 12, 15, 12));
		hbox.setSpacing(10);
		// hbox.setStyle("-fx-background-color: #336699;");

		TextField messageTextField = new TextField();
		messageTextField.setText("");
		messageTextField.setPrefSize(600, 20);
		Button buttonCurrent = new Button("Send message");
		buttonCurrent.setPrefSize(200, 20);
		buttonCurrent.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
            	String message = App.username + ": " + messageTextField.getText();
            	client.message(message);
            	App.messages.add(message);
            	messageTextField.setText("");
            	updateMessages(message);
            }
        });
		hbox.getChildren().addAll(messageTextField, buttonCurrent);

		border.setBottom(hbox);
		
		ScrollPane scroll = new ScrollPane();
		scroll.setFitToHeight(true);
        scroll.setFitToWidth(true);
        scroll.setPadding(new Insets(15, 12, 15, 12));
		messageList = new TextArea();
        String messages = "";
        if (App.messages != null) {
        	for (String message: App.messages) {
            	messages += message +"\n";
            }
        }
        messageList.setText(messages);
        messageList.setEditable(false);
        messageList.setWrapText(true);
        messageList.setPrefSize(525, 500);
        scroll.setContent(messageList);
        border.setCenter(scroll);
        
        
        scene = new Scene(border, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
        this.primaryStage = primaryStage;
	}

	public static void updateUsers() {
		for (String user : App.activeUsers) {
			userList.getItems().add(user);
		}
		userList.refresh();
	}
	
	public static void updateMessages(String text) {
		messageList.appendText(text + "\n");
	}
}
