package ui.issuepanel.comments;

import java.lang.ref.WeakReference;

import util.DialogMessage;
import handler.IssueDetailsContentHandler;
import model.TurboIssue;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

public class IssueDetailsDisplay extends VBox {
	public enum DisplayType{
		COMMENTS,
		LOG
	}
	
	private TabPane detailsTab;
	
	private IssueDetailsContentHandler contentHandler;
	
	private TurboIssue issue;
	
	DetailsPanel commentsDisplay;
	DetailsPanel issueLogDisplay;
	
	Thread backgroundThread;
	
	public IssueDetailsDisplay(TurboIssue issue){
		this.issue = issue;
		setupDetailsContents();
		setupDisplay();
	}
	
	private void setupDetailsContents(){
		contentHandler = new IssueDetailsContentHandler(issue);
	}
	
	
	private void setupDetailsTab(){
		this.detailsTab = new TabPane();
		Tab commentsTab = createCommentsTab();
		Tab logTab = createChangeLogTab();
		detailsTab.getTabs().addAll(commentsTab, logTab);
	}
	
	private void setupDisplay(){
		setupDetailsTab();
		this.getChildren().add(detailsTab);
	}
	
	public void show(){
		if(issue == null || issue.getId() <= 0){
			return;
		}
		
		Task<Boolean> bgTask = new Task<Boolean>(){

			@Override
			protected Boolean call() throws Exception {
				contentHandler.startContentUpdate();
				return true;
			}
			
		};	
		WeakReference<IssueDetailsDisplay> selfRef = new WeakReference<>(this);
		bgTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

            @Override
            public void handle(WorkerStateEvent t) {
            	IssueDetailsDisplay self = selfRef.get();
            	if(self != null){
            		self.scrollDisplayToBottom();
            	}
            }
        });
		DialogMessage.showProgressDialog(bgTask, "Loading Issue Comments...");
		backgroundThread = new Thread(bgTask);
		backgroundThread.start();
	}
	
	private void scrollDisplayToBottom(){
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				commentsDisplay.scrollToBottom();
        		issueLogDisplay.scrollToBottom();
			}
		});	
	}
	
	public void hide(){
		contentHandler.stopContentUpdate();
	}
	
	public void cleanup(){
		contentHandler.stopContentUpdate();
	}
	
	private DetailsPanel createTabContentsDisplay(DisplayType type){
		return new DetailsPanel(issue, contentHandler, type);
	}
	
	private Tab createCommentsTab(){
		Tab comments =  new Tab();
		comments.setText("C");
		comments.setClosable(false);
		commentsDisplay = createTabContentsDisplay(DisplayType.COMMENTS);
		VBox.setVgrow(commentsDisplay, Priority.ALWAYS);
		comments.setContent(commentsDisplay);
		return comments;
	}
	
	private Tab createChangeLogTab(){
		Tab log = new Tab();
		log.setText("Log");
		log.setClosable(false);
		issueLogDisplay = createTabContentsDisplay(DisplayType.LOG);
		log.setContent(issueLogDisplay);
		return log;
	}
	
}

@@ -63,16 +63,37 @@
 rence;%0A%0A
+import ui.StatusBar;%0A
 import u
@@ -111,16 +111,16 @@
 essage;%0A
-
 import h
@@ -319,24 +319,71 @@
 entHandler;%0A
+import javafx.scene.control.ProgressIndicator;%0A
 import javaf
@@ -722,16 +722,50 @@
 ssue;%0A%09%0A
+%09private int loadFailCount = 0;%0A%09%0A
 %09Details
@@ -1626,16 +1626,20 @@
 %09%09%09%0A%09%09%7D;
+%0A%09%09%09
 %09%0A%09%09Weak
@@ -1931,16 +1931,16 @@
  null)%7B%0A
-
         
@@ -1967,32 +1967,694 @@
 playToBottom();%0A
+            %09%09self.loadFailCount = 0;%0A            %09%7D%0A            %7D%0A        %7D);%0A%0A%09%09bgTask.setOnFailed(new EventHandler%3CWorkerStateEvent%3E() %7B%0A%0A            @Override%0A            public void handle(WorkerStateEvent t) %7B%0A            %09IssueDetailsDisplay self = selfRef.get();%0A            %09if(self != null)%7B%0A            %09%09self.loadFailCount += 1;%0A            %09%09if(loadFailCount %3C= 3)%7B%0A            %09%09%09self.show();%0A            %09%09%7Delse%7B%0A            %09%09%09//Notify user of load failure and reset count%0A            %09%09%09loadFailCount = 0;%0A            %09%09%09StatusBar.displayMessage(%22An error occured while loading the issue's comments. Comments partially loaded%22);%0A            %09%09%7D%0A
             %09%7D%0A 

handle failure during issue comments loading