/*
 * 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.selenium2.webclient.protractor.preferences;

import java.io.File;
import java.util.prefs.PreferenceChangeListener;
import java.util.prefs.Preferences;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.spi.project.ProjectServiceProvider;
import org.netbeans.spi.project.support.ant.PropertyUtils;
import org.openide.filesystems.FileUtil;

/**
 *
 * @author Theofanis Oikonomou
 */
@ProjectServiceProvider(service = ProtractorPreferences.class, projectType = "org-netbeans-modules-web-clientproject") // NOI18N
public final class ProtractorPreferences {

    public static final String USER_CONFIGURATION_FILE = "user.configuration.file"; // NOI18N
    public static final String ENABLED = "enabled"; // NOI18N
    private static final String PROTRACTOR = "protractor"; // NOI18N

    private final Project project;

    // @GuardedBy("this")
    private Preferences preferences;


    public ProtractorPreferences(Project project) {
        assert project != null;
        this.project = project;
    }

    public static boolean isEnabled(Project project) {
        return getPreferences(project).getBoolean(ENABLED, false);
    }

    public static void setEnabled(Project project, boolean enabled) {
        getPreferences(project).putBoolean(ENABLED, enabled);
    }

    @CheckForNull
    public static String getProtractor(Project project) {
        return resolvePath(project, getPreferences(project).get(PROTRACTOR, null));
    }

    public static void setProtractor(Project project, String protractor) {
        getPreferences(project).put(PROTRACTOR, relativizePath(project, protractor));
    }

    @CheckForNull
    public static String getUserConfigurationFile(Project project) {
        return resolvePath(project, getPreferences(project).get(USER_CONFIGURATION_FILE, null));
    }

    public static void setUserConfigurationFile(Project project, String userConfigurationFile) {
        getPreferences(project).put(USER_CONFIGURATION_FILE, relativizePath(project, userConfigurationFile));
    }

    public static void addPreferenceChangeListener(Project project, PreferenceChangeListener listener) {
        getPreferences(project).addPreferenceChangeListener(listener);
    }

    public static void removePreferenceChangeListener(Project project, PreferenceChangeListener listener) {
        getPreferences(project).removePreferenceChangeListener(listener);
    }

    private static Preferences getPreferences(final Project project) {
        assert project != null;
        return project.getLookup()
                .lookup(ProtractorPreferences.class)
                .getPreferences();
    }

    synchronized Preferences getPreferences() {
        if (preferences == null) {
            preferences = ProjectUtils.getPreferences(project, ProtractorPreferences.class, false);
        }
        return preferences;
    }

    private static String relativizePath(Project project, String filePath) {
        if (filePath == null
                || filePath.trim().isEmpty()) {
            return ""; // NOI18N
        }
        File file = new File(filePath);
        String path = PropertyUtils.relativizeFile(FileUtil.toFile(project.getProjectDirectory()), file);
        if (path == null
                || path.startsWith("../")) { // NOI18N
            // cannot be relativized or outside project
            path = file.getAbsolutePath();
        }
        return path;
    }

    private static String resolvePath(Project project, String filePath) {
        if (filePath == null
                || filePath.trim().isEmpty()) {
            return null;
        }
        return PropertyUtils.resolveFile(FileUtil.toFile(project.getProjectDirectory()), filePath).getAbsolutePath();
    }

}
