/*******************************************************************************
 * Copyright (c) 2017 Pivotal, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Pivotal, Inc. - initial API and implementation
 *******************************************************************************/
package org.springframework.ide.vscode.boot.java.utils;

import java.time.Duration;
import java.util.Arrays;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.stream.Stream;

import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.springframework.ide.vscode.boot.java.handlers.BootJavaHoverProvider;
import org.springframework.ide.vscode.boot.java.handlers.RunningAppProvider;
import org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.HighlightParams;
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder;
import org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver;
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.util.Log;
import org.springframework.ide.vscode.commons.util.text.TextDocument;

/**
 * @author Martin Lippert
 */
public class SpringLiveHoverWatchdog {


	public static final Duration DEFAULT_INTERVAL = Duration.ofMillis(5000);

	private final long POLLING_INTERVAL_MILLISECONDS;
	private final Set<String> watchedDocs;
	private final SimpleLanguageServer server;
	private final BootJavaHoverProvider hoverProvider;
	private RunningAppProvider runningAppProvider;

	private boolean highlightsEnabled = true;

	private Timer timer;

	private JavaProjectFinder projectFinder;

	private void refreshEnablement() {
		boolean shouldEnable = highlightsEnabled && hasInterestingProject(watchedDocs.stream());
		if (shouldEnable) {
			start();
		} else {
			shutdown();
		}
	}

	private boolean hasInterestingProject(Stream<String> uris) {
		return uris.anyMatch(uri -> projectFinder.find(new TextDocumentIdentifier(uri)).isPresent());
	}

	public SpringLiveHoverWatchdog(
			SimpleLanguageServer server,
			BootJavaHoverProvider hoverProvider,
			RunningAppProvider runningAppProvider,
			JavaProjectFinder projectFinder,
			ProjectObserver projectChanges,
			Duration pollingInterval
	) {
		this.POLLING_INTERVAL_MILLISECONDS = pollingInterval == null ? DEFAULT_INTERVAL.toMillis() : pollingInterval.toMillis();
		this.server = server;
		this.hoverProvider = hoverProvider;
		this.runningAppProvider = runningAppProvider;
		this.projectFinder = projectFinder;
		this.watchedDocs = new ConcurrentSkipListSet<>();
		projectChanges.addListener(new ProjectObserver.Listener() {

			@Override
			public void deleted(IJavaProject project) {
				refreshEnablement();
			}

			@Override
			public void created(IJavaProject project) {
				refreshEnablement();
			}

			@Override
			public void changed(IJavaProject project) {
				refreshEnablement();
			}
		});
	}

	private synchronized void start() {
		if (highlightsEnabled && timer == null) {
			Log.debug("Starting SpringLiveHoverWatchdog");
			this.timer = new Timer();

			TimerTask task = new TimerTask() {
				@Override
				public void run() {
					update();
				}
			};

			timer.scheduleAtFixedRate(task, 0, POLLING_INTERVAL_MILLISECONDS);
		}
	}

	public synchronized void shutdown() {
		if (timer != null) {
			Log.info("Shutting down SpringLiveHoverWatchdog");
			timer.cancel();
			timer = null;
			watchedDocs.forEach(uri -> cleanupLiveHints(uri));
		}
	}

	public synchronized void watchDocument(String docURI) {
		this.watchedDocs.add(docURI);
		refreshEnablement();
	}

	public synchronized void unwatchDocument(String docURI) {
		this.watchedDocs.remove(docURI);
		cleanupLiveHints(docURI);

		if (watchedDocs.size() == 0) {
			cleanupResources();
		}
		refreshEnablement();
	}

	public void update(String docURI) {
		if (highlightsEnabled) {
			try {
				SpringBootApp[] runningBootApps = runningAppProvider.getAllRunningSpringApps().toArray(new SpringBootApp[0]);
				TextDocument doc = this.server.getTextDocumentService().get(docURI);
				if (doc != null) {
					Range[] ranges = this.hoverProvider.getLiveHoverHints(doc, runningBootApps);
					publishLiveHints(docURI, ranges);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	protected void update() {
		if (this.watchedDocs.size() > 0) {
			for (String docURI : watchedDocs) {
				update(docURI);
			}
		}
	}

	private void publishLiveHints(String docURI, Range[] ranges) {
		server.getClient().highlight(new HighlightParams(new TextDocumentIdentifier(docURI), Arrays.asList(ranges)));
	}

	private void cleanupLiveHints(String docURI) {
		publishLiveHints(docURI, new Range[0]);
	}

	private void cleanupResources() {
		// TODO: close and cleanup open JMX connections and cached data
	}

	public synchronized void enableHighlights() {
		if (!highlightsEnabled) {
			highlightsEnabled = true;
			refreshEnablement();
		}
	}

	public synchronized void disableHighlights() {
		if (highlightsEnabled) {
			highlightsEnabled = false;
			refreshEnablement();
		}
	}

}
