/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.netbeans.modules.spring.beans.hyperlink;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.netbeans.api.java.project.JavaProjectConstants;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.api.project.SourceGroup;
import org.netbeans.api.project.Sources;
import org.openide.awt.StatusDisplayer;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.util.NbBundle;

/**
 *
 * @author Rohan Ranade (Rohan.Ranade@Sun.COM)
 */
public class ResourceHyperlinkProcessor extends HyperlinkProcessor {

    public ResourceHyperlinkProcessor() {
    }

    @Override
    public void process(HyperlinkEnv env) {
        FileObject fo = env.getFileObject();
        if (fo == null) {
            return;
        }
        FileObject parent = fo.getParent();

        String value = env.getValueString();
        if (value.contains("classpath")) {          //NOI18N
            if (openFromClasspath(fo, value)) {
                return;
            }
        } else {
            if (openFile(parent.getFileObject(env.getValueString()))) {
                return;
            }
        }

        String message = NbBundle.getMessage(ResourceHyperlinkProcessor.class, "LBL_ResourceNotFound", env.getValueString()); //NOI18N
        StatusDisplayer.getDefault().setStatusText(message);
    }

    private static boolean openFromClasspath(FileObject fo, String value) {
        int colonOffset = value.indexOf(":");   //NOI18N
        String subPath = value.substring(colonOffset + 1);
        Project project = FileOwnerQuery.getOwner(fo);
        if (project != null) {
            List<SourceGroup> groups = new ArrayList<>();
            Sources sources = ProjectUtils.getSources(project);
            groups.addAll(Arrays.asList(sources.getSourceGroups("doc_root"))); //NOI18N
            groups.addAll(Arrays.asList(sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)));
            for (SourceGroup sourceGroup : groups) {
                FileObject fileObject = sourceGroup.getRootFolder().getFileObject(subPath);
                if (fileObject != null) {
                    openFile(fileObject);
                    return true;
                }
            }
        }
        return false;
    }

    private static boolean openFile(FileObject file) {
        if (file == null) {
            return false;
        }
        DataObject dObj;
        try {
            dObj = DataObject.find(file);
        } catch (DataObjectNotFoundException ex) {
            return false;
        }
        EditorCookie editorCookie = dObj.getCookie(EditorCookie.class);
        if (editorCookie == null) {
            return false;
        }
        editorCookie.open();
        return true;
    }
}
