/*
 * Copyright 2018, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.tools.protojs.files;

import org.gradle.api.Project;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.code.proto.FileDescriptors.KNOWN_TYPES;

@SuppressWarnings("DuplicateStringLiteralInspection") // Random duplication of path elements.
public final class ProjectFiles {

    private static final String MAIN = "main";
    private static final String TEST = "test";

    private ProjectFiles() {
    }

    public static Path mainProtoJsLocation(Project project) {
        checkNotNull(project);
        Path location = protoJsLocation(project, MAIN);
        return location;
    }

    public static Path testProtoJsLocation(Project project) {
        checkNotNull(project);
        Path location = protoJsLocation(project, TEST);
        return location;
    }

    public static File mainDescriptorSetFile(Project project) {
        checkNotNull(project);
        File file = descriptorSetFile(project, MAIN);
        return file;
    }

    public static File testDescriptorSetFile(Project project) {
        checkNotNull(project);
        File file = descriptorSetFile(project, TEST);
        return file;
    }

    private static Path protoJsLocation(Project project, String sourceSet) {
        File projectDir = project.getProjectDir();
        String absolutePath = projectDir.getAbsolutePath();
        return Paths.get(absolutePath, "proto", sourceSet, "js");
    }

    private static File descriptorSetFile(Project project, String sourceSet) {
        File projectDir = project.getProjectDir();
        String absolutePath = projectDir.getAbsolutePath();
        Path path = Paths.get(absolutePath, "build", "descriptors", sourceSet, KNOWN_TYPES);
        File file = path.toFile();
        return file;
    }
}

@@ -1041,24 +1041,61 @@
 ojs.files;%0A%0A
+import io.spine.code.DefaultProject;%0A
 import org.g
@@ -1259,71 +1259,8 @@
 ull;
-%0Aimport static io.spine.code.proto.FileDescriptors.KNOWN_TYPES;
 %0A%0A@S
@@ -1986,51 +1986,135 @@
     
-File file = descriptorSetFile(project, MAIN
+DefaultProject defaultProject = DefaultProject.at(project.getProjectDir());%0A        File file = defaultProject.mainDescriptors(
 );%0A 
@@ -2247,185 +2247,58 @@
     
-File file = descriptorSetFile(project, TEST);%0A        return file;%0A    %7D%0A%0A    private static Path protoJsLocation(Project project, String sourceSet) %7B%0A        File projectDir = 
+DefaultProject defaultProject = DefaultProject.at(
 proj
@@ -2308,32 +2308,33 @@
 .getProjectDir()
+)
 ;%0A        String
@@ -2331,124 +2331,73 @@
     
-String absolutePath = projectDir.getAbsolutePath();%0A        return Paths.get(absolutePath, %22proto%22, sourceSet, %22js%22)
+File file = defaultProject.testDescriptors();%0A        return file
 ;%0A  
@@ -2424,30 +2424,28 @@
 tic 
-File descriptorSetFile
+Path protoJsLocation
 (Pro
@@ -2602,20 +2602,24 @@
    Path 
-path
+location
  = Paths
@@ -2642,88 +2642,31 @@
 h, %22
-build%22, %22descriptors%22, sourceSet, KNOWN_TYPES);%0A        File file = path.toFile(
+proto%22, sourceSet, %22js%22
 );%0A 
@@ -2671,30 +2671,34 @@
 %0A        return 
-file
+location
 ;%0A    %7D%0A%7D%0A

Use `DefaultProject` facility for descriptor file lookup