/*******************************************************************************
 * 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.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.ide.vscode.boot.java.links.SourceLinks;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.util.Renderables;

/**
 * Helper class, represents parsed info from a Resource, and provide method(s) to
 * display it somehow.
 */
public class SpringResource {

	private static final String FILE = "file";
	private static final String CLASS_PATH_RESOURCE = "class path resource";

	private SourceLinks sourceLinks;
	private String type;
	private String path;
	private IJavaProject project;

	private static final Pattern BRACKETS = Pattern.compile("\\[[^\\]]*\\]");

	public SpringResource(SourceLinks sourceLinks, String toParse, IJavaProject project) {
		this.sourceLinks = sourceLinks;
		this.project = project;
		Matcher matcher = BRACKETS.matcher(toParse);
		if (matcher.find()) {
			type = toParse.substring(0, matcher.start()).trim();
			path = toParse.substring(matcher.start()+1, matcher.end()-1);
		} else {
			path = toParse;
		}
	}

	public String toMarkdown() {
		if (type==null) {
			return path; //path is just the raw text in this case
		}
		Optional<String> linkUrl;
		switch (type) {
		case FILE:
			String relativePath = projectRelativePath(path);
			if (relativePath != path && path.endsWith(SourceLinks.CLASS)) {
				linkUrl = sourceLinks.sourceLinkUrlForClasspathResource(project, relativePath);
			} else {
				linkUrl = sourceLinks.sourceLinkForResourcePath(Paths.get(path));
			}
			// not a project relative path
			return linkUrl.isPresent() ? Renderables.link(relativePath, linkUrl.get()).toMarkdown()
					: "`" + projectRelativePath(path) + "`";
		case CLASS_PATH_RESOURCE:
			linkUrl = sourceLinks.sourceLinkUrlForClasspathResource(project, path);
			return linkUrl.isPresent() ? Renderables.link(path, linkUrl.get()).toMarkdown() : "`"+path+"`";
		default:
			return path;
		}
	}

	private String projectRelativePath(String pathStr) {
		Path path = Paths.get(pathStr);
		IClasspath classpath = project.getClasspath();
		Path outputFolder = classpath.getOutputFolder();
		if (path.startsWith(outputFolder)) {
			return outputFolder.relativize(path).toString();
		}
		return pathStr;
	}

}

@@ -95,16 +95,22 @@
 (c) 2017
+, 2018
  Pivotal
@@ -1429,16 +1429,206 @@
 %5D*%5C%5C%5D%22);
+%0A%09%0A%09private static final String ID_PATTERN = %22%5C%5Cp%7BjavaJavaIdentifierStart%7D%5C%5Cp%7BjavaJavaIdentifierPart%7D*%22;%0A    private static final String REGEX_FQCN = ID_PATTERN + %22(%5C%5C.%22 + ID_PATTERN + %22)*%22;
 %0A%0A%09publi
@@ -1961,16 +1961,205 @@
 d()-1);%0A
+%09%09%7D else if (Pattern.matches(REGEX_FQCN, toParse)) %7B%0A%09%09%09// Resource is fully qualified Java type name%0A%09%09%09type = CLASS_PATH_RESOURCE;%0A%09%09%09path = toParse.replace('.','/') + SourceLinks.CLASS;%0A
 %09%09%7D else

Handle fully qualified type names in resource from JMX bean